Generally Spring has 5type of scopes
- 1. SingleTon : Spring IOC make one bean context for container
- 2. Prototypes : Every request, Spring IOC make every new context
- 3. Request : For One Request, Spring IOC make one bean context
- 4. Session : For Session, Spring IOC make one bean context
- 5. GlobalSession : ...
First default Scope(Singleton)
Student class
package prototype; /** * @author jjhangu * */ public class Student { private String name = null; /** * @author jjhangu @create 2013. 4. 25. */ public String getName() { return name; } /** * @author jjhangu @create 2013. 4. 25. */ public void setName(String name) { this.name = name; } }Bus class
package prototype; import java.util.ArrayList; import java.util.List; /** * @author jjhangu * */ public class Bus { private ListMain classstudents = new ArrayList (); public void addStudent(Student student) { students.add(student); } public String getStudents() { String names = ""; for (final Student student : students) { names = names + "," + student.getName(); } return names; } }
package prototype; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author jjhangu * */ public class Main { public static void main(String args[]) { final ApplicationContext context = new ClassPathXmlApplicationContext("Prototype.xml"); final Student aaa = (Student) context.getBean("aaa"); final Student bbb = (Student) context.getBean("bbb"); final Student ccc = (Student) context.getBean("ccc"); final Bus bus1 = (Bus) context.getBean("bus"); bus1.addStudent(aaa); bus1.addStudent(bbb); System.out.println(" Bus 1 list : " + bus1.getStudents()); final Bus bus2 = (Bus) context.getBean("bus"); bus2.addStudent(ccc); System.out.println(" Bus 2 list : " + bus2.getStudents()); } }Prototype.xml class
aaa bbb ccc
Bus 1 list : ,aaa,bbb
Bus 2 list : ,aaa,bbb,ccc
Bus 2 list : ,aaa,bbb,ccc
Second Scope(prototype)
Prototype.xml class
aaa bbb ccc
Bus 1 list : ,aaa,bbb
Bus 2 list : ,ccc
Bus 2 list : ,ccc
No comments:
Post a Comment