Spring学习笔记—最小化Spring XML配置
生活随笔
收集整理的這篇文章主要介紹了
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進行自動裝配。 ? ? ?? ? ? ? byName自動裝配:
<bean id = "kenny" class = "com.ouc.test.springs.Instruments" autowire = "byName" ><property name = "song" value = "bells" /> </bean> ? ??為屬性自動裝配ID與該屬性的名字相同的Bean。 ? ?? ? ? ?byType自動裝配: ? ??如果Spring尋找到多個Bean,它們的類型與自動裝配的屬性類型都相匹配,Spring不會猜測哪一個Bean更適合自動裝配,而是會拋出異常。
? ? 可以為自動裝配標識一個首選Bean,或者可以取消某個Bean自動裝配的候選資格。為了使用primary屬性,不得不將非首選Bean的primary屬性設置為false。 <bean id = "saxophone" class = "com.ouc.test.springs.Saxophone" primary = "false" /> ? ? primary屬性僅對標識首選Bean有意義。
? ? 如果希望排除某些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;} ? ???Spring會嘗試對該方法執行byType自動裝配,可以使用@Autowired標注需要自動裝配Bean引用的任意方法。? ? ?可以使用@Autowired注解直接標注屬性,并刪除setter方法: @Autowiredprivate Instrument instrument; ? ? 1)可選的自動裝配:
? ? ? ? 默認情況下,@Autowired所標注的屬性或參數必須是可以裝配的。如果沒有Bean可以裝配到@Autowired所標注的屬性或參數中,自動裝配就會失敗(拋出NoSuchBeanDefinitionException).
? ? ? ? 可以通過設置@Autowired的required屬性為false來配置自動裝配是可選的。
@Autowired(required=false)private Instrument instrument; ? ? ?2)限定歧義性的依賴
? ? @Qualifier注解縮小了自動裝配挑選候選Bean的范圍,通過指定Bean的ID把選擇范圍縮小到只剩下一個Bean。
@Autowired@Qualifier("guitar")private Instrument instrument; ? ? ?3)創建自定義的限定器(Qualifier) 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沒有required屬性。? ? ?限定@Inject所標注的屬性。 @Inject@Named("guitar")private Instrument instrument; ? ? @Named通過Bean的ID來標識可選擇的Bean,@Named實際上是一個使用@Qualifier注解所標注的注解。
? ? 創建自定義的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{} ? ? @Configuration注解會作為一個標識告知Spring:這個類將包含一個或多個Spring Bean的定義。? ??
? ?4.2 聲明一個簡單的Bean
@Beanpublic Performer duke(){return new Juggler();} ? ? ? @Bean告知Spring這個方法將返回一個對象,該對象應該被注冊為Spring應用上下文中的一個Bean,方法名將作為該Bean的ID。轉載于:https://www.cnblogs.com/wp5719/p/5136177.html
總結
以上是生活随笔為你收集整理的Spring学习笔记—最小化Spring XML配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018同行取现手续费多少?六大商业银行
- 下一篇: 大话设计模式之策略模式