The major difference between ApplicationContext and BeanFactory is that ApplicationContext interface provides pre-loading of beans, where BeanFactory provides lazy loading.
Note:-
Step1. Create SpringFile.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
<bean id="JobSeekerId" class="com.ps.model.JobSeeker" scope="singleton"></bean>
</beans>
Step2. Create POJO class
package com.ps.model;
public class JobSeeker {
private int jsId;
private String jsName;
public int getJsId() {
return jsId;
}
public void setJsId(int jsId) {
this.jsId = jsId;
}
public String getJsName() {
return jsName;
}
public void setJsName(String jsName) {
this.jsName = jsName;
}
public JobSeeker() {
System.out.println("********Object is creating******");
}
public void diplayJSInfo()
{
System.out.println("Job Seeker ID: "+this.getJsId()+" Job Seeker Name: "+this.getJsName());
}
}
Step3. Create main class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ps.model.JobSeeker;
public class SpringApp {
public static void main(String[] args) {
System.out.println("*****Before creating ApplicationContext Object******");
ApplicationContext appContext = new ClassPathXmlApplicationContext("SpringFile.xml"); //ApplicationContext object pre initialize and load all beans from springFile.xml
System.out.println("*****After creating ApplicationContext Object******");
JobSeeker jobSeekerObj=(JobSeeker)appContext.getBean("JobSeekerId");
jobSeekerObj.setJsId(100);
jobSeekerObj.setJsName("Heera");
jobSeekerObj.diplayJSInfo();
JobSeeker jobSeekerObj1=(JobSeeker)appContext.getBean("JobSeekerId");
jobSeekerObj1.diplayJSInfo();
}
}
Sample OUTPUT:
*****Before creating ApplicationContext Object******
********Object is Creating******
*****After creating ApplicationContext Object******
Job Seeker ID: 100 Job Seeker Name: Heera
Job Seeker ID: 100 Job Seeker Name: Heera
After using lazy-init=”true”, then springfile.xml will look like as below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
<bean id="JobSeekerId" class="com.ps.model.JobSeeker" scope="singleton" lazy-init="true" ></bean>
</beans>
Sample OUTPUT:
*****Before creating ApplicationContext Object******
*****After creating ApplicationContext Object******
********Object is Creating******
Job Seeker ID: 100 Job Seeker Name: Heera
Job Seeker ID: 100 Job Seeker Name: Heera
- When we use lazy-init=”true” in bean tag means that your are saying to spring container that you are not going to load this bean on application start time.
- Spring container forced by this tag and so not going to instantiate or create object for this bean on startup.
- Spring container will create the object only when requested by application that means when getbean(“beanname”) method will be called.
- There is another beanfactory interface known as BeanFactory that always perform lazy loading by default.
- It means there is no option of pre-loading of beans in Beanfactory.
- So that this is the major difference between applicationcontext and Beanfactory.
- ApplicationContext interface provides pre-loading of beans, where BeanFactory provides lazy loading.