IOC Container
Spring framework comes up with its own container whose job is to manage the beans life cycle, associate the dependent beans together (which is called wiring).
The container in spring is provided in the form of interfaces namely
1) BeanFactory
2) ApplicationContext
These 2 interfaces provided by spring acts as Containers, we can use any one of these containers based on the requirement.
Spring container uses the XML configuration to create the bean and wire among them.
Once the container loads the XML configuration, it will instantiate the beans and wire among the dependent beans and makes it ready for the application classes to use it.
For every bean defined in the XML configuration, there should be a corresponding java class.
So as shown below, Container reads the XML configuration and Java classes, then it instantiate and wires it if required and provide the readily available object to the application.
Using Bean Factory container
Since BeanFactory is an interface, we should use its implementation class to use the container features.
XmlBeanFactory is the implementation class for BeanFactory and it can be used as below
- Resource resource=new ClassPathResource("beans.xml");
- BeanFactory factory=new XmlBeanFactory(resource);
Resource resource=new ClassPathResource("beans.xml"); BeanFactory factory=new XmlBeanFactory(resource);
We need to pass Resource object to XmlBeanFactory’s constructor,so we have created the ClassPathResource object first by passing the spring configuration file name and then passed it to the XmlBeanFactory’s constructor.
Now Spring container reads the beans.xml file and corresponding java classes and instantiate the beans and manages its entire life cycle.
Using ApplicationContext container
Since ApplicationContext is an interface, we should use its implementation class to use the container features.
ClassPathXmlApplicationContext is the implementation class for ApplicationContext and it can be used as below
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
We need to pass the string value to the ClassPathXmlApplicationContext constructor and it should be the spring configuration file name, in this case it is beans.xml.
Now Spring container reads the beans.xml file and corresponding java classes and instantiate the beans and manage its entire life cycle.
So both the containers are performing the same thing i.e. managing the beans life cycle.
Then what we should use ?
BeanFactory vs ApplicationContext
org.springframework.beans.factory.BeanFactory and the org.springframework.context.ApplicationContext
Both are spring containers and both are configured using XML configuration, both will perform the basic container functionality.
However ApplicationContext interface extends BeanFactory interface which means ApplicationContext will have some additional features along with all the features provided by BeanFactory.
Those additional features are as below
Internationalization
BeanFactory does not support internationalization (i18N) but ApplicationContext support it.
Event Publishing
ApplicationContext provides the facility to publish the events for the beans that are registered as listeners.
Spring AOP support
It provides integration with spring AOP
Since ApplicationContext has many advanced features along with all the features provided by BeanFactory, it is advisable to use ApplicationContext than BeanFactory.
BeanFactory is good to use if we don’t want to use any of the additional features provided by ApplicationContext.
Very easy to understand , and i was able to explain clearly in interviews after visiting this website . Thanks Raj for this website and tutorials that make us understand in simple way .
this is the amazing website to learn spring to beginners.
nice explanation and very easy to understand.
Love your tutorials!! So easy to understand with step by step example.
Hi sir..
First of All I would like to thank you…you are really doing great work by sharing java course. It is really very helpful… Sir do you have any channel in YouTube….!!
Hi,
Thank you very much !!
Currently I don’t have YouTube channel, planning for it.
Hi Karibasapp,
In all your tutorials, you have given explanations in simple words with good examples. Very nice!
Are you planning to publish for Spring integration tutorials?
Thanks,
Prem
Thank you Prem!
I have the plan for all the Spring modules including integration, please give some time to reach that.