Spring 의 scope prototype 과 simpleton 의 차이

5186 단어 prototype
Spring 의 scope prototype 과 simpleton 의 차이
우선 applicationContext. 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"
             xmlns:p="http://www.springframework.org/schema/p"
             xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans           
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop      
            http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="scopeSample" class="Spring.t02.ScopeSample" init-method="doInit" 
scope="prototype">
<!--
<bean id="scopeSample" class="Spring.t02.ScopeSample" init-method="doInit" 
scope="simpleton(       ,    simpleton)">
-->
   <constructor-arg value="first.."></constructor-arg>
    <constructor-arg value="second.."></constructor-arg>
</bean>
<bean id="scopeSample2" class="Spring.t02.ScopeSample2" init-method="doInit">
<property name="name" value="xuhui"></property>

</bean>
</beans>

ScopeSample. java 에서:
package Spring.t02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeSample {
private String first;
private String second;
public String getFirst() {
   return first;
}
public void setFirst(String first) {
   this.first = first;
}
public String getSecond() {
   return second;
}
public void setSecond(String second) {
   this.second = second;
}
public ScopeSample(String first, String second) {
   System.out.println(" two arguments constructor.....");
   this.first = first;
   this.second = second;
}
public ScopeSample() {
   System.out.println(" no argument constructor.....");
 
}
public ScopeSample(String first) {
 
   this.first = first;
   System.out.println(" one argument constructor.....");
}
public void doInit(){
   System.out.println("init .....");
}
public void doDestroy(){
   System.out.println(" destroy.....");
}
   public static void main(String[]args){
    ApplicationContext ac=new
ClassPathXmlApplicationContext("applicationContext.xml");
      ScopeSample s1=(ScopeSample)ac.getBean("scopeSample");
      ScopeSample s2 = (ScopeSample) ac. getBean ("scopeSample"); / sope 가 prototype 일 때 두 번 의 구조 방법 을 호출 합 니 다.
      System.out.println("s1==s2?"+(s1==s2));
   
   }
}
Scope Sample2. java 에서:
package Spring.t02;
public class ScopeSample2 {
private String name="";
public void setName(String name) {
   this.name = name;
}
public ScopeSample2(String name) {
   super();
   this.name = name;
   System.out.println(" ScopeSample2 one arg construtctor .... ");
}
public ScopeSample2() {
 
   super();
   System.out.println(" ScopeSample2 no arg construtctor .... ");
   // TODO Auto-generated constructor stub
}
public void doInit(){
   System.out.println(this.getClass()+" init....");
}
}
scope 형식 이 simpleton 일 때:
ScopeSample. java 출력 결과 실행:
two arguments constructor.....
init .....
ScopeSample2 no arg construtctor ....
class Spring.t02.ScopeSample2 init....
s1==s2?true
scope 형식 이 prototype 형식 일 때 실행 결 과 는:
ScopeSample2 no arg construtctor ....
class Spring.t02.ScopeSample2 init....
two arguments constructor.....
init .....
two arguments constructor.....
init .....
s1==s2?false
이 두 번 의 운행 결 과 는 비교적 요약 할 수 있다.
scope 가 simpleton 일 때, 그러면 Spring.
IOC 용기 에는 공 유 된 bean 인 스 턴 스 만 존재 하 며, bean 에 대한 모든 요청 은 id 가 이 bean 정의 와 일치 하면 bean 의 같은 인 스 턴 스 만 되 돌려 줍 니 다. 다시 말 하면 하나의 bean 을 singleton 역할 영역 으로 정의 할 때 Spring
IOC 용 기 는 이 bean 이 정의 하 는 유일한 인 스 턴 스 만 만 만 들 수 있 습 니 다.
그래서 ScopeSmaple. java 에서 getBean 을 두 번 호출 했 지만 구조 방법 과 init 방법 만 호출 했 습 니 다.
scope 가 prototype 일 때 Bean 을 요청 할 때마다 (즉, geiBean 방법 을 호출 할 때) 대상 (즉 구조 방법 을 호출 하 는 것) 을 만 들 고 init 방법 을 호출 합 니 다.
둘 의 차 이 는 또 하나 있다.
위의 두 예 를 보면 출력 순서 도 다 릅 니 다. applicationContext. xml 파일 을 불 러 올 때 주의 하 십시오.
scope = simpleton (기본 상황 에서) 을 요청 하지 않 고 bean 을 얻 으 면 구조 방법 으로 대상 을 만 듭 니 다.
하지만 scope = prototype 에서 bean 을 요청 할 때 만 대상 을 만 듭 니 다.
본문 전재:
http://hi.baidu.com/zhuyuli521/blog/item/fb61990e31c310206059f311.html

좋은 웹페이지 즐겨찾기