通过条件注解@Conditional细粒度的选择bean实例
在进行spring进行开发时,当某个接口有多种实现方式并且我们只想让一种生效时,比如声明如下一个接口和两个实现:
public interface LanggageService {
String say();
}
public class ChineseServiceImpl implements LanggageService {
@Override
public String say() {
return "你好";
}
}
public class EnglishServiceImpl implements LanggageService {
@Override
public String say() {
return "hello";
}
}
我们通常使用xml为某个接口配置实现类;
<bean id="languageService" class="com.liam.service.ChineseServiceImpl"></bean>
当然后来我们习惯使用注解的方式了,比如
@Bean
public LanggageService englishService(){
return new EnglishServiceImpl();
}
当然也可以通过@Component进行扫描注入,这也是为了去除java令人发指的诸多xml配置的一项进步,所以当使用spring boot进行开发时,我们尽量摒弃了诸多配置,不管是Hibernate,JPA亦或者是mybatis...
但是也显然,有时候我们需要通过配置来选择我们使用的bean,基于xml还好,但是基于注解的话通过重新编译打包显然是不能接受的。。比如测试环境我们设置的数据源是mysql,生产环境换成oracle了,当然我们通常使用的是profile进行配置,但还是不够灵活,我们需要更细粒度的管理bean..所以也就有了Conditional。。。(废话连篇。。。)
比如我们在spring boot中要通过application.properties中进行配置来选择我们使用的bean
say.method=chinese
则可以声明两个Condition
public class SayChineseCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
String m= conditionContext.getEnvironment().getProperty("say.method").toString();
boolean isChi= "chinese".equals(m);
System.err.println("current chinese "+ isChi);
return isChi;
}
}
public class SayEnglishCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
String m= conditionContext.getEnvironment().getProperty("say.method").toString();
boolean isEn= "english"==m;
System.err.println("current english "+ isEn);
return isEn;
}
}
然后实现类中配置上对应的Conditional:
@Service
@Conditional(SayChineseCondition.class)
public class ChineseServiceImpl implements LanggageService {
@Override
public String say() {
return "你好";
}
}
简单,灵活。
此处需要注意的是application.properties在spring boot中属于环境变量级别的配置加载,所以优先级较高,假如通过其他配置文件进行加载时借助PropertySource或者ConfigurationProperties之类的是不能如愿的,因为加载Condition时,我们的配置bean尚未加载,比如下面的方式是无效的。
@PropertySource(value = "first.properties")
public class SayEnglishCondition implements Condition {
@Value("${say.method}")
private String lang;
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
String m=lang;// conditionContext.getEnvironment().getProperty("say.method").toString();
boolean isEn= "english".equals(m);
System.err.println("current english "+ isEn);
return isEn;
}
}
所以。。我们只能手动加载了。。
public class SayEnglishCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Properties properties = new Properties();
try {
properties.load(conditionContext.getResourceLoader().getResource("first.properties").getInputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
boolean isEn = "english".equals(properties.getProperty("say.method"));
System.err.println("current english " + isEn);
return isEn;
}
}
通过条件注解@Conditional细粒度的选择bean实例的更多相关文章
- [读书笔记] 二、条件注解@Conditional,组合注解,元注解
一.条件注解@Conditional,组合注解,元注解 1. @Conditional:满足特定条件创建一个Bean,SpringBoot就是利用这个特性进行自动配置的. 例子: 首先,两个Condi ...
- Spring Boot实战笔记(八)-- Spring高级话题(条件注解@Conditional)
一.条件注解@Conditional 在之前的学习中,通过活动的profile,我们可以获得不同的Bean.Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Conditional注解 ...
- Spring_总结_04_高级配置(二)_条件注解@Conditional
一.前言 本文承接上一节:Spring_总结_04_高级配置(一)之Profile 在上一节,我们了解到 Profile 为不同环境下使用不同的配置提供了支持,那么Profile到底是如何实现的呢?其 ...
- 条件注解@Conditional
通过活动的profile,可以获得不同的Bean.Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Conditonal注解 @Conditional根据满足某一个特定条件创建一个特定 ...
- springboot自动装配(3)---条件注解@Conditional
之前有说到springboot自动装配的时候,都是去寻找一个XXXAutoConfiguration的配置类,然而我们的springboot的spring.factories文件中有各种组件的自动装配 ...
- SpringBoot条件注解@Conditional
最近项目中使用到了关于@Conditional注解的一些特性,故写此文记录一下 @Conditional是啥呀? @Conditional注解是个什么东西呢,它可以根据代码中设置的条件装载不同的bea ...
- Spring条件注解@Conditional
@Conditional是Spring4新提供的注解,它的作用是根据某个条件创建特定的Bean,通过实现Condition接口,并重写matches接口来构造判断条件.总的来说,就是根据特定条件来控制 ...
- Spring知识点回顾(06)Profile 和 条件注解 @Conditional
1.设定环境中的active profiles 如:DispatcherServerlet的init-param spring.profiles.active=production spring.pr ...
- JavaEE开发之Spring中的条件注解组合注解与元注解
上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...
随机推荐
- 【PyQt5 学习记录】009:批量创建组件并查找
这里需要在创建组件时使用 setObjectName() 为其添加 objectName 属性,再通过 findChild() 或者 findChildren() 函数来查找. 举个栗子,批量创建10 ...
- C# 如何在Linux操作系统下读取文件
发布在Window环境上的微服务需要部署在Linux环境上,本以为没有什么问题,结果因为一处读取文件路径的原因报错了,在此记录一下两个问题:1.C#如何判断当前运行环境是什么操作系统:2.C#读取文件 ...
- sqlserver年月日转汉字大写--自定义函数--繁体
两个自定义函数结合 函数一: create function convertNumToChinese ()) ) as begin ) ' set @temStr = '壹' ' set @temSt ...
- 【Python】Java程序员学习Python(一)— 为什么学习Python
现在是6月份,毕业快3年了,虽然不能说对Java掌握的程度达到了如火纯青的地步,但是依然感觉到了瓶颈期,Java用的多了,也到了随心所欲的地步了,所以学第二门语言的想法一直在我脑海里闪现,有想法了就要 ...
- webstorm忽略node_modules目录
我在使用了cnpm后node_modules之前的层级目录变成了同一级目录,所以目录很多,造成webstorm读取时卡死. 网上大家列了各种方法,在这里我归纳一下! 先给大家看看一些相关链接. 方法1 ...
- Week8——hashcode()和equals()方法
equals()方法 Object类中的equals方法和“==”是一样的,没有区别,即俩个对象的比较是比较他们的栈内存中存储的内存地址.而String类,Integer类等等一些类,是重写了equ ...
- mybatis大于小于的转义
最近在使用mybatis,然后用到了小于等于,直接在XML中使用了<=,结果XML文件一直显示红色错误,如下: sum(case when p.pool_year <= '2014' th ...
- Jmeter 测试 JMS (Java Message Service)/ActiveMQ 性能
前言 JMS介绍:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- SQLSERVER中KeyHashValue的作用(下)
SQLSERVER中KeyHashValue的作用(下) 昨天中午跟高文佳童鞋讨论了KeyHashValue的作用,到最后还是没有讨论出结果 昨天晚上德国的兄弟傅文伟做了一下实验,将实验结果交给我 感 ...
- 将 ExpressRoute 线路从经典部署模型转移到 Resource Manager 部署模型
本文概述将 Azure ExpressRoute 线路从经典部署模型转移到 Azure Resource Manager 部署模型的效果. Azure 当前使用两种部署模型:Resource Mana ...