Spring中Bean的命名方式代碼詳解
本文主要描述的是關于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接口:
public interface HelloWorld { public void sayHello(); }
接下來我們創(chuàng)建HelloWorldImpl實現(xiàn)類:
public class HelloWorldImpl implements HelloWorld{ public void sayHello() { System.out.println("Hello World"); } }
接下來我們在配置文件中為HelloWorldImpl進行Bean的命名:
<bean class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
我們在程序入口Mian.java來加載配置文件以及運行示例。
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進行配置
<bean id="HelloWorldById" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法來調用bean
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進行配置
<bean name="HelloWorldByName" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法來調用bean
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進行配置
<bean id="HelloWorldById01" name="HelloWorldByName01" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/>
修改Main程序入口,新建方法來調用bean
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進行配置
<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
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進行配置
<bean name="bean3" class="cn.lovepi.chapter03.definition.HelloWorldImpl"/> <alias name="bean3" alias="alias21"/> <alias name="bean3" alias="alias22"/>
修改Main程序入口,新建方法來調用bean
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的命名方式代碼詳解的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
《Spring工廠方法創(chuàng)建(實例化)bean實例代碼》
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
使用Idea maven創(chuàng)建Spring項目過程圖解
這篇文章主要介紹了使用Idea maven創(chuàng)建Spring項目過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02Spring Boot Thymeleaf實現(xiàn)國際化的方法詳解
這篇文章主要給大家介紹了關于Spring Boot Thymeleaf實現(xiàn)國際化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-10-10如何在SpringBoot中使用Spring-AOP實現(xiàn)接口鑒權
這篇文章主要介紹了如何在SpringBoot中使用Spring-AOP實現(xiàn)接口鑒權,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-09-09ArrayList與linkedList的用法區(qū)別及擴容方式
這篇文章主要介紹了ArrayList與linkedList的用法區(qū)別及擴容方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03