码迷,mamicode.com
首页 > 编程语言 > 详细

Spring4.0系列5-@Conditional

时间:2017-09-07 13:44:57      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:布尔   metadata   meta   star   rri   配置   some   .com   param   

这篇文章介绍Spring 4的@Conditional注解。在Spring的早期版本你可以通过以下方法来处理条件问题:

  • 3.1之前的版本,使用Spring Expression Language(SPEL)。
  • 3.1版本有个新特性叫profile,用来解决条件问题。

1、Spring Expression Language(SPEL)

SPEL有一个三元运算符(if-then-else)可以在配置文件中当作条件语句,如下:

<bean id="flag">  
   <constructor-arg value="#{systemProperties[‘system.propery.flag‘] ?: false }" />  
</bean>  
<bean id="testBean">  
    <property name="prop" value="#{ flag ? ‘yes‘ : ‘no‘ }"/>  
</bean>  

 testBean的prop动态依赖于flag的值。

2、使用Profile

<!-- 如果没有设置profile,default.xml将被加载 -->  
<!-- 必须放置在配置文件的最底下,后面再也没有bean的定义 -->  
<beans profile="default">  
     <import resource="classpath:default.xml" />  
</beans>  
<!-- some other profile -->  
<beans profile="otherProfile">  
    <import resource="classpath:other-profile.xml" />  
</beans> 

3、使用@Conditional

官方文档定义:“Indicates that a component is only eligible for registration when all specified conditions match”,意思是只有满足一些列条件之后创建一个bean

@Conditional定义

@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.TYPE, ElementType.METHOD)  
public @interface Conditional{  
    Class<? extends Condition>[] value();
}  

@Conditional注解主要用在以下位置:

  • 类级别可以放在注标识有@Component(包含@Configuration)的类上
  • 作为一个meta-annotation,组成自定义注解
  • 方法级别可以放在标识由@Bean的方法上

如果一个@Configuration的类标记了@Conditional,所有标识了@Bean的方法和@Import注解导入的相关类将遵从这些条件。

 

condition接口定义如下:

public interface Condition {

    /**
     * Determine if the condition matches.
     * @param context the condition context
     * @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
     * or {@link org.springframework.core.type.MethodMetadata method} being checked.
     * @return {@code true} if the condition matches and the component can be registered
     * or {@code false} to veto registration.
     */
    boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);

}

 

 

下面看一个例子:

Java代码  技术分享
  1. import org.springframework.context.annotation.Condition;  
  2. import org.springframework.context.annotation.ConditionContext;  
  3. import org.springframework.core.type.AnnotatedTypeMetadata;  
  4.    
  5. public class LinuxCondition implements Condition{  
  6.    
  7.   @Override  
  8.   public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {  
  9.     return context.getEnvironment().getProperty("os.name").contains("Linux");  }  
  10. }  

 

Java代码  技术分享
  1. import org.springframework.context.annotation.Condition;   
  2. import org.springframework.context.annotation.ConditionContext;   
  3. import org.springframework.core.type.AnnotatedTypeMetadata;   
  4.    
  5. public class WindowsCondition implements Condition{  
  6.    
  7.   @Override   
  8.   public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {  
  9.     return context.getEnvironment().getProperty("os.name").contains("Windows");  
  10.   }  
  11. }  

 

我们有两个类LinuxCondition 和WindowsCondition 。两个类都实现了Condtin接口,重载的方法返回一个基于操作系统类型的布尔值。

 

下面我们定义两个bean,一个符合条件另外一个不符合条件:

Java代码  技术分享
  1. import org.springframework.context.annotation.Bean;  
  2. import org.springframework.context.annotation.Conditional;  
  3. import org.springframework.context.annotation.Configuration;  
  4.    
  5. @Configuration  
  6. public class MyConfiguration {  
  7.    
  8.   @Bean(name="emailerService")  
  9.   @Conditional(WindowsCondition.class)  
  10.   public EmailService windowsEmailerService(){  
  11.       return new WindowsEmailService();  
  12.   }  
  13.    
  14.   @Bean(name="emailerService")  
  15.   @Conditional(LinuxCondition.class)  
  16.   public EmailService linuxEmailerService(){  
  17.     return new LinuxEmailService();  
  18.   }  
  19. }  

 

 当符合某一个条件的时候,这里的@Bean才会被初始化。 

Spring4.0系列5-@Conditional

标签:布尔   metadata   meta   star   rri   配置   some   .com   param   

原文地址:http://www.cnblogs.com/duanxz/p/7489033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!