本文主要描述的是關(guān)于spring中bean的命名方式,通過(guò)簡(jiǎn)單實(shí)例向大家介紹了六種方式,具體如下。
一般情況下,在配置一個(gè)Bean時(shí)需要為其指定一個(gè)id屬性作為bean的名稱。id在IoC容器中必須是唯一的,此外id的命名需要滿足xml對(duì)id的命名規(guī)范。
在實(shí)際情況中,id命名約束并不會(huì)給我們帶來(lái)影響。但是如果用戶確實(shí)希望用到一些特殊字符來(lái)對(duì)bean進(jìn)行命名,那么可以使用bean的name屬性來(lái)進(jìn)行命名,name屬性沒(méi)有字符上的限制,幾乎可以使用任何字符。
每個(gè)Bean可以有一個(gè)或多個(gè)id,我們把第一個(gè)id稱為“標(biāo)識(shí)符”,其余id叫做“別名”,這些id在IoC容器中必須唯一。
首先來(lái)介紹一下Beanid的命名規(guī)則:
1.遵循XML命名規(guī)范
2.由字母,數(shù)字,下劃線組成
3.駝峰式,首個(gè)單詞字母小寫,從第二個(gè)單詞開(kāi)始首字母大寫。
接下來(lái)我們使用具體的例子來(lái)介紹Bean的不同命名方式
1.配置全限定類名,唯一
在示例中主要向大家輸出問(wèn)候信息,我們需要一個(gè)HelloWorld接口以及一個(gè)名稱為HelloWorldImpl的實(shí)現(xiàn)類。接下來(lái)我們創(chuàng)建一個(gè)配置文件和一個(gè)程序的入口類。
首先在項(xiàng)目中創(chuàng)建包definition,接下來(lái)在包中創(chuàng)建HelloWorld接口:
1
2
3
|
public interface HelloWorld { public void sayHello(); } |
接下來(lái)我們創(chuàng)建HelloWorldImpl實(shí)現(xiàn)類:
1
2
3
4
5
|
public class HelloWorldImpl implements HelloWorld{ public void sayHello() { System.out.println( "Hello World" ); } } |
接下來(lái)我們?cè)谂渲梦募袨镠elloWorldImpl進(jìn)行Bean的命名:
1
|
< bean class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
我們?cè)诔绦蛉肟贛ian.java來(lái)加載配置文件以及運(yùn)行示例。
1
2
3
4
5
6
7
8
|
public static void sayHelloWorldByClass(){ //使用FileSystemXmlApplicationContext加載配置文件信息 BeanFactory beanFactory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); //獲取bean實(shí)例 HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl. class ); helloWorld.sayHello(); } |
在Main.java文件當(dāng)中我們需要:
1.完成配置文件的加載以及SpringIoC容器的啟動(dòng)
2.從容器中獲得HelloWorldImpl實(shí)現(xiàn)類的實(shí)例
3.輸出問(wèn)候信息
2.指定id,唯一
在配置文件中對(duì)bean進(jìn)行配置
1
|
< bean id = "HelloWorldById" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來(lái)調(diào)用bean
1
2
3
4
5
6
|
public static void sayHelloWorldById(){ BeanFactory factory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); HelloWorld helloWorld=factory.getBean( "HelloWorldById" ,HelloWorldImpl. class ); helloWorld.sayHello(); } |
3. 指定name,name為標(biāo)識(shí)符,唯一
在配置文件中對(duì)bean進(jìn)行配置
1
|
< bean name = "HelloWorldByName" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來(lái)調(diào)用bean
1
2
3
4
5
6
|
public static void sayHelloWorldByName(){ BeanFactory factory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); HelloWorld helloWorld=factory.getBean( "HelloWorldByName" ,HelloWorldImpl. class ); helloWorld.sayHello(); } |
4.指定id和name,其中id為標(biāo)識(shí)符,name為別名,唯一
在配置文件中對(duì)bean進(jìn)行配置
1
2
|
< bean id = "HelloWorldById01" name = "HelloWorldByName01" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來(lái)調(diào)用bean
1
2
3
4
5
6
7
8
|
public static void sayHelloWorldByNameAndId(){ BeanFactory factory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); HelloWorld helloWorld01=factory.getBean( "HelloWorldById01" ,HelloWorldImpl. class ); HelloWorld helloWorld02=factory.getBean( "HelloWorldByName01" ,HelloWorldImpl. class ); helloWorld01.sayHello(); helloWorld02.sayHello(); } |
5. 指定多個(gè)name,其中多個(gè)name需要用分號(hào)來(lái)進(jìn)行分割,第一個(gè)name為標(biāo)識(shí)符,其他的為別名,唯一
在配置文件中對(duì)bean進(jìn)行配置
1
2
3
4
|
< bean name = "bean1;alias01;alias02;alias03" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> < bean id = "bean2" name = "alias11;alias12;alias13" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來(lái)調(diào)用bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public static void sayHelloWorldByMutilName(){ BeanFactory factory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); HelloWorld helloWorld1=factory.getBean( "bean1" ,HelloWorldImpl. class ); HelloWorld helloWorld01=factory.getBean( "alias01" ,HelloWorldImpl. class ); HelloWorld helloWorld02=factory.getBean( "alias02" ,HelloWorldImpl. class ); HelloWorld helloWorld03=factory.getBean( "alias03" ,HelloWorldImpl. class ); helloWorld1.sayHello(); helloWorld01.sayHello(); helloWorld02.sayHello(); helloWorld03.sayHello(); HelloWorld helloWorld2=factory.getBean( "bean2" ,HelloWorldImpl. class ); HelloWorld helloWorld11=factory.getBean( "alias11" ,HelloWorldImpl. class ); HelloWorld helloWorld12=factory.getBean( "alias12" ,HelloWorldImpl. class ); HelloWorld helloWorld13=factory.getBean( "alias13" ,HelloWorldImpl. class ); helloWorld2.sayHello(); helloWorld11.sayHello(); helloWorld12.sayHello(); helloWorld13.sayHello(); } |
6. 指定別名,使用alias標(biāo)簽來(lái)進(jìn)行指定,唯一
在配置文件中對(duì)bean進(jìn)行配置
1
2
3
|
< bean name = "bean3" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> < alias name = "bean3" alias = "alias21" /> < alias name = "bean3" alias = "alias22" /> |
修改Main程序入口,新建方法來(lái)調(diào)用bean
1
2
3
4
5
6
7
8
9
10
11
|
public static void sayHelloWorldByAlias(){ BeanFactory factory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); HelloWorld helloWorld01=factory.getBean( "bean3" ,HelloWorldImpl. class ); HelloWorld helloWorld02=factory.getBean( "alias21" ,HelloWorldImpl. class ); HelloWorld helloWorld03=factory.getBean( "alias22" ,HelloWorldImpl. class ); helloWorld01.sayHello(); helloWorld02.sayHello(); helloWorld03.sayHello(); } |
利用別名命名時(shí)得先有一個(gè)唯一的名稱(id和name都可以)
總結(jié)
以上就是本文關(guān)于Spring中Bean的命名方式代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/icarus_wang/article/details/51586692