(殘夢(mèng)追月原創(chuàng),轉(zhuǎn)載請(qǐng)注明)
在jsp中,使用jsp標(biāo)簽
如果設(shè)置為false,那么每當(dāng)其他bean依賴此bean時(shí),容器則會(huì)重新實(shí)例化一個(gè)該bean對(duì)象,為其注入。
需要注意的是:??? 1、在下面的例子中,如果把computer1和computer2兩個(gè)受管bean都設(shè)置成單例bean,Spring IoC容器則分別實(shí)例化兩個(gè)bean,把它們作為兩個(gè)不同的bean對(duì)待,盡管他們的類相同。
??
2?<bean?id="computer2"class="ioc.test.Computer"?scope="singleton"/></bean>
2、一般來(lái)說(shuō),對(duì)于無(wú)狀態(tài)的bean使用單例模式,對(duì)于有狀態(tài)的bean使用prototype模式。
3、Spring IoC容器不會(huì)維護(hù)prototype類型的bean的整個(gè)聲明周期,容器在實(shí)例化、配置、注入之后就把它扔給調(diào)用者,然后就不管了。
4、如果一個(gè)單例bean computer引用了一個(gè)prototype類型的bean host,由于單例bean只初始化一次,所以不能保證每次調(diào)用computer時(shí)host都是最新的。解決辦法是使用lookup方法注入。
到了Spring2.0時(shí)代,scope屬性代替了原來(lái)的的singleton屬性,scope提供了更多的選項(xiàng),從而可以更加靈活的配置bean的作用范圍。Spring2.0中,scope屬性有如下可能的取值,說(shuō)明如下:1、 singleton,即單例bean,和1.x中singleton=”true”相同。
2、 prototype,同Spring1.x中的singleton=”false”。
?? 3、 request,這種bean在web的request范圍內(nèi)有效,即每次請(qǐng)求時(shí)都會(huì)產(chǎn)生一個(gè)實(shí)例。只用于web程序中。?? 4、 session,這種bean在web的session范圍內(nèi)有效。只用于web程序中。
?? 5、 global session,這種bean在web的全局session范圍內(nèi)有效。只用于web portlet框架中。
下面通過(guò)一個(gè)例子來(lái)說(shuō)明單例bean和prototype bean的使用。在例子中,我們創(chuàng)建一個(gè)DateTime類,在其構(gòu)造方法中獲取當(dāng)前的系統(tǒng)時(shí)間,并存貯于date成員之中。然后利用該類定義兩個(gè)bean,一個(gè)為單例bean,一個(gè)為prototype bean。利用線程,兩次調(diào)用getBean方法從IoC容器中獲取這兩個(gè)bean的實(shí)例,并將存儲(chǔ)于其中時(shí)間打印出來(lái)。為了便于測(cè)試,在兩次調(diào)用getBean方法之間讓線程暫停小段時(shí)間。這樣,如果是單例bean,由于在容器中只是實(shí)例化一次,那么兩次調(diào)用顯示的時(shí)間應(yīng)當(dāng)相同,prototype則不一樣。通過(guò)其返回時(shí)間是否一支來(lái)查看受管bean是否重新被實(shí)例化。
?? 1、 新建一個(gè)java工程,為添加Spring開(kāi)發(fā)能力后,建一個(gè)包ioc.test。
2、創(chuàng)建一個(gè)類DateTime,添加一Date類型的成員,并添加Geter方法。修改其構(gòu)造方法,讓其在構(gòu)造方法中獲取當(dāng)前系統(tǒng)時(shí)間,并存貯與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實(shí)例,然后分別將存貯與bean實(shí)例中的時(shí)間打印出來(lái)。代碼如下:
?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()+"?時(shí)間:"+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()+"?時(shí)間:"+dt.getDate());????????????????????}
27?}
28?

2

3

4

5

6

5、編寫(xiě)一測(cè)試類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?????????//測(cè)試單例bean
11?????????MyThread?mt1?=?new?MyThread(ac,"singletonDateTime");
12?????????mt1.start();
13?????????
14?????????//測(cè)試prototype?bean
15?????????MyThread?mt2?=?new?MyThread(ac,"prototypeDateTime");
16?????????mt2.start();
17?????}
18?}
19?
6、運(yùn)行測(cè)試類,結(jié)果如下:

By:殘夢(mèng)追月