一、基於 XML 的 Bean 的配置——通過全類名(反射)
<bean
<!-- id: bean 的名稱在IOC容器內必須是唯一的若沒有指定,則自動的將全限定類名作為 改 bean 的名稱-->id="hello"
<!-- 通過全類名的方式來配置 bean -->class="com.atguigu.spring.helloworld.HelloWorld">
</bean>
1.通過屬性注入
即通過 setXxx() 方法注入 Bean 的屬性值或依賴的對象。
使用 <property name="" value="" ref=""/> 元素,其中 name 值為對應 Bean 屬性名稱,value 指定對應屬性值,ref 引用其他 Bean,value 屬性和 ref 屬性不可以同時存在。
也可以通過 <value></value> 子節點的方式指定屬性值。
e:
<bean id="helloWorld" class="com.nucsoft.spring.helloworld.HelloWorld"> <property name="hello" value="spring"/> <property name="world"> <value>spring</value> </property> </bean>
注意:在此種方式下,要配置的 Bean 必須存在空參構造器。
2.通過構造器注入
通過構造方法注入屬性值或依賴的對象。
使用 <constructor-arg value="" index="" name="" ref="" type=""/> 元素,value 屬性為對應的參數指定值,ref 引用其他 Bean。不可同時存在。
通過 name 屬性、index 屬性、type屬性提供了三種注入方式:
(1)name:按照構造器參數名匹配入參
<bean id="car1" class="com.nucsoft.spring.helloworld.Car"> <constructor-arg name="brand" value="奧迪"/> <constructor-arg name="corp" value="上海"/> <constructor-arg name="price" value="400000"/> </bean>
(2)index:按照索引匹配入參
<bean id="car2" class="com.nucsoft.spring.helloworld.Car"> <constructor-arg index="0" value="大眾"/> <constructor-arg index="1" value="10000"/> </bean>
(3)type:按照類型匹配入參
<bean id="car3" class="com.nucsoft.spring.helloworld.Car"> <constructor-arg type="java.lang.String" value="寶馬"/> <constructor-arg type="double" value="200000"/> </bean>
注意:Spring 雖然提供了通過構造器的方式進行注入,但是並不推薦使用此種方式。
3.細節問題
(1)若注入的屬性值包含特殊字符,可以使用 <![CDATA[]]> 包含該屬性值。
如:
<bean id="car4" class="com.nucsoft.spring.Car"> <property name="brand"> <value><![CDATA[<BM>]]> </value> </property> <property name="corp" value="德國"/> <property name="maxSpeed" value="230"/> <property name="price" value="20000000"/> </bean>
(2)可以通過 <ref> 元素或 ref 屬性為 Bean 的屬性或構造器指定對 Bean 的引用。
如:
e1:
<bean id="service" class="com.nucsoft.spring.Service"/> <bean id="action" class="com.nucsoft.spring.Action"> <property name="service" ref="service"/> </bean>
e2:
<bean id="feather" class="com.nucsoft.spring.Feather"> <property name="length" value="13"/> </bean> <bean id="bird" class="com.nucsoft.spring.Bird"> <constructor-arg name="birdName" value="smallBird"/> <constructor-arg name="feather" ref="feather"/> </bean>
(3)內部Bean:可以在屬性或構造器里包含 Bean 的聲明,內部 Bean 不需要指定 id,同時在別的地方也無法引用。
如:
e1:
<bean id="action" class="com.nucsoft.spring.Action"> <property name="service"> <bean class="com.nucsoft.spring.Service"/> </property> </bean>
e2:
<bean id="bird" class="com.nucsoft.spring.Bird"> <constructor-arg name="birdName" value="smallBird"/> <constructor-arg name="feather"> <bean class="com.nucsoft.spring.Feather"> <property name="length" value="13"/> </bean> </constructor-arg> </bean>
4.為引用類型(字符串或對象)注入 Null 值
<bean id="bird" class="com.nucsoft.spring.Bird"> <constructor-arg name="birdName" value="smallBird"/> <constructor-arg name="feather"> <null/> </constructor-arg> </bean>
5.為級聯屬性注入
(1)通過屬性
<bean id="feather" class="com.nucsoft.spring.Feather"> <property name="length" value="44"/> </bean> <bean id="bird" class="com.nucsoft.spring.Bird"> <property name="birdName" value="smallBird"/> <property name="feather" ref="feather"/> <property name="feather.length" value="23"/> </bean>
(2)通過構造器
<bean id="feather" class="com.nucsoft.spring.Feather"> <property name="length" value="44"/> </bean> <bean id="bird2" class="com.nucsoft.spring.Bird"> <constructor-arg name="birdName" value="bigBird"/> <constructor-arg name="feather" ref="feather"/> <property name="feather.length" value="33"/> </bean>
注意:設置級聯屬性的前提是,級聯屬性所屬對象已經被注入到容器中。同時需要為級聯屬性所屬對象提供 getXxx() 方法。
6.集合屬性
(1)集合性質的類型:List,Set,Map 和 數組
(2)使用 <list>,<set>,<map> 來配置集合屬性,其中數組和 List 都使用 <list>
(3)List:通過引用外部 Bean 或 創建內部 Bean 的方式
<bean id="department" class="com.nucsoft.spring.Department"> <property name="deptName" value="dept01"/> </bean> <bean id="company" class="com.nucsoft.spring.Company"> <property name="companyName" value="ICBC"/> <property name="departments"> <list> <ref bean="department"/> <bean class="com.nucsoft.spring.Department"> <property name="deptName" value="dept02"/> </bean> </list> </property> </bean>
測試:
private ApplicationContext ctx = null; @Before public void init() { ctx = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml"); } @Test public void test01() { Company com = ctx.getBean(Company.class); System.out.println(com); }
輸出:
Company{companyName='ICBC', departments=[Department{deptName='dept01'}, Department{deptName='dept02'}]}
(4)Set:與 List 類似。不做重復說明。
(5)Map: 在<map> 標簽中使用多個 <entry> 子標簽:簡單字面值使用屬性 key 和 value 來定義,Bean 的引用通過 key-ref 和 value-ref 屬性定義。
如:
<bean id="emp02" class="com.nucsoft.spring.Employee"> <property name="employeeName" value="emp02"/> <property name="age" value="23"/> </bean> <bean id="department" class="com.nucsoft.spring.Department"> <property name="deptName" value="dept01"/> </bean> <bean id="department02" class="com.nucsoft.spring.Department"> <property name="deptName" value="dept02"/> </bean> <bean id="company" class="com.nucsoft.spring.Company"> <property name="companyName" value="ICBC"/> <property name="departments"> <map> <entry key-ref="department" value-ref="emp01"/> <entry key-ref="department02" value-ref="emp02"/> </map> </property> <property name="strs"> <map> <entry key="key01" value="value01"/> <entry key="key02" value="value02"/> </map> </property> </bean>
測試:
private ApplicationContext ctx = null; @Before public void init() { ctx = new ClassPathXmlApplicationContext("spring/ApplicationContext.xml"); } @Test public void test01() { Company com = ctx.getBean(Company.class); System.out.println(com); }
輸出:
Company{companyName='ICBC', departments={Department{deptName='dept01'}=Employee{employeeName='emp01', age=12}, Department{deptName='dept02'}=Employee{employeeName='emp02', age=23}}}
注意:不能在 <entry> 標簽中定義 Bean 標簽,不支持。
(6)Properties 類型的屬性:和 Map 類似,在 <props> 標簽中使用 <prop> 子標簽, 每個 <prop> 子標簽必須定義 key 屬性。
<bean id="DataSourceId" class="com.nucsoft.spring.DataSourceBean"> <property name="propertiesInfo"> <props> <prop key="user">root</prop> <prop key="password"></prop> <prop key="jdbcUrl">jdbc:mysql:///test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean>
7.定義單獨的集合 Bean
使用基本的集合標簽定義集合時,不能將集合作為獨立的 Bean 定義,導致其他 Bean 無法引用該集合,所以無法在不同的 Bean 之間共用。
可以使用 util schema 里的集合標簽定義獨立的集合 Bean。需要注意的是,定義的集合 Bean 與 <bean> 標簽是同級別的。
<bean id="company" class="com.nucsoft.spring.Company"> <property name="companyName" value="ICBC"/> <property name="departments" ref="departments"/> </bean> <util:list id="departments"> <bean class="com.nucsoft.spring.Department"> <property name="deptName" value="dept01"/> </bean> <ref bean="department02"/> </util:list>
可以定義到外部的集合 Bean 有:
8.使用 p 命名空間,簡化 XML 配置文件
<bean id="bird3" class="com.nucsoft.spring.Bird" p:birdName="smallBird" p:feather-ref="feather"/>
二、基於 XML 的 Bean 的配置——通過工廠方法
1.通過靜態工廠方法
直接調用某一個類的靜態方法,可以返回一個 Bean 的實例。通過 class 屬性來指定全類名,使用 factory-method 來指定生成 bean 的靜態方法。使用 constructor-arg 為靜態方法傳入參數。如:
<bean class="java.text.DateFormat" id="dateFormat" factory-method="getDateInstance"> <constructor-arg value="1"/> </bean>
@Test public void test04() { DateFormat dateFormat = (DateFormat) ctx.getBean("dateFormat"); System.out.println(dateFormat); }
適用情況:官方或第三方提供的通過靜態方法生成 bean 對象,不需要我們再創建,對應的 Bean 類一般是通過 newInstance 軟編碼的方式來創建。
2.通過實例工廠方法:將對象創建的過程封裝到另外一個對象的方法里。
(1)首先要創建和配置一個含有工廠方法的 Bean
(2)在另一個 bean 配置中調用含有工廠方法 bean 的非 static 方法(即工廠方法)來返回一個 bean 的實例,factory-bean:指向已經創建好的工廠 bean ,factory-method:實例化工廠方法,construtor-arg:為實例工廠方法傳入參數。
如:
工廠 Bean 的配置:
<bean class="java.text.SimpleDateFormat" id="simpleDateFormat"> <constructor-arg value="yyyy-MM-dd"/> </bean>
目標 Bean 的配置:
<bean factory-bean="simpleDateFormat" factory-method="parse" id="date"> <constructor-arg value="2012-02-24"/> </bean>
測試:
@Test public void test05() { Date date = (Date) ctx.getBean("date"); System.out.println(date); }
控制台輸出:
Fri Feb 24 00:00:00 CST 2012
三、基於 XML 的 Bean 的配置——通過 FactoryBean 配置 Bean
1.Spring 中有兩類 Bean,一種是普通 Bean,另外一種是工廠 Bean,即 FactoryBean
2.工廠 Bean 與普通 Bean 不同,其返回的對象不是指定類的一個實例,其返回的是該工廠 Bean 的 getObject() 方法返回的對象
3.具體操作
(1)目標工廠 Bean 需要實現 FactoryBean 接口
(2)返回的對象為 getObject() 方法返回的對象。
如:
工廠 Bean:
/** * @author solverpeng * @create 2016-07-19-11:45 */ public class DepartmentFactoryBean implements FactoryBean<Department>{ @Override public Department getObject() throws Exception { Department department = new Department(); department.setDeptName("dept01"); department.setEmployee(new Employee()); return department; } @Override public Class<?> getObjectType() { return Department.class; } @Override public boolean isSingleton() { return true; } }
Spring Config 配置:
<bean class="com.nucsoft.spring.bean.DepartmentFactoryBean" id="department"/>
測試:
@Test public void test06() { Department department = (Department) ctx.getBean("department"); System.out.println(department); }
三、基於注解的 Bean 的配置
前提:需要導入 spring-aop-4.0.0.RELEASE.jar 包
1.在 classpath 中掃描組件(component scanning):Spring 能夠從 classpath 下自動掃描和實例化具有特定注解的組件。
2.特定的組件包括:
(1)@Component:基本注解,標識了一個受 Spring 管理的組件
(2)@Respository:標識持久層組件
(3)@Service:標識服務層組件
(4)@Controller :標識表現層組件
3.對於掃描到的組件,Spring 有默認的命名策略:使用簡短類名,第一個字母小寫。也可以通過 value 屬性指定名稱。
4.當在組件類上使用了特定注解后,還需要在 Spring 的配置文件中聲明 <context:component-scan>。
屬性:
base-package:指定要掃描的基類包,Spring會自動掃描這個基類包及其子包中的所有類。當需要掃描多個包是,可以使用逗號進行分割。
如:
<context:component-scan base-package="com.nucsoft.spring"/>
resource-pattern:若需要掃描基包下指定的類,可以使用該屬性過濾特定的類。默認是:"**/*.class"。
如:
<context:component-scan base-package="com.nucsoft.spring" resource-pattern="component/*.class" />
<context:include-filter type="" expression=""/> 子節點表示要包含的目標類
<context:exclude-filter type="" expression=""/> 子節點表示要排出在外的目標類
可以同時包含多個 <context:include-filter type="" expression=""/> 和 <context:exclude-filter type="" expression=""/> 子節點
屬性 type:表示支持不同類型的過濾
annotation:所有標注某注解的類。該類型采用目標類是否標注了某注解進行過濾
e:所有在基包下標注 @Controller 注解的類都不被掃描
<context:component-scan base-package="com.nucsoft.spring"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
assinable:所有繼承或擴展某個特定的類進行過濾
e:所有繼承或擴展 Person 類的類都不被掃描
<context:component-scan base-package="com.nucsoft.spring" resource-pattern="component/*.class"> <context:exclude-filter type="assignable" expression="com.nucsoft.spring.bean.Person"/> </context:component-scan>
四、基於注解來裝配 Bean 的屬性
1.<context:component-scan> 還會自動注冊 AutowiredAnnotationBeanPostProcessor 實例,該實例可以自動裝配具有 @Autowired 和 @Resource、@Inject 注解的屬性。
2.@Autowired 注解
自動裝配目標 Bean 的 Bean 屬性(該屬性為一個 Bean 的實例,該 Bean 為在 IOC 容器中存在的 Bean)
(1)可以對屬性標注該注解。如:
/** * @author solverpeng * @create 2016-07-19-16:49 */ @Component public class Department { private String deptName; @Autowired private Employee employee; @Autowired private Address address; public void setDeptName(String deptName) { this.deptName = deptName; } @Override public String toString() { return "Department{" + "deptName='" + deptName + '\'' + ", employee=" + employee + ", address=" + address + '}'; } }
(2)可以對方法標注該注解,方法參數為要自動裝配的類型,可以是任意方法
/** * @author solverpeng * @create 2016-07-19-16:49 */ @Component public class Department { private String deptName; private Employee employee; private Address address; @Autowired public void setAddress(Address address) { this.address = address; } @Autowired public void abc(Employee employee) { this.employee = employee; } public void setDeptName(String deptName) { this.deptName = deptName; } @Override public String toString() { return "Department{" + "deptName='" + deptName + '\'' + ", employee=" + employee + ", address=" + address + '}'; } }
(3)可以對構造器標注該注解,方法參數為要自動裝配的類型。
/** * @author solverpeng * @create 2016-07-19-16:49 */ @Component public class Department { private String deptName; private Employee employee; private Address address; public void setDeptName(String deptName) { this.deptName = deptName; } @Autowired public Department(Employee employee, Address address) { this.employee = employee; this.address = address; } @Override public String toString() { return "Department{" + "deptName='" + deptName + '\'' + ", employee=" + employee + ", address=" + address + '}'; } }
注意:
使用 @Autowired 自動匹配時,如果只匹配到一個的情況下:
按照類型進行匹配
如果匹配到多個:
標記有 primary="true" 的 bean 有限,不能同時出現多個 primary="true" 的相同類型的 bean。否則:UnsatisfiedDependencyException
若都沒有 primary 屬性,在按照 @Autowired 標注的構造器或方法 的參數名稱進行匹配 Bean。若還沒有匹配到,則報:UnsatisfiedDependencyException異常。
具體參見:
org.springframework.beans.factory.support.DefaultListableBeanFactory#findAutowireCandidates
org.springframework.beans.factory.support.DefaultListableBeanFactory#determinePrimaryCandidate
(4)若某一個屬性允許不被設置,可以設置 @Autowired 注解的 required 屬性為 false。
(5)@Qualifier 注解:Spring 允許對方法的入參或屬性標注 @Qualifier 注解。@Qualifier 注解里提供 Bean 的名稱。
如:
屬性處:
@Autowired @Qualifier("address2") private Address address;
方法處:
@Autowired public Department(Employee employee, @Qualifier("address2") Address address) { this.address = address; this.employee = employee; }
(6)@Autowired 注解可以標識在數組類型的屬性上,此時會把所有匹配的 Bean 進行自動裝配
(7)@Autowired 注解可以標識在集合屬性上,此時會將集合類型對應的 Bean 進行自動裝配
(8)@Autowired 注解可以標識在 Map 上,若 key 為 String,則此時會將 value 對應的 Bean 的類型進行自動裝配
3.@Resource 注解
(1)如果說 @Autowired 屬性是按照類型進行自動裝配的,那么 @Resource 注解可以看成是按照名稱進行裝配的。
@Resource 注解要求提供一個 Bean 名稱的屬性,若該屬性為空,則自動采用標注出的變量或方法名作為 Bean 的名稱。
注意:不能標注於構造器。
4.@Inject 注解
和 @Autowire的 注解一樣也是按照類型匹配注入的 Bean ,但是沒有 required 屬性。
5.最后,對比一下@Resource和@Autowired
(1)@Resource默認是按照名稱來裝配注入的,只有當找不到與名稱匹配的bean才會按照類型來裝配注入
(2)@Autowired默認是按照類型裝配注入的,如果想按照名稱來轉配注入,則需要結合@Qualifier一起使用
(3)@Resource注解是又J2EE提供,而@Autowired是由spring提供,故若要減少系統對spring的依賴建議使用 @Resource的方式;
6.最最后,推薦使用 @Autowired 注解。