(殘夢追月原創(chuàng),轉(zhuǎn)載請注明)
在jsp中,使用jsp標簽
如果設(shè)置為false,那么每當(dāng)其他bean依賴此bean時,容器則會重新實例化一個該bean對象,為其注入。
需要注意的是:??? 1、在下面的例子中,如果把computer1和computer2兩個受管bean都設(shè)置成單例bean,Spring IoC容器則分別實例化兩個bean,把它們作為兩個不同的bean對待,盡管他們的類相同。
??
2?<bean?id="computer2"class="ioc.test.Computer"?scope="singleton"/></bean>
2、一般來說,對于無狀態(tài)的bean使用單例模式,對于有狀態(tài)的bean使用prototype模式。
3、Spring IoC容器不會維護prototype類型的bean的整個聲明周期,容器在實例化、配置、注入之后就把它扔給調(diào)用者,然后就不管了。
4、如果一個單例bean computer引用了一個prototype類型的bean host,由于單例bean只初始化一次,所以不能保證每次調(diào)用computer時host都是最新的。解決辦法是使用lookup方法注入。
到了Spring2.0時代,scope屬性代替了原來的的singleton屬性,scope提供了更多的選項,從而可以更加靈活的配置bean的作用范圍。Spring2.0中,scope屬性有如下可能的取值,說明如下:1、 singleton,即單例bean,和1.x中singleton=”true”相同。
2、 prototype,同Spring1.x中的singleton=”false”。
?? 3、 request,這種bean在web的request范圍內(nèi)有效,即每次請求時都會產(chǎn)生一個實例。只用于web程序中。?? 4、 session,這種bean在web的session范圍內(nèi)有效。只用于web程序中。
?? 5、 global session,這種bean在web的全局session范圍內(nèi)有效。只用于web portlet框架中。
下面通過一個例子來說明單例bean和prototype bean的使用。在例子中,我們創(chuàng)建一個DateTime類,在其構(gòu)造方法中獲取當(dāng)前的系統(tǒng)時間,并存貯于date成員之中。然后利用該類定義兩個bean,一個為單例bean,一個為prototype bean。利用線程,兩次調(diào)用getBean方法從IoC容器中獲取這兩個bean的實例,并將存儲于其中時間打印出來。為了便于測試,在兩次調(diào)用getBean方法之間讓線程暫停小段時間。這樣,如果是單例bean,由于在容器中只是實例化一次,那么兩次調(diào)用顯示的時間應(yīng)當(dāng)相同,prototype則不一樣。通過其返回時間是否一支來查看受管bean是否重新被實例化。
?? 1、 新建一個java工程,為添加Spring開發(fā)能力后,建一個包ioc.test。
2、創(chuàng)建一個類DateTime,添加一Date類型的成員,并添加Geter方法。修改其構(gòu)造方法,讓其在構(gòu)造方法中獲取當(dāng)前系統(tǒng)時間,并存貯與date屬性中。代碼如下:
?2?
?3?import?java.util.Calendar;
?4?import?java.util.Date;
?5?
?6?public?class?DateTime?{
?7?????private?Date?date;
?8?????DateTime(){
?9?????????this.date?=?Calendar.getInstance().getTime();
10?????}
11?????public?Date?getDate()?{
12?????????return?date;
13?????}
14?}
15?
3、新建一Thread類的子類MyThread,重載run方法,在run方法中兩次調(diào)用getBean方法從容器獲取bean實例,然后分別將存貯與bean實例中的時間打印出來。代碼如下:
?2?import?org.springframework.context.ApplicationContext;
?3?public?class?MyThread?extends?Thread?{
?4?????
?5?????private?ApplicationContext?ac;
?6?????private?DateTime?dt;
?7?????private?String?bean;
?8?????MyThread(ApplicationContext?ac,String?bean){
?9?????????this.ac=ac;
10?????????this.bean=bean;
11?????}
12?????
13?????@Override
14?????public?void?run()?{????????
15?????????//第一次從容器取得bean
16?????????dt?=?(DateTime)?ac.getBean(bean);
17?????????System.out.println("Thread?Id:"?+?this.getId()+"?時間:"+dt.getDate());????????????????
18?????????
19?????????//線程暫停5秒
20?????????try?{
21?????????????sleep(1000?*?5);
22?????????}?catch?(InterruptedException?e)?{
23?????????}????????
24?????????//第二次從容器取得bean
25?????????dt?=?(DateTime)?ac.getBean(bean);
26?????????System.out.println("Thread?Id:"?+?this.getId()+"?時間:"+dt.getDate());????????????????????}
27?}
28?

2

3

4

5

6

5、編寫一測試類TestMain,代碼如下:
?2?
?3?import?org.springframework.context.ApplicationContext;
?4?import?org.springframework.context.support.ClassPathXmlApplicationContext;
?5?
?6?public?class?TestMain?{
?7?????public?static?void?main(String[]?args)?{
?8?????????ApplicationContext?ac?=?new?ClassPathXmlApplicationContext(
?9?????????????????"applicationContext.xml");
10?????????//測試單例bean
11?????????MyThread?mt1?=?new?MyThread(ac,"singletonDateTime");
12?????????mt1.start();
13?????????
14?????????//測試prototype?bean
15?????????MyThread?mt2?=?new?MyThread(ac,"prototypeDateTime");
16?????????mt2.start();
17?????}
18?}
19?
6、運行測試類,結(jié)果如下:

By:殘夢追月