08-02-2023, 03:41 PM
In a real-time scenario, the difference between the Spring IOC Core container (BeanFactory) and Advanced J2EE container (ApplicationContext) are as follows.
1. BeanFactory will create objects for the beans (i.e., for POJO classes) mentioned in the spring.xml file (`<bean></bean>`) only when you call the .getBean() method, but whereas ApplicationContext creates the objects for all the beans (`<bean></bean>` if its scope is not explicitly mentioned as "Prototype") configured in the spring.xml while loading the spring.xml file itself.
2. BeanFactory: (Lazy container because it creates the objects for the beans only when you explicitly call from the user/main class)
/*
* Using core Container - Lazy container - Because it creates the bean objects On-Demand
*/
//creating a resource
Resource r = (Resource) new ClassPathResource("com.spring.resources/spring.xml");
//creating BeanFactory
BeanFactory factory=new XmlBeanFactory®;
//Getting the bean for the POJO class "HelloWorld.java"
HelloWorld worldObj1 = (HelloWorld) factory.getBean("test");
ApplicationContext: (Eager container because of creating the objects of all singleton beans while loading the spring.xml file itself)
ApplicationContext context = new ClassPathXmlApplicationContext("com/ioc/constructorDI/resources/spring.xml");
3. Technically, using ApplicationContext is recommended because in real-time applications, the bean objects will be created while the application is getting started in the server itself. This reduces the response time for the user request as the objects are already available to respond.
1. BeanFactory will create objects for the beans (i.e., for POJO classes) mentioned in the spring.xml file (`<bean></bean>`) only when you call the .getBean() method, but whereas ApplicationContext creates the objects for all the beans (`<bean></bean>` if its scope is not explicitly mentioned as "Prototype") configured in the spring.xml while loading the spring.xml file itself.
2. BeanFactory: (Lazy container because it creates the objects for the beans only when you explicitly call from the user/main class)
/*
* Using core Container - Lazy container - Because it creates the bean objects On-Demand
*/
//creating a resource
Resource r = (Resource) new ClassPathResource("com.spring.resources/spring.xml");
//creating BeanFactory
BeanFactory factory=new XmlBeanFactory®;
//Getting the bean for the POJO class "HelloWorld.java"
HelloWorld worldObj1 = (HelloWorld) factory.getBean("test");
ApplicationContext: (Eager container because of creating the objects of all singleton beans while loading the spring.xml file itself)
ApplicationContext context = new ClassPathXmlApplicationContext("com/ioc/constructorDI/resources/spring.xml");
3. Technically, using ApplicationContext is recommended because in real-time applications, the bean objects will be created while the application is getting started in the server itself. This reduces the response time for the user request as the objects are already available to respond.