9 Kasım 2018 Cuma

SpringContext BeanFactory Arayüzü

Giriş 
Şu satırı dahil ederiz
import org.springframework.beans.factory.BeanFactory;
BeanFactory Nedir?
Açıklaması şöyle.
A FactoryBean is an interface that you, as a developer, implements when writing factory classes and you want the object created by the factory class to be managed as a bean by Spring, while a BeanFactory on the other hand, represents the Spring IoC container, it contains the managed beans and provides access to retrieving them. It is part of the core of the framework which implements the base functionality of an inversion of control container.
Lifecycle
Bean Lifecycle yazısına bakabilirsiniz

BeanFactory ve ApplicationContext Arasındaki Fark Nedir?
ApplicationContext bu arayüzden kalıtır. Şeklen şöyle

Açıklaması şöyle.
The difference between BeanFactory and ApplicationContext in Spring framework is another frequently asked Spring interview question mostly asked Java programmers with 2 to 4 years experience in Java and Spring. Both BeanFactory and ApplicationContext provides a way to get a bean from Spring IOC container by calling getBean("bean name"), but there is some difference in there working and features provided by them. One difference between bean factory and application context is that former only instantiate bean when you call getBean() method while ApplicationContext instantiates Singleton bean when the container is started,  It doesn't wait for getBean to be called.
Daha anlaşılır bir açıklama şöyle. Yani BeanFactory lazy loading yapıyor
There are mainly two types of containers — BeanFactory and ApplicationContext.

For a BeanFactory container, when client asks for a yet initialized bean, or injects a yet initialized bean into another bean, the container will invoke createBean for instantiation. This process is lazy loading.

In contrast with BeanFactory, ApplicationContext goes with eager loading. For an ApplicationContext container, it will instantiate all beans when the container starts.

getBean metodu
Örnek
Şöyle yaparız
@Autowired
private BeanFactory beanFactory; ... // retrieve a single object (exception otherwise): Pet pet = beanFactory.getBean(Pet.class); // retrieve a provider: ObjectProvider<Pet> petProvider = beanFactory.getBeanProvider(Pet.class); // works only with bean name, not with @Qualifier Pet feline = beanFactory.getBean("feline", Pet.class); // works both with bean name and @Qualifier BeanFactoryAnnotationUtils.qualifiedBeanOfType(beanFactory, Pet.class, "feline");




Hiç yorum yok:

Yorum Gönder