Spring學習(五)
自動裝備
1、定義
- 自動裝配(autowiring): 將某個Bean實例中所引用的其它的Bean自動註入到當前Bean實例中
- 自動裝配就是指由Spring來自動地註入依賴對象,無需人工參與。
- 自動裝配的好處是減少構造器註入和setter註入配置,減少配置文件的長度。自動裝配通過配置<bean>標簽的 “autowire”屬性來改變自動裝配方式。
2、四種裝配方式
- no(default): 默認值,即不啟動自動裝配,需要顯示地引用相應的bean
setter 註入 : ( setter-base ),提供setter方法進行註入,依賴於無參構造和setter方法
Person類
package ecut.ioc.autowiring; public class Person { private Integer id; private String name; private Dog wangcai ; public Person() { super(); } public Person(Dog wangcai) { super(); this.wangcai = wangcai; System.out.println(
Dog類
package ecut.ioc.autowiring; public class Dog { private String name ; public String getName() { return name; } public void setName(String name) { this.name = name; } }
配置文件
<bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> <bean id="p" class="ecut.ioc.autowiring.Person" > <property name="id" value="1001" /> <property name="name" value="華安" /> <property name="wangcai" ref="dog" /> </bean>
構造方法註入 : ( constructor-base )通過構造方法往裏面傳入值
配置文件
<bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> <bean id="p" class="ecut.ioc.autowiring.Person" > <property name="id" value="1001" /> <property name="name" value="華安" /> <constructor-arg name="wangcai" ref="dog" /> </bean>
- byName: 根據屬性名稱和被引用的bean名稱來實現自動註入(setter)
byName-autowiring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> <bean id="wangcai" class="ecut.ioc.autowiring.Dog" > <property name="name" value="旺財" /> </bean> <!-- 根據 名稱 實現 自動裝配 ( setter ) --> <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="byName" > <property name="id" value="1001" /> <property name="name" value="華安" /> <!-- <property name="wangcai" ref="wangcai" /> --> </bean> </beans>
如果在被裝配的bean中含有xxx屬性(實際上是setter方法),則會自動把id為xxx的bean裝配上來
測試類package ecut.ioc.autowiring; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireByName { public static void main(String[] args) { String configLocations = "classpath:ecut/**/byName-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Person s = container.getBean( "huaan" , Person.class ); System.out.println( s.getId() + " : " + s.getName() ); Dog d = s.getWangcai(); System.out.println( d.getName() ); container.close(); } }
- byType: 根據屬性類型和被引用的bean的類型來實現自動註入(setter)
byType-autowiring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <!-- NoUniqueBeanDefinitionException <bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> --> <bean id="wangcai" class="ecut.ioc.autowiring.Dog" > <property name="name" value="旺財" /> </bean> <!-- 根據 類型 實現 自動裝配 ( setter ) --> <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="byType" > <property name="id" value="1001" /> <property name="name" value="華安" /> </bean> </beans>
如果在被裝配的bean中含有一個xxx類型的屬性,則會自動把類型為xxx的bean裝配上來,但是如果含有多個xxx類型的bean,則拋出NoUniqueBeanDefinitionException
測試案例package ecut.ioc.autowiring; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireByType { public static void main(String[] args) { String configLocations = "classpath:ecut/**/byType-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Person s = container.getBean( "huaan" , Person.class ); System.out.println( s.getId() + " : " + s.getName() ); Dog d = s.getWangcai(); System.out.println( d.getName() ); container.close(); } }
- constructor: 根據構造方法的參數類型和被引用的Bean的類型實現自動裝配(constructor)
當存在多個同種類型的Bean與構造方法中的參數類型相同時:
如果某Bean的名稱跟參數的名稱一致,則根據名稱進行自動裝配。constructor-autowiring.xml中bean的名稱和構造方法中 public Person(Dog wangcai) 的參數保持一致。下面配置輸出狗的名稱是旺財<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> <bean id="wangcai" class="ecut.ioc.autowiring.Dog" > <property name="name" value="旺財" /> </bean> <!-- 根據 類型 實現 自動裝配 ( constructor ) --> <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="constructor" > <property name="id" value="1001" /> <property name="name" value="華安" /> </bean> </beans>
如果這些Bean的名稱跟參數的名稱都不相同,則不再執行自動裝配。但是,也不拋出異常
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> <bean id="wc" class="ecut.ioc.autowiring.Dog" > <property name="name" value="旺財" /> </bean> <!-- 根據 類型 實現 自動裝配 ( constructor ) --> <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="constructor" > <property name="id" value="1001" /> <property name="name" value="華安" /> </bean> </beans>
當且僅當與構造方法中的參數類型相同的Bean只有一個時,此時根據類型進行自動裝配。下面配置輸出狗的名稱是拉的多不多
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="dog" class="ecut.ioc.autowiring.Dog" > <property name="name" value="拉的多不多" /> </bean> <!-- 根據 類型 實現 自動裝配 ( constructor ) --> <bean id="huaan" class="ecut.ioc.autowiring.Person" autowire="constructor" > <property name="id" value="1001" /> <property name="name" value="華安" /> </bean> </beans>
測試類
package ecut.ioc.autowiring; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireByConstructor { public static void main(String[] args) { String configLocations = "classpath:ecut/**/constructor-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations ); Person s = container.getBean( "huaan" , Person.class ); System.out.println( s.getId() + " : " + s.getName() ); Dog d = s.getWangcai(); System.out.println( d.getName() ); container.close(); } }
使用構造方法自動裝備,創建person實例時,使用帶參數的構造,並為參數註入指定類型的bean。
加載配置文件通配符
1、* 匹配同一級別路勁的多個字符,ecut/*/autowiring/constructor-autowiring.xml
2、 ** 匹配多級路勁中的多個字符,ecut/**/constructor-autowiring.xml
String configLocations = "classpath:ecut/**/constructor-autowiring.xml" ; AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );
ecut.ioc.autowiring包中有一個constructor-autowiring.xml
3、 ?僅匹配一個字符
4、如果多個包中都beans.xml文件,並且期望全部加載它們,則可以寫作classpath*:ecut/**/beans.xml
ecut包中有beans.xml, ecut.ex包中有beans.xml,ecut.xxx.yyyy包中有beans.xml
轉載請於明顯處標明出處
https://www.cnblogs.com/AmyZheng/p/9253193.html
Spring學習(五)