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 注解的更多相关文章

  1. Spring基于注解的Cache支持

    Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...

  2. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用(转)

    原文地址:https://www.cnblogs.com/fashflying/p/6908028.html 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对 ...

  3. Spring 缓存注解之@Cacheable,@CacheEvit

    Spring引入了Cache的支持,其使用方法类似于Spring对事务的支持.Spring Cache是作用于方法上的,其核心思想是,当我们调用一个缓存方法时,把该方法参数和返回结果作为一个键值对存放 ...

  4. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  5. Spring缓存注解

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  6. Spring框架系列(七)--Spring常用注解

    Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...

  7. Spring常用注解式开发

    1.组件注册@Configuration.@Bean给容器中注册组件. 注解,@Configuration告诉Spring这是一个配置类,相当于bean.xml配置文件. 注解,@Bean给Sprin ...

  8. Spring走向注解驱动编程

    SpringFramework的两大核心,IOC(Inversion of control)控制反转和DI(Dependency Inject)依赖注入,其推崇的理念是应用系统不应以java代码的方式 ...

  9. Spring 缓存注解解析过程

    Spring 缓存注解解析过程 通过 SpringCacheAnnotationParser 的 parseCacheAnnotations 方法解析指定方法或类上的缓存注解, @Cacheable ...

随机推荐

  1. C#的7个原则

    C#的七个原则如下: 1.单一职责原则(Single Responsibility Principle, SRP):一个类只负责一个功能领域中的相应职责. 2.开闭原则(Open-Closed Pri ...

  2. android中工作线程安全

    当应用程序启动,创建了一个叫“main”的线程,用于管理UI相关,又叫UI线程.其他线程叫工作线程(Work Thread). Single Thread Model 一个组件的创建并不会新建一个线程 ...

  3. DevOps is dirty work - CI drives you crazy

    一直很想谈谈Continuous Integration(CI),持续集成. 就在不久前一次朋友聚会上,一个刚刚跳槽到一家创业公司的朋友跟我抱怨说他们没有CI,没有code review,要做点事太累 ...

  4. C语言

    HTML的学习早已落下帷幕,我们已经进入了C语言的学习,这段时间时间主要学了运算符.表达式.循环语句以及数组和字符串,感觉到了一种朦朦胧胧懂得尴尬. 运算符主要包括:算术运算符.赋值运算符.关系运算符 ...

  5. 在MVC架构中使用CodeSmith生成NHibernate映射对象和实体类

    第一步:找到生成模板,如下图 第二步:配置数据库连接(如下图),然后右击第一步找到的模板,点击Excute 第三步:执行操做(如下图) 第四步: 找到之前配置生成的文件夹,找到如下文件(图中标记的文件 ...

  6. js 获取指定日期

    查询几天后的js代码,如果查询当天的日期 if($("input[name='startTime']").val()==""){ $("input[n ...

  7. 刷新本地的DNS缓存数据

    ipconfig /flushdns”执行,刷新本地的DNS缓存数据. ipconfig /displaydns      查看本地DNS缓存记录的命令为:ipconfig /displaydns.你 ...

  8. 在SQL语句中加入时间比较作为查询条件

    select * from 表名 where 列名 = ? and DATEDIFF(hh,时间列,'2016-08-22 15:05:59.000')<9

  9. 安装运行Hadoop

    1 准备环境 1.1 Ubuntu 或者 VMware Workstation Pro+Ubuntu 1.2 Jdk 1.3 eclipse 或其他开发工具(可选) 2 安装Hadoop 2.1 从h ...

  10. 简单的Markdown功能实现——marked模块的使用

    marked是一个markdown 解析.编译器,通过引入marked模块,可以实现一个简单的markdown编辑器.使用方式如下: Install 新建一个项目文件夹,在项目中下载marked模块 ...