本文主要描述的是關于spring中bean的命名方式,通過簡單實例向大家介紹了六種方式,具體如下。
一般情況下,在配置一個Bean時需要為其指定一個id屬性作為bean的名稱。id在IoC容器中必須是唯一的,此外id的命名需要滿足xml對id的命名規(guī)范。
在實際情況中,id命名約束并不會給我們帶來影響。但是如果用戶確實希望用到一些特殊字符來對bean進行命名,那么可以使用bean的name屬性來進行命名,name屬性沒有字符上的限制,幾乎可以使用任何字符。
每個Bean可以有一個或多個id,我們把第一個id稱為“標識符”,其余id叫做“別名”,這些id在IoC容器中必須唯一。
首先來介紹一下Beanid的命名規(guī)則:
1.遵循XML命名規(guī)范
2.由字母,數(shù)字,下劃線組成
3.駝峰式,首個單詞字母小寫,從第二個單詞開始首字母大寫。
接下來我們使用具體的例子來介紹Bean的不同命名方式
1.配置全限定類名,唯一
在示例中主要向大家輸出問候信息,我們需要一個HelloWorld接口以及一個名稱為HelloWorldImpl的實現(xiàn)類。接下來我們創(chuàng)建一個配置文件和一個程序的入口類。
首先在項目中創(chuàng)建包definition,接下來在包中創(chuàng)建HelloWorld接口:
1
2
3
|
public interface HelloWorld { public void sayHello(); } |
接下來我們創(chuàng)建HelloWorldImpl實現(xiàn)類:
1
2
3
4
5
|
public class HelloWorldImpl implements HelloWorld{ public void sayHello() { System.out.println( "Hello World" ); } } |
接下來我們在配置文件中為HelloWorldImpl進行Bean的命名:
1
|
< bean class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
我們在程序入口Mian.java來加載配置文件以及運行示例。
1
2
3
4
5
6
7
8
|
public static void sayHelloWorldByClass(){ //使用FileSystemXmlApplicationContext加載配置文件信息 BeanFactory beanFactory= new FileSystemXmlApplicationContext( "src/conf/conf-definition.xml" ); //獲取bean實例 HelloWorld helloWorld=beanFactory.getBean(HelloWorldImpl. class ); helloWorld.sayHello(); } |
在Main.java文件當中我們需要:
1.完成配置文件的加載以及SpringIoC容器的啟動
2.從容器中獲得HelloWorldImpl實現(xiàn)類的實例
3.輸出問候信息
2.指定id,唯一
在配置文件中對bean進行配置
1
|
< bean id = "HelloWorldById" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來調用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為標識符,唯一
在配置文件中對bean進行配置
1
|
< bean name = "HelloWorldByName" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來調用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為標識符,name為別名,唯一
在配置文件中對bean進行配置
1
2
|
< bean id = "HelloWorldById01" name = "HelloWorldByName01" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> |
修改Main程序入口,新建方法來調用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. 指定多個name,其中多個name需要用分號來進行分割,第一個name為標識符,其他的為別名,唯一
在配置文件中對bean進行配置
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程序入口,新建方法來調用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標簽來進行指定,唯一
在配置文件中對bean進行配置
1
2
3
|
< bean name = "bean3" class = "cn.lovepi.chapter03.definition.HelloWorldImpl" /> < alias name = "bean3" alias = "alias21" /> < alias name = "bean3" alias = "alias22" /> |
修改Main程序入口,新建方法來調用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(); } |
利用別名命名時得先有一個唯一的名稱(id和name都可以)
總結
以上就是本文關于Spring中Bean的命名方式代碼詳解的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/icarus_wang/article/details/51586692