? ? ? 自動裝配(autowiring)有助于減少甚至消除配置<property>元素和<constructor-arg>元素,讓Spring自動識別如何裝配Bean的依賴關系。
? ? ? 自動檢測(autodiscovery)比自動裝配更進了一步,讓Spring能夠自動識別哪些類需要被配置成Spring Bean,從而減少對<bean>元素的使用。
1.自動裝配屬性
? ???1.1 ?4種類型的自動裝配
? ? ??●?byName——把與Bean的屬性具有相同名字(或者ID)的其他Bean自動裝配到Bean的對應屬性中。如果沒有跟屬性的名字相匹配的Bean,則該屬性不進行裝配。? ? ? ●?byType——把與Bean的屬性具有相同類型的其他Bean自動裝配到Bean的對應屬性中。如果沒有跟屬性的類型相匹配的Bean,則該屬性不被裝配。
? ? ? ●?constructor——把與Bean的構造器入參具有相同類型的其他Bean自動裝配到Bean構造器的對應入參中。
? ? ? ●?autodetect——首先嘗試使用constructor進行自動裝配。如果失敗,在嘗試使用byType進行自動裝配。
? ? ??
? ? ? byName自動裝配:
<bean id = "kenny" class = "com.ouc.test.springs.Instruments" autowire = "byName" ><property name = "song" value = "bells" />
</bean>
? ??
? ? ?byType自動裝配:
? ??如果Spring尋找到多個Bean,它們的類型與自動裝配的屬性類型都相匹配,Spring不會猜測哪一個Bean更適合自動裝配,而是會拋出異常。
? ? 可以為自動裝配標識一個首選Bean,或者可以取消某個Bean自動裝配的候選資格。為了使用primary屬性,不得不將非首選Bean的primary屬性設置為false。
? ? 可以為自動裝配標識一個首選Bean,或者可以取消某個Bean自動裝配的候選資格。為了使用primary屬性,不得不將非首選Bean的primary屬性設置為false。
<bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" primary = "false" />
? ? 如果希望排除某些Bean,可以設置這些Bean的autowire-candidate屬性為false。
<bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" autowire-candidate = "false" />
? ? constructor自動裝配:
? ??如果要通過構造器注入來配置Bean,可以移除<constructor-arg>元素,由Spring在應用上下文中自動選擇Bean注入到構造器入參中。
<bean id = "saxophone" class="com.ouc.springs.test.Saxophone" autowire ="constructor" />
? ? 最佳自動裝配:
<bean id = "saxophone" class="com.ouc.springs.test.Saxophone" autowire ="autodetect" />
? ?1.2 默認自動裝配
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsddefault-autowire="byType" ></beans>
2 .使用注解裝配
? ??Spring容器默認禁用注解裝配。? ? 啟用注解裝配最簡單的方式是使用Spring的context命名空間配置中的<context:annotation-config>元素。
? ? Spring 3 支持幾種不同的用于自動裝配的注解:
? ?? ●?Spring自帶的@Autowired注解;
? ? ?●?JSR-330的@Inject注解;
? ? ?●?JSR-250的@Resource注解。
? ??
? ?2.1?使用@Autowired
@Autowiredpublic void setInstrument(Instrument instrument){this.instrument = instrument;}
? ? ?可以使用@Autowired注解直接標注屬性,并刪除setter方法:
@Autowiredprivate Instrument instrument;
? ? ? ? 默認情況下,@Autowired所標注的屬性或參數必須是可以裝配的。如果沒有Bean可以裝配到@Autowired所標注的屬性或參數中,自動裝配就會失敗(拋出NoSuchBeanDefinitionException).
? ? ? ? 可以通過設置@Autowired的required屬性為false來配置自動裝配是可選的。
@Autowired(required=false)private Instrument instrument;
? ? @Qualifier注解縮小了自動裝配挑選候選Bean的范圍,通過指定Bean的ID把選擇范圍縮小到只剩下一個Bean。
@Autowired@Qualifier("guitar")private Instrument instrument;
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.beans.factory.annotation.Qualifier;@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Qualifierpublic @interface StringedInstrument{}
? 2.2?借助@Inject實現基于標準的自動裝配
@Injectprivate Instrument instrument;
? ? ?限定@Inject所標注的屬性。
@Inject@Named("guitar")private Instrument instrument;
? ? 創建自定義的JSR-330 Qualifier
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import javax.inject.Qualifier;@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Qualifierpublic @interface StringedInstrument{}
? ?2.3?在注解注入中使用表達式
@Value("#{systemProperties.myFavoriteSong}")private String song;
3.自動檢測Bean
? ? ?使用<context:component-scan>元素配置自動檢測。<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context=="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/beans/spring-context-3.1.xsd" ><context:component-scan base-package="com.ouc.springs.test" ></context:component-scan></beans>
? ? 3.1為自動檢測標注Bean
? ? ?默認情況下,<context:component-scan>查找使用構造型(stereotype)注解所標注的類,這些特殊的注解如下:? ? ? ?@Component——通用的構造型注解,標識該類為Spring組件。
? ? ? ?@Controller——標識將該類定義為Spring MVC Controller。
? ? ? ?@Repository——標識將該類定義為數據倉庫。
? ? ? ?@Service——標識將該類定義為服務。
? ? ? ? 使用@Component標注的任意自定義注解。
? ?3.2?過濾組件掃描
? ? 通過為<context:component-scan>配置<context:include-filter>和<context:exclude-filter>子元素,可以隨意調整掃描行為。<context:component-scan base-package="com.ouc.springs.test" ><context:include-filter type="assignable" expression="com.ouc.springs.tst.Instrument" /><context:exclude-filter type="annotation" expression="com.ouc.springs.tst.SkipIt" />
</context:component-scan>
4.使用Spring基于Java的配置
? ? 4.1?定義一個配置類
import org.springframework.context.annotation.Configuration;@Configurationpublic class SpringIdolConfig{}
? ??
? ?4.2 聲明一個簡單的Bean
@Beanpublic Performer duke(){return new Juggler();}