Search code examples
javaspringjavabeansapplicationcontextspring-ioc

BeanFactory Vs ApplicationContext in concern of BeanPostProcessor And BeanFactoryPostProcessor


I am now learning little bit detail about Spring IoC Containers with the difference between BeanFactory Vs Application Context. I was also referring the previous stack overflow discussion and tutorial point from the following links,

BeanFactory vs ApplicationContext

https://www.tutorialspoint.com/spring/spring_ioc_containers.htm

In my learning I found that ApplicationContext is capable of resolving textual messages from a properties file and the ability to publish application events to interested event listeners

And BeanPostProcessor Registration and BeanFactoryPostProcessor Registration doing manually in BeanFactory and automatically doing in application context,

So my confusions are,

  1. Since BeanFactory Manually doing BeanPostProcessor registration, Does that mean the developer is doing something while bean is getting initiating ? Because, the same process ApplicationContext does automaticall according to documentations

  2. What is the purpose of BeanFactoryPostProcessor registration ? What is the difference between BeanPostProcessor and BeanFactoryPostProcessor ?

  3. If we say ApplicationContext has the ability to resolving text messages from properties file (Message source access), So for what purpose application context is communicating with property file ?

How can I better understand this concept ?


Solution

    1. BeanFactory is lightweight and it doesn't load in-memory/initialize any bean from application.xml (even singleton beans) unless factory.getBean("bean-name") method is called; whereas ApplicationContext is heavyweight is loads everything on container startup. BeanFactory also supports limited scopes like proto and singleton and features like AOP doesn't work with BeanFactory. So to answer your 1st question YES while using BeanFactory just loading the xml and creating BeanFactory object does not guarantee any bean is initialized; only place to use it is when you are concerned of runtime memory consumption Note: ApplicationContext extends ListableBeanFactory extends BeanFactory.

    2. BeanPostProcessor as the name suggest gives you hooks to work on an instance of a bean created by the spring container by ApplciationContext/BeanFactory implementation (i.e. before and after initialization) Spring AOPs like transactions. caching etc. ref @Required for example. BeanFactoryPostProcessor on the other end lets you modify the actual bean definition before initialization of the instance. The PropertyResourceConfigurer and PropertyPlaceholderConfigurer are two good examples refer PropertyResourceConfigurer example, how properties are changed before initializing the actual beans.

    3. ApplicationContext main responsibility is initialization of bean, now based on the individual bean is defined it provides way to perform specialized tasks like resources bundle loading/property configurer. I suggest you look at the javadoc for a concrete implementation of ApplicationContext like GenericApplicationContext . If you look at how each methods are implemented you will understand how the control flows from AppContext to individual beans before and after they are initialized. AppContext only has the contract for supports tasks