spring @condition 注解
spring @condition注解是用来在不同条件下注入不同实现的
demo如下:
package com.foreveross.service.weixin.test.condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; /**
*
* windows下的环境
*
*/
public class WindowsCondition implements Condition{ @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Windows");
} }
package com.foreveross.service.weixin.test.condition; 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");
} }
package com.foreveross.service.weixin.test.condition;
public interface ListService {
String showListCmd();
}
package com.foreveross.service.weixin.test.condition; import org.springframework.stereotype.Service; @Service
public class LinuxService implements ListService { public String showListCmd(){
return "ls";
} }
package com.foreveross.service.weixin.test.condition; import org.springframework.stereotype.Service; @Service
public class WindowsService implements ListService{ public String showListCmd(){
return "dir";
} }
package com.foreveross.service.weixin.test.condition; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyConfiguration { @Bean(name = "service")
@Conditional(WindowsCondition.class)
public ListService windowsService() {
return new WindowsService();
} @Bean(name = "service")
@Conditional(LinuxCondition.class)
public ListService linuxEmailerService() {
return new LinuxService();
}
}
package com.foreveross.service.weixin.test.condition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MyConfiguration.class);
ListService service= context.getBean(ListService.class);
System.out.println("操作符为:"+service.showListCmd()+",系统为:"+context.getEnvironment().getProperty("os.name"));
context.close();
}
}
spring @condition 注解的更多相关文章
- Spring基于注解的Cache支持
Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用(转)
原文地址:https://www.cnblogs.com/fashflying/p/6908028.html 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对 ...
- Spring 缓存注解之@Cacheable,@CacheEvit
Spring引入了Cache的支持,其使用方法类似于Spring对事务的支持.Spring Cache是作用于方法上的,其核心思想是,当我们调用一个缓存方法时,把该方法参数和返回结果作为一个键值对存放 ...
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- Spring缓存注解
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- Spring框架系列(七)--Spring常用注解
Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...
- Spring常用注解式开发
1.组件注册@Configuration.@Bean给容器中注册组件. 注解,@Configuration告诉Spring这是一个配置类,相当于bean.xml配置文件. 注解,@Bean给Sprin ...
- Spring走向注解驱动编程
SpringFramework的两大核心,IOC(Inversion of control)控制反转和DI(Dependency Inject)依赖注入,其推崇的理念是应用系统不应以java代码的方式 ...
- Spring 缓存注解解析过程
Spring 缓存注解解析过程 通过 SpringCacheAnnotationParser 的 parseCacheAnnotations 方法解析指定方法或类上的缓存注解, @Cacheable ...
随机推荐
- linux vps安装kloxo配置全部过程
第一步如何登录Linux VPS进行远程(SSH)管理 很多人可能用过免费虚拟主机,但绝没有用过好用的免费服务器租用,仅有的少数免费服务器都只针对有较高访问量的大站(以交换广告为条件),而普通小站是无 ...
- git 常见问题
RPC failed; error: RPC failed; curl 56 SSL read: error:00000000:lib(0):func(0):reason(0), er rno 100 ...
- maven settings.xml 阿里云
<?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...
- phpexcel 导出 科学计数问题
今天在用php做excel导出的时候遇到了一个小问题,如图 单元格默认格式为常规格式,当数值过长时就会变成科学计数. 解决方法: 如果输出的excel的$data数据是手动添加的,那就在对应值得后面添 ...
- mysql去重
select a.id,a.ssmz,(select count(ssmz) from shop_tourist_key b where b.ssmz=a.ssmz) as count ...
- Hadoop - Unable to load native-hadoop library for your platform
简介 运行hadoop或者spark(调用hdfs等)时,总出现这样的错误“Unable to load native-hadoop library for your platform”,其实是无法加 ...
- JMeter学习-037-JMeter调试工具之四-BeanShell+jmeter.log
前面三篇文章分别讲述了 HTTP Mirror Server . Debug PostProcessor 和 Debug Samper 的脚本调试实例.此文主要讲述第四种调试方法,通过 BeanShe ...
- JavaScript编码规范指南
前言 本文摘自Google JavaScript编码规范指南,截取了其中比较容易理解与遵循的点作为团队的JavaScript编码规范. JavaScript 语言规范 变量 声明变量必须加上 var ...
- javascript客户端与服务器端通信
高性能的网络通信包括以下方面:选择正确的数据格式和与之匹配的传输技术. 一.数据格式 用于传输的数据格式有: 1)html,仅适用于特定场合,传输数据量大,不过它可以节省客户端的CPU周期, 2)XM ...
- iOS计算完整文字高度(适应iOS 10)
动态计算文字的高度:(切记LineSapcing>=2,不然会显示不全) +(CGSize) boundingALLRectWithSize:(NSString*) txt Font:(UIFo ...