- Singleton
- Prototype
- Global
- Session
- Application
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<bean id="student" class="com.studentact.model.Student" scope="prototype" destroy-method="destroy" init-method="init"/>
<bean id="studentDao" class="com.studentact.dao.StudentDaoImpl" destroy-method="destroy" init-method="init">
<property name="student" ref="student"/>
</bean>
<bean id="studentServiceProxy" class="com.studentact.proxy.StudentServiceProxy" init-method="init" destroy-method="destroy">
<property name="student" ref="student"/>
</bean>
<bean id="studentService" class="com.studentact.service.StudentService" destroy-method="destroy" init-method="init">
<property name="studentServiceProxy" ref="studentServiceProxy"/>
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
We can see that the scope of student bean is specified as prototype and other beans don’t have this scope attribute which means they are all singleton.
Let’s say StudentServiceProxy’s method getVOCTopStudent gets student data from some web service and assign them in student object. Likewise StudentDaoImpl’s method getGovtStudent gets the from DB and assign them in student object. StudentService will call these methods and store the return student object in a list as below,
public class StudentService implements IStudentService {
private IStudentDao studentDao;
private IStudentServiceProxy studentServiceProxy;
public IStudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
public IStudentServiceProxy getStudentServiceProxy() {
return studentServiceProxy;
}
public void setStudentServiceProxy(IStudentServiceProxy studentServiceProxy) {
this.studentServiceProxy = studentServiceProxy;
}
@Override
public List<Student> getTopStudents() {
List<Student> topStudents = new ArrayList<Student>();
topStudents.add(studentDao.getGovtStudent());
topStudents.add(studentServiceProxy.getVOCTopStudent());
return topStudents;
}
public void destroy(){
System.out.println("detroy method of StudentService");
}
public void init(){
System.out.println("init method of StudentService");
}
//Other operations go here
}
We can use the below code to test whether the list has same student object or different objects.
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
List<Student> students = studentService.getTopStudents();
System.out.println(students);
Student aStudent = null;
for(Student student:students){
if(null == aStudent){
aStudent = student;
} else if(student == aStudent) {
Assert.assertFalse(true);
}
}
We mentioned student bean scope as prototype and injected it in other beans like studentDao and studentServiceProxy. So the resultant list will have different student objects. If it’s a singleton, then both studentDao and studentServiceProxy will have the same object and the resultant list will have the same student objects.
public class StudentServiceTest extends BaseTest {
protected AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
@Test
public void testGetTopStudents() {
applicationContext.registerShutdownHook();
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
List<Student> students = studentService.getTopStudents();
System.out.println(students);
Student aStudent = null;
for(Student student:students){
if(null == aStudent){
aStudent = student;
} else if(student == aStudent) {
Assert.assertFalse(true);
}
}
}
}