【Spring】Spring高级话题-条件注解-@Condition
发布日期:2021-06-29 13:38:49
浏览次数:2
分类:技术文章
本文共 3843 字,大约阅读时间需要 12 分钟。
转载请注明出处:
本文源自【】
进行本示例的演示,需要先配置好Maven和Spring哦、
见:分析
在前面的一篇博客中:
通过profile,我们可以获得不同的profile,我们可以获得不同的Bean。Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Condition注解。
@Condition根据满足某一个特定条件创建一个特定的Bean。
比如说,当某一个jar包在一个类路径下的时候,自动配置一个或多个Bean;或者只有某个Bean被创建才会创建另外一个Bean。总的来说,就是根据特定条件来控制Bean的创建行为,这样我们可以利用这个特性来进行一些自动的配置。
下面这个示例将以不同的操作系统来作为条件,通过实现Condition接口,并重写其matches方法来构造判断条件。
若在Windows系统下运行程序,则输出列表命令为dir;若在Linux操作系统下运行程序,则输出列表命令为ls。
示例
先需要定义判断条件
判定Windows的条件
package cn.hncu.p3.p4_conditional;import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext;import org.springframework.core.type.AnnotatedTypeMetadata;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:24. * Explain:判断Windows的条件 */public class WindowsCondition implements Condition{ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata){ return context.getEnvironment().getProperty("os.name").contains("Windows"); }}
判定Linux的条件
package cn.hncu.p3.p4_conditional;import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext;import org.springframework.core.type.AnnotatedTypeMetadata;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:28. * Explain:判定Linux的条件 */public class LinuxCondition implements Condition{ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Linux"); }}
不同系统下Bean的类
首先需要一个接口,接下来不同的Bean都需要实现这个接口。
接口
package cn.hncu.p3.p4_conditional;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:31. * Explain:接口-Bean需要实现的接口 */public interface ListService { public String showListCmd();}
Windows下所要创建的Bean的类
package cn.hncu.p3.p4_conditional;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:41. * Explain:Windows下所要创建的Bean的类 */public class WindowsListService implements ListService { @Override public String showListCmd() { return "dir"; }}
Linux下所要创建的Bean的类
package cn.hncu.p3.p4_conditional;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:42. * Explain:Linux下所要创建的Bean的类 */public class LinuxListService implements ListService { @Override public String showListCmd() { return "ls"; }}
配置类
package cn.hncu.p3.p4_conditional;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Conditional;import org.springframework.context.annotation.Configuration;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:48. * Explain:配置类 */@Configurationpublic class ConditionConfig { //matches方法返回true的,就运行哪个方法 @Bean @Conditional(WindowsCondition.class)//通过@Condition注解,符合Windows条件则实例化windowsListService public ListService windowsListService(){ return new WindowsListService(); } @Bean @Conditional(LinuxCondition.class)//通过@Condition注解,符合Linux条件则实例化linuxListService public ListService linuxListService(){ return new LinuxListService(); }}
运行
package cn.hncu.p3.p4_conditional;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/12/7. * Time: 下午 7:57. * Explain:运行类 */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class); ListService listService = context.getBean(ListService.class); System.out.println(context.getEnvironment().getProperty("os.name") +"系统下的列表命令为:" +listService.showListCmd() ); }}
运行结果
本文章由编写, 所有权利保留。
转载请注明出处:
本文源自【】
转载地址:https://chenhx.blog.csdn.net/article/details/53510156 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月25日 18时05分00秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java 父线程与子线程相互通信的方法
2019-04-29
Redis 六种淘汰策略和三种删除策略
2019-04-29
Java LinkedHashMap
2019-04-29
JPA 多线程同时对一条数据进行Update的问题
2019-04-29
JPA 多线程对数据进行更新,Update和Insert同时存在的问题
2019-04-29
Java 高性能队列Disruptor
2019-04-29
SpringBoot 使用https
2019-04-29
Java 读写锁
2019-04-29
JVM Minor GC、Full GC和Major GC
2019-04-29
SpringBoot @Scheduled 执行两次的问题
2019-04-29
tomcat配置JVM
2019-04-29
Ubuntu软件安装&卸载
2019-04-29
面试笔试易错知识点Java篇八
2019-04-29
弹性事务框架ETF4J——面向Java微服务的交易最终一致性解决方案
2019-04-29
【Scala 教程】Scala 条件与循环语句
2019-04-29
【Scala 教程】Scala 集合类型
2019-04-29
JAVA 线程同步机制 synchronized
2019-04-29
MySQL 安装教程(无脑版)
2019-04-29
IDEA 怎么删除一个Module
2019-04-29