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. spring的显示装配bean(1)------通过XML文件装配

    1:spring环境的简单搭建 (1)导入spring相关的jar包. 2:准备要进行装配的Java类 这里给出两个举例类 (1) (2) 3:配置XML文件 (1)在配置文件的顶部声明多个XML模式 ...

  2. ccpcfinal总结

    ccpcfinal拿了块铁回来了... 虽然一开始知道我们三个过去 应该就是铁了.. 然而之后训练了几场区域赛的题 感觉 还是有希望拿个铜的... 不过..当出发前听说一共有120个队之后..不过我觉 ...

  3. Model & ModelMap & ModelAndView 比较ModelFactory简介

    Model: 是一个接口,其实现类必继承ModelMap. ModelMap: 继承与LinkedHashMap,相当于自定义了一个map. ModelAndView: 里面封装了两个对象,其中vie ...

  4. 再谈java两种变量(基本类型和引用类型)(综合各路大神)

    基本类型: 基本类型自然不用说了,它的值就是一个数字,一个字符或一个布尔值. int  a:   a=250: //声明变量a的同时,系统给a分配了数据空间. 引用类型: 是一个对象类型,值是什么呢? ...

  5. MySQL触发器使用详解

    MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器在MySQL中,创建触发器语法如下: ...

  6. Spark 宏观架构&执行步骤

    Spark 使用主从架构,有一个中心协调器和许多分布式worker. 中心协调器被称为driver.Driver 和被称为executor 的大量分布式worker 通信 Driver 运行在它自己的 ...

  7. c# json TO xml

    using System.IO;using System.Text;using System.Xml.Serialization;using System.Xml;using System.Runti ...

  8. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  9. linux命令日常总结

    1.date 显示系统日期2. mkdir xx 创建xx目录 rmdir xx 删除xx目录(空目录) rm -rf xx 删除xx目录(非空目录) 3. vi xx 创建某文件 写入-->e ...

  10. 【转载】JMeter学习(三十六)发送HTTPS请求

    Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程中需要进行登录操作,而登录请求是HTTPS协议.这就需要对jmeter进行设置. ...