Spring Xml裝配Bean的思路詳解
1,概述
在Spring中提供了三種方式來對Bean進行配置:
在xml文件中配置在Java的接口和實現(xiàn)類中配置隱式Bean的發(fā)現(xiàn)機制和自動裝配原則
這三種方式都經(jīng)常用到,而且常常會混合使用。這篇先寫xml裝配Bean。
2,分析bean標簽
<bean id="pserson" class="com.xl.spring.xlIoc.beans.Person"> <property name="id" value="1"/> <property name="name" value="宋智孝"/> <property name="nickName" value="懵懵"/> </bean>
1.id:為bean取一個全局唯一的名字。該屬性可選,如果沒有聲明該屬性,那么Spring會采用"全限定名#{number}"的方式自動生成一個編號,number從0開始計數(shù)。
比如聲明了兩個Person對象,如下:
<bean class="com.xl.spring.xlIoc.beans.Person"> <property name="id" value="1"/> <property name="name" value="宋智孝"/> <property name="nickName" value="懵懵"/> </bean> <bean class="com.xl.spring.xlIoc.beans.Person"> <property name="id" value="2"/> <property name="name" value="周杰倫"/> <property name="nickName" value="Jack"/> </bean>
這時想要獲取對象:
ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml"); Person person = (Person) ac.getBean(Person.class.getName() + "#1"); System.out.println(person.toString());
2.class:注入的對象的類型,對應(yīng)的是類的全限定名。
3.property:定義類的屬性,其中name表示屬性名,value表示屬性值。
3,裝配集合
以上實現(xiàn)注入,都是對于一些基本數(shù)據(jù)類型和String類型。如果數(shù)據(jù)類型是集合的話,那么用如下做法:
1.首先定義一個新的類 --> Coder:
package com.xl.spring.xlIoc.beans; import lombok.Data; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @Data public class Coder { private String name; private List<String> languages; private Set<String> friends; private Map<String, Integer> houses; private Properties properties; }
2.在Spring的xml文件中,注入Coder:
<bean id="coder" class="com.xl.spring.xlIoc.beans.Coder"> <property name="name" value="宋智孝"/> <!--配置list集合--> <property name="languages"> <list> <value>java</value> <value>JavaScript</value> <value>SQL</value> </list> </property> <!--配置set集合--> <property name="friends"> <set> <value>金鐘國</value> <value>河?xùn)|勛</value> <value>劉在石</value> <value>池石鎮(zhèn)</value> <value>全邵旻</value> </set> </property> <!--配置map集合--> <property name="houses"> <map> <entry key="麻浦區(qū)" value="240000000" /> </map> </property> <!--配置properties集合--> <property name="properties"> <props> <prop key="身高">168</prop> <prop key="體重">52.3</prop> <prop key="年齡">30</prop> </props> </property> </bean>
3.調(diào)用:
public static void testCoder() { //1.通過配置文件獲取到spring的上下文 ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml"); Coder coder = (Coder) ac.getBean("coder"); coder.getFriends().forEach(s -> System.out.println("friend ---> " + s)); }
4,復(fù)雜Bean裝配
如果屬性是一個復(fù)雜集合對象,比如屬性是List或Map,而這個List的泛型是一個對象,或者說Map的key和value都是一個對象。
這個時候怎么辦?
解決思路:先把泛型對應(yīng)的Bean注入好,然后在注入屬性的時候引入過去就好。
1.首先定義三個類:其中UserRole中需要使用User對象和Role對象
package com.xl.spring.xlIoc.beans; import lombok.Data; @Data public class User { private Integer id; private String name; }
package com.xl.spring.xlIoc.beans; import lombok.Data; @Data public class Role { private Integer id; private String name; } package com.xl.spring.xlIoc.beans; import lombok.Data; import java.util.List; import java.util.Map; @Data public class UserRole { private Integer id; private List<User> users; private Map<Role, User> map; }
2.先注入對應(yīng)的User對象和Role對象,然后再注入UserRole對象:
<bean id="user1" class="com.xl.spring.xlIoc.beans.User"> <property name="id" value="1"/> <property name="name" value="張生"/> </bean> <bean id="user2" class="com.xl.spring.xlIoc.beans.User"> <property name="id" value="2"/> <property name="name" value="鶯鶯"/> </bean> <bean id="role1" class="com.xl.spring.xlIoc.beans.Role"> <property name="id" value="1"/> <property name="name" value="管理員"/> </bean> <bean id="role2" class="com.xl.spring.xlIoc.beans.Role"> <property name="id" value="2"/> <property name="name" value="普通用戶"/> </bean> <bean id="userRole" class="com.xl.spring.xlIoc.beans.UserRole"> <property name="id" value="1"/> <property name="users"> <list> <ref bean="user1"/> <ref bean="user2"/> </list> </property> <property name="map"> <map> <entry key-ref="role1" value-ref="user1"/> <entry key-ref="role2" value-ref="user2"/> </map> </property> </bean>
3.創(chuàng)建對象并使用:
public static void testComplexBean() { ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml"); UserRole userRole = (UserRole) ac.getBean("userRole"); System.out.println(userRole); }
到此這篇關(guān)于Spring Xml裝配Bean的文章就介紹到這了,更多相關(guān)Spring Xml裝配Bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot中登錄后關(guān)于cookie和session攔截問題的案例分析
這篇文章主要介紹了Springboot中登錄后關(guān)于cookie和session攔截案例,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Spring Boot集成 Spring Boot Admin 監(jiān)控
這篇文章主要介紹了Spring Boot集成 Spring Boot Admin 監(jiān)控,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-08-08Java項目中實現(xiàn)使用traceId跟蹤請求全流程日志
這篇文章主要介紹了Java項目中實現(xiàn)使用traceId跟蹤請求全流程日志方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08springboot中報錯Invalid character found in
這篇文章主要介紹了springboot中報錯Invalid character found in the request的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09