27 Ağustos 2020 Perşembe

SpringContext BeanFactoryPostProcessor Arayüzü - Extension Point. Henüz Daha Hiç Bir Bean Yaratılmadan Çalışır.

Giriş
Henüz daha hiç bir bean yaratılmadan çalışır. Şeklen şöyle



postProcessBeanFactory metodu
Açıklaması şöyle. Parametre olarak geçilen BeanFactory yani "bean container" yaratıldıktan sonra bir bean'in özelliğini değiştirmek için kullanılır.
BeanFactoryPostProcessor is called when bean definitions are loaded but beans itself are not created. Great example is PropertyPlaceholderConfigurer which replaces placeholders with concrete values.
Açıklaması şöyle.
BeanFactoryPostProcessor is one of the two ways Spring provides a mechanism to modify the bean definition/instances before making them available to the application (the other is using BeanPostProcessors)

You can absolutely use BeanFactoryPostProcessors to add/modify bean definitions,...
Örnek
Eski Spring'de bean'leri lazy yapmak için şöyle yapalır
public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
      BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
      definition.setLazyInit(true);
    }
  }
}

postProcessBeanFactory İçinde Bir Başka Bean Yaratma
Açıklaması şöyle.
A BeanFactoryPostProcessor may interact with and modify bean definitions, but never bean instances. Doing so may cause premature bean instantiation, violating the container and causing unintended side-effects. If bean instance interaction is required, consider implementing BeanPostProcessor instead.
Bu açıklamaya rağmen Factory Bean'lere çeşitli ayarlar ve atamalar yapıldığında bir sorun çıkmadı. Ancak bu yöntem yine de tehlikeli bu yüzden FactoryBean arayüzünü PriorityOrdered olarak gerçekleştirmek daha kolay olabilir.

Örnek
Bur örnekte bean yaratılmadığı halde getBean() çağrısı ile bean'in yartılması da sağlanıyor. Yani yukarıdaki açıklmaa ile tezat. Ancak problem çıkmıyor. Şöyle yaparız.
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
  throws BeansException {
    ServiceConfiguration appConfig = beanFactory.getBean("appConfig",
      ServiceConfiguration.class);
    appConfig.setFoo("foo");
  }
});
Örnek
Şöyle yaparız.
//create IOC Container
ApplicationContext ctx=new FileSystemXmlApplicationContext("...");
//get PropertyPlaaceHolderConfigurer OBJ
BeanFactoryPostProcessor bfpp=ctx.getBean("ppc",PropertyPlaceholderConfigurer.class);
bfpp.postProcessBeanFactory((ConfigurableListableBeanFactory)ctx);
registerBeanDefinition
Açıklaması şöyle. Yeni bir bean definition yaratmak için kullanılır. InstantiationAwareBeanPostProcessorAdapter sınıfı gibidir.
You can use the BeanFactoryPostProcessor to define your beans dynamically


Hiç yorum yok:

Yorum Gönder