Spring4.0系列1-新特性
Spring4.0系列2-环境搭建
Spring4.0系列3-@RestController
Spring4.0系列4-Meta Annotation(元注解)
Spring4.0系列5-@Conditional
Spring4.0系列6-Generic Qualifier(泛型限定)
Spring4.0系列7-Ordering Autowired Collections
Spring4.0系列8-Groovy DSL
Spring4.0系列9-websocket简单应用
更多正在编写中。。。
这篇文章介绍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注解主要用在以下位置:
如果一个@Configuration的类标记了@Conditional,所有标识了@Bean的方法和@Import注解导入的相关类将遵从这些条件。
condition接口定义如下:
public interface Condition{
/** Determine if the condition matches.
* @param context the condition context
* @param metadata meta-data of the {@link AnnotationMetadata class} or
* {@link Method 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, AnnotatedTypeMedata metadata);
}
下面看一个例子:
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class LinuxCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Linux"); }
}
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class WindowsCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
}
}
我们有两个类LinuxCondition 和WindowsCondition 。两个类都实现了Condtin接口,重载的方法返回一个基于操作系统类型的布尔值。
下面我们定义两个bean,一个符合条件另外一个不符合条件:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfiguration {
@Bean(name="emailerService")
@Conditional(WindowsCondition.class)
public EmailService windowsEmailerService(){
return new WindowsEmailService();
}
@Bean(name="emailerService")
@Conditional(LinuxCondition.class)
public EmailService linuxEmailerService(){
return new LinuxEmailService();
}
}
当符合某一个条件的时候,这里的@Bean才会被初始化。
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
分享到:
相关推荐
Spring In Action-3.2@Conditional条件化Bean,Spring In Action-3.2@Conditional条件化Bean,Spring In Action-3.2@Conditional条件化Bean
Spring Boot的强大之处在于使用了Spring 4框架的新特性:@Conditional注释,此注释使得只有在特定条件满足...下面这篇文章主要给大家介绍了关于Spring4.0中新功能:@Conditional注解的相关资料,需要的朋友可以参考下。
spring里@Conditional注解使用示例代码,具体使用参考博客地址:http://791202.com/2019/11/10/spring/63/
SpringBoot中使用@Conditional示例代码,使用 @Conditional 注解,根据是否满足指定的条件来决定是否装配 Bean
Spring之条件注解@Conditional,条件(系统)不同注入的对象也不同。演示用例
自定义@Conditional 注释示例什么是@Conditional 是添加到 Spring 4 的注解,用于指定处理特定配置或将特定 bean 注册到 Spring 上下文的条件。为什么重要Spring Boot 项目自动配置支持依赖于 @Conditional - 它有...
如何根据不同的条件来判定到底注入那个Bean,即@Conditional注解的用法实例代码
Zhu, S. , & Fukushima, M. (2009). Worst-case conditional value-at-risk with applica- tion to robust portfolio management. Operations Research, 57 (5), 1155–1168 .
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
SpringBoot2的最佳实践课程的技术含量几乎是近期SpringBoot2相关课程的天花板。课程内容以时下最新的技术点为主线,逐步的进行展开,分别细化的讲解了SpringBoot2基础课程...├─10、底层注解-@Conditional条件装配.mp
主要介绍了Spring @Conditional注解讲解及示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Spring @Conditional注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
离线安装包,亲测可用
主要给大家介绍了关于Spring条件注解@Conditional的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
Li-StoryGAN-A-Sequential-Conditional-GAN-for-Story-Visualization
主要介绍了浅谈SpringBoot中的@Conditional注解的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
离线安装包,亲测可用
条件注解@Conditional 组合注解与元注解 @Enable*注解的工作原理 直接导入配置类 依据条件选择配置类 动态注册Bean 测试 Spring TestContext Framework集成测试 SpringMVC基础 Spring MVC...
主要给大家介绍了关于Spring Boot @Conditional注解用法的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
个人应对js-conditional-compile-loader官方依赖包升级导致的bug而出的包