最小化Spring XML配置

@Inject
  和@Autowired同樣,@Inject能夠用來自動裝配屬性、方法和構造器;與@Autowired不一樣的是,@Inject沒有required屬性。
 所以,@Inject註解所標註的關係必須存在,若是不存在,則會拋出異常。
  @Inject還提供了另外一種技巧。與其直接注入一個應用,不如要求@Inject注入一個Provider。Provider接口能夠實現Bean應用的
 延遲注入以及Bean的多個實例等功能。
  例如,咱們有一個KnifeJuggler類須要注入一個或多個Knife的實例。假設Knife Bean的做用域聲明爲prototype,下面的
 KnifeJuggler的構造器將得到5個KnifeBean:
  private Set<Knife> knives;
  
  public KnifeJuggler(Provider<Knife> knifeProvider){
   knives = newHashSet<>();
   for(int i = 0;i<5;i++){
    knives.add(knifeProvider.get());
   }
  }
  
  KnifeJuggler將得到一個Provider<Knife>,而不是在構造器中得到以個Knife實例。這個時候,只有provider被注入進去
 在調用provider的get()方法以前,實際的knife對象並無被注入。在這個示例中,get()方法被調用了5次。由於Knife name的做用域
 爲prototype,因此knife的Set集合將被賦予5個不一樣的Knife對象
 
在註解注入中使用表達式@Value
 @Value直接標註某個屬性、方法或者方法參數,並傳入一個String類型的表達式來裝配屬性,
 例如:
  @Value("Eruption")
  private String song;
  
 實際上,裝配簡單的值並非@Value所擅長的,不過,藉助SpEL表達式,@Value被賦予了魔力。
 例如: 
  @Value("#{sytemProperties.myFavoriteSong}")
  private String song;
  
自動檢測Bean:
 一、<contxt:componenet-scan>元素除了完成與<context:annotation-config>同樣的工做,還容許Spring自動檢測Bean和定義Bean
 這意味着Spring應用中的大多數(或者全部)Bean都可以實現定義和裝配.
 
 二、過濾組件掃描
    事實上,能夠經過爲<context:componenet-scan>配置<context:include-filter>和/或者<context:exclude-filter>
 子元素,咱們能夠隨意調整掃描行爲
 
  <context:componenet-scan base-package = "com.springinaction.springidol">
   <context:include-filter type="assignable" expression="com.springinaction.springidol.Instruments"/>
  </context:componenet-scan>
  
  過濾器類型                     描述
  annotation                    過濾器掃描使用指定註解所標註的哪些類,經過expression屬性指定要掃描的註解
  assignable                    過濾器掃描派生與expression屬性所指定雷丁的那些類
  aspectj                       過濾器掃描與expression屬性所指定的AspectJ表達式所匹配的那些類
  custom                        使用自定義的org.springframework.core.type.TypeFilter實現類,該類由expression屬性指定
  regex                         過濾器掃描類的名稱與expression屬性所指定的正則表達式所匹配的那些類
  
  除了使用<context:include-filter>告知<context:componenet-scan>哪些類須要註冊爲Spring Bean之外,咱們還能夠使用
 <context:exclude-filter>來告知<context:componenet-scan>哪些類不須要註冊爲Spring Bean。
     例如,除了自定義的@SkipIt註解的類,其餘全部的Instruments實現都須要註冊爲Spring Bean。
  
  <context:componenet-scan base-package = "com.springinaction.springidol">
   <context:include-filter type="assignable" expression="com.springinaction.springidol.Instruments"/>
   <context:exclude-filter type="annotation" expression="com.springinaction.springidol.SkipIt"/>
  </context:componenet-scan>
  
  
定義個一個配置類
    在基於java的配置裏使用@Configuration註解的java類,就等價於xml配置中的<beans>元素元素。
 例如: 
 package com.springinaction.springidol
 import org.springframework.context.annotation.Configuration;
 
 @Configuration
 public class SpringIdoIConfig{
 
  //Bean declaration methods go here
 }
 
  @Configuration註解會做爲一個標識告訴Spring:這個類將包含一個或多個Spring Bean的定義。這些Bean的定義是使用
 @Bean註解所標註的方法。讓咱們看一下如何使用@Bean來裝配使用spring基於java的配置所聲明的Bean。
 
  @Bean
  public Performer duke(){
   return new Juggler();
  }
  
  這個簡單方法就是java配置,他等價於咱們以前使用xml所配置的<bean>元素。@Bean告知Spring這個方法將返回一個對象,
 這個對象應該被註冊爲Spring應用上下文中的一個Bean。方法名將做爲該Bean的Id。在該方法中所實現的全部邏輯本質上都是爲了建立Beanjava

相關文章
相關標籤/搜索