Middle
ApplicationContext-də eyni bini almağa çalışsanız nə baş verər?
sobes.tech Süni İntellekt
AI-dan cavab
ApplicationContext varsayılan olaraq eyni nümunəyə istinad edən bir referans qaytaracaq, çünki Spring-də beyin görünüş sahəsi singleton-dır.
Əgər beyin başqa bir görünüş sahəsi ilə konfiqurasiya olunubsa, məsələn prototype, hər sorğuda ApplicationContext yeni beyin nümunəsini yaradacaq və geri qaytaracaq.
prototype görünüş sahəsi ilə beyin konfiqurasiyası nümunəsi:
@Configuration
public class AppConfig {
@Bean
@Scope("prototype") // Görünüş sahəsini `prototype` olaraq göstəririk
public MyPrototypeBean myPrototypeBean() {
return new MyPrototypeBean();
}
}
ApplicationContext-dən beyin alınması nümunəsi:
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyPrototypeBean bean1 = context.getBean(MyPrototypeBean.class);
MyPrototypeBean bean2 = context.getBean(MyPrototypeBean.class);
System.out.println(bean1 == bean2); // Singleton üçün true, prototype üçün false
context.close();
}
}