bean標籤scope屬性
阿新 • • 發佈:2021-01-22
- scope=“singleton” 表示單例,同一個bean只能獲取同一個物件
- scope="prototype"表示多例,同一個bean,獲取的是不同的物件
public class People {
private String name;
}
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml" );
People peo=ac.getBean("people",People.class);
People peo2=ac.getBean("people",People.class);
System.out.println(peo);
System.out.println(peo2);
}
}
單例測試
<bean id="people" class="cn.wit.test.People" scope="singleton"></bean>
多例測試
<bean id="people" class="cn.wit.test.People" scope="prototype"></bean>