???
初探Spring程序,程序如下:
?1?
BeanNaming.java
?2?????package
?spring.beantest;
?3?
?4?????import
?org.springframework.beans.factory.BeanFactory;
?5?????import
?org.springframework.beans.factory.xml.XmlBeanFactory;
?6?????import
?org.springframework.core.io.FileSystemResource;
?7?
?8?????public?class
?BeanNaming?{
?9?????public?static?void
?main(String?[]args)
10?
????{
11?????????BeanFactory?factory?=?new?XmlBeanFactory(new?FileSystemResource("src/applicationContext.xml"
));
12?
????
13?????????String?s1?=?(String)factory.getBean("name1"
);
14?????????String?s2?=?(String)factory.getBean("name2"
);
15?????????String?s3?=?(String)factory.getBean("name3"
);
16?????????String?s4?=?(String)factory.getBean("name4"
);
17?
????????
18?????????System.out.println((s1?==
?s2));
19?????????System.out.println((s2?==
?s3));
20?????????System.out.println((s3?==
?s4));
21?
????????
22?????????String[]?x?=?factory.getAliases("name3"
);
23?????????for
(String?str:x)
24?
????????{
25?
????????????System.out.println(str);
26?
????????}
27?
????????
28?
??????}
29?
????}
30?
?
31?
??????applicationContext.xml
32??????<?xml?version="1.0"?encoding="UTF-8"?>
33??????<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">
34??????<beans>
35??????<!--?aliasing?examples?-->
36??????<bean?id="name1"?name="name2,name3,name4"?class="java.lang.String"/>
37??????</beans>
???
輸出結(jié)果為:
??? true
??? true
??? true
??? name1
??? name4
??? name2
???
???
可以看出,使用中id屬性和name屬性幾乎沒(méi)有任何區(qū)別。調(diào)用beanfactory.getAliases(string)的方法時(shí),傳入的參數(shù)可以是任意一個(gè)bean名字,輸出的別名則是除去作為參數(shù)本身之外的所有 bean名。
??
?? 另:在寫(xiě) applicationContxt.xml文件時(shí),如果沒(méi)有定義<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">,則會(huì)拋出Cannot find the declaration of element 'beans'異常。
??
?? ???
以上使用BeanFactory對(duì)配置文件進(jìn)行加載,BeanFactory,是根據(jù)配置文件負(fù)責(zé)創(chuàng)建Bean的實(shí)例,并負(fù)責(zé)Bean的生命周期的管理- --,包括Bean的生成與銷(xiāo)毀、Bean的創(chuàng)建方式的識(shí)別(是否為singleton)、Bean的各個(gè)屬性的設(shè)定、依賴(lài)關(guān)系的建立等。
??? ApplicationContext接口,提供了國(guó)際化、事件處理及beans在context中的自查能力。它也可創(chuàng)建具有層次結(jié)構(gòu)context環(huán)境,將bean的作用域和可訪問(wèn)區(qū)域限制在應(yīng)用程序的一個(gè)特定部分中。下面演示如何使用ApplicationContext接口。
???
?1?<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-????beans.dtd">
?2?????<beans>
?3?????<bean?id="bean_1"?class="BeanTest"/>
?4?????<bean?id="bean_2"?class="BeanTest"/>
?5?????</beans>
?6?
?7?
?8?????public?class
?BeanTest?{
?9?????public?void
?test()?{
10?????????System.out.println("test.."
);
11?
????}
12?
???}
13?
14?
15????import
?org.springframework.context.ApplicationContext;
16????import
?org.springframework.context.support.FileSystemXmlApplicationContext;
17?
18????public?class
?BeanNameExample?{
19?
20?????public?static?void
?main(String[]?args)?{
21?????????ApplicationContext?ctx?=?new
?FileSystemXmlApplicationContext(
22?????????"build/applicationContext.xml"
);
23?
????????
24?????????BeanTest?beanOne?=?(BeanTest)ctx.getBean("bean_1"
);
25?????????BeanTest?beanTwo?=?(BeanTest)ctx.getBean("bean_2"
);
26?
????????
27?
????????beanOne.test();
28?
????????beanTwo.test();
29?
??????}
30?
???}
31?
??
綜上,ApplicationContext與BeanFactory的不同點(diǎn)
?? BeanFactory提供了針對(duì)JavaBean的管理功能,而ApplicationContext提供了一個(gè)更為框架化的實(shí)現(xiàn)(從上面的示例中可以看出,BeanFactory的使用方式更加類(lèi)似一個(gè)API,而非Framework style)。ApplicationContext覆蓋了BeanFactory的所有功能,并提供了更多的特性。此外, ApplicationContext為與現(xiàn)有應(yīng)用框架相整合,提供了更為開(kāi)放式的實(shí)現(xiàn)(如對(duì)于Web應(yīng)用,我們可以在web.xml中對(duì) ApplicationContext進(jìn)行配置)。
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭(zhēng)朝夕
???? 隱姓埋名/低調(diào)華麗/簡(jiǎn)單生活/完美人生