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.
What’s the difference between and
please explain
Thanks Shailendra !!
I have already done this for Spring MVC and Spring Security related posts where i have given an option to download the entire project.
But for core concepts , i have not done this, I will do it for sure, Please give sometime.
Thanks for alarming on this!!
Sir your website is very nice but some problem
You are not mension jar files ig we run your app how to know which jar files are support yor app
pls sir mention jar files i’m waiting……………….
Never understood the Spring concepts so clearly from any other tutorial.Great Work!!
Thank you so much Shilpa !! , Happy learning 🙂
Difference between component-scan and annotation-config in Spring has to more to do with you want to auto wire the beans or also auto discover the beans. Using context:component-scan will not only auto wire the beans but also auto discover the beans.
http://netjs.blogspot.com/2016/04/difference-between-component-scan-and-annotation-config-spring.html
Yes, You are absolutely right , thats the gist of my below article
http://javainsimpleway.com/contextcomponent-scan-vs-contextannotation-config-2/
Thank you !!
HI, karibasappagc.
I agree with basavaraj.
very clear and informative tutorial. I enjoy reading it last 2 days 🙂
Great Thanks!
Thank you Artem, Happy learning …
Cool…Thanks Man..happy learning..
i did not seen any tutorial like this, i really thank full for sharing your valuable knowledge in spring with good and valid examples.
i have plan to learn spring long back, i search many tutorials there i find only theory kind of stuffs, i find your java guide this is very useful and now i feel happy. i proudly say i learned spring.
thank you karibasappa sir