spring學習筆記—最小化Spring XML配置
自動裝配(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進行自動裝配。
- <bean id = "kenny" class = "com.ouc.test.springs.Instruments" autowire = "byName" >
- <property name = "song" value = "bells" />
- </bean>
可以爲自動裝配標識一個首選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" />
- <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/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- default-autowire="byType" >
- </beans><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
2 .使用註解裝配
Spring容器默認禁用註解裝配。啓用註解裝配最簡單的方式是使用Spring的context命名空間配置中的<context:annotation-config>元素。
Spring 3 支持幾種不同的用於自動裝配的註解:
● Spring自帶的@Autowired註解;
● JSR-330的@Inject註解;
● JSR-250的@Resource註解。
2.1 使用@Autowired
- @Autowired
- public void setInstrument(Instrument instrument){
- this.instrument = instrument;
- }
可以使用@Autowired註解直接標註屬性,並刪除setter方法:
- @Autowired
- private 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)
- @Qualifier
- public @interface StringedInstrument{}
2.2 藉助@Inject實現基於標準的自動裝配
- @Inject
- private 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)
- @Qualifier
- public @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/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://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;
- @Configuration
- public class SpringIdolConfig{}
4.2 聲明一個簡單的Bean
- @Bean
- public Performer duke(){
- return new Juggler();
- }