Spring進階裝配(二) 條件化的bean

來源:互聯網
上載者:User

標籤:pts   var   bsp   actor   註解   bool   gic   nat   執行個體化   

如果你希望一個bean在特定的條件下才會出現:

  • 應用的類路徑下包含特定的庫時才建立
  • 只有當某個特定的bean也聲明之後才會建立
  • 某個特定的環境變數設定之後才建立某個bean

在Spring 4之前,很難實現這種層級的條件化配置,但是Spring4引入了一個新的@Conditional註解,它可以用到帶有@Bean註解的方法上。如果給定的條件計算結果為true,就會建立這個bean,否則的話,這個bean會被忽略。

樣本:設定了magic環境屬性才去執行個體化MagicBean

1 @Bean2 @Conditional(MagicExistsCondition.class)    // 條件化地建立bean3 public MagicBean magicBean() {4     return new MagicBean();5 }

這裡的@Conditional中給定了一個Class,它指明了條件,也就是MagicCondition。

Condition介面如下:

1 public interface Condition {2     boolean matches(ConditionContext ctxt, AnnotatedTypeMetadata metadata);3 }

 

 設定給@Conditional的類可以使任意實現了Condition介面的類型。如此一來,只需要提供matches( )方法的實現即可。如果matches( )方法返回為true,那麼就會建立帶有@Conditional註解的bean。反之則不會建立這些bean。

MagicExistsCondition的實現:

1 public class MagicExistsCondition implements Condition {2     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {3         Environment env = context.getEnvironment();4         return env.containsProperty("magic");    // 檢查是否有magic屬性5     }6 }

 

上面的例子中只擷取了Environment,ConditionContext中的內容還挺豐富的,它是一個介面:

1 public interface ConditionContext {2     BeanDefinitionRegistry getRegistry();3     ConfigurableListableBeanFactory getBeanFactory();4     Environment getEnvironment();5     ResourceLoader getResourceLoader();6     ClassLoader getClassLoader();7 }

 

通過ConditionContext,可以拿到的資源有:

  • 藉助getRegistry()返回的BeanDefinitionRegistry檢查bean的定義
  • 藉助getBeanFactory()返回的ConfigurableListableBeanFactory檢查bean是否存在(這個屌),甚至探查bean的屬性
  • 藉助getEnvironment()返回的Environment檢查環境變數是否存在已經它的值是什麼
  • 讀取並探查getResourceLoader()返回的ResourceLoader所載入的資源
  • 藉助getClassLoader()返回的ClassLoader載入並檢查類是否存在

 

AnnotationedTypeMetadata則能夠讓我們檢查帶有@Bean註解的方法上還有什麼其他註解。

1 public interface AnnotatedTypeMetadata {2     boolean isAnnotated(String var1);3     Map<String, Object> getAnnotationAttributes(String var1);4     Map<String, Object> getAnnotationAttributes(String var1, boolean var2);5     MultiValueMap<String, Object> getAllAnnotationAttributes(String var1);6     MultiValueMap<String, Object> getAllAnnotationAttributes(String var1, boolean var2);7 }

 

通過isAnnotated方法,我們能夠判斷帶有@Bean註解的方法是不是還有其他特定的註解。藉助其他的方法,能夠檢查@Bean註解的方法上其他註解的屬性。

 

在Spring 4開始,@Profile註解進行了重構,使其基於@Conditional和Condition的實現。

在Spring 4中@Profile的實現如下:

1 @Retention(RetentionPolicy.RUNTIME)2 @Target({ElementType.TYPE, ElementType.METHOD})3 @Documented4 @Conditional({ProfileCondition.class})5 public @interface Profile {6     String[] value();7 }

 

ProfileCondition:

 1 class ProfileCondition implements Condition { 2     @Override 3     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 4         if (context.getEnvironment() != null) { 5             MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName()); 6             if (attrs != null) { 7                 for (Object value : attrs.get("value")) { 8                     if (context.getEnvironment().acceptsProfiles(((String[]) value))) { 9                         return true;10                     }11                 }12                 return false;13             }14         }15         return true;16     }17 }

ProfileCondition通過AnnotatedTypeMetadata得到了用於@Profile註解的所有屬性。藉助該資訊,它會明確地檢查value屬性,該屬性包含了bean的profile名稱。然後根據ConditionContext得到的Environment來檢查(藉助acceptsProfiles()方法)該profile是否處於啟用狀態。

總結
  • @Conditional註解可以實現一個bean的條件化聲明
  • Profile在Spring 4中使用@Conditional進行了重構
  • 實現條件化配置中有兩個關鍵的介面:ConditionContext和AnnotatedTypeMetadata,在檢測條件的時候起了關鍵作用
  • 這裡沒有講在xml中如何?條件化配置
  • 這裡發現用Java來進行config還是挺爽的

Spring進階裝配(二) 條件化的bean

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.