Spring示例讲解条件注入方法
2022-06-21 14:15:19 来源:易采站长站 作者:
目录
简介@Component+条件注解@Configuration+@Bean+条件注解@Configuration+条件注解+@Bean自定义Condition简介
说明
本文用实例介绍Spring的条件注入的用法。
@Component、@Configuration+@Bean都可以与条件注入的注解结合。
@Component+条件注解
Bean
package com.example.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; @Component @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true") public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
application.yml
custom:
myComponent:
enabled: true
运行结果:
[MyComponent#MyComponent]
若将application.yml的custom.myComponent.enabled去掉,或者设置为非true值,则不会输出上边的运行结果。
@Configuration+@Bean+条件注解
Bean
package com.example.config; public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
配置类
package com.example.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true") public MyComponent getMyComponent() { return new MyComponent(); } }
application.yml
custom:
myComponent:
enabled: true
运行结果:
[MyComponent#MyComponent]
若将application.yml的custom.myComponent.enabled去掉,或者设置为非true值,则不会输出上边的运行结果。
@Configuration+条件注解+@Bean
Bean
package com.example.config; public class MyComponent { public MyComponent() { System.out.println("[MyComponent#MyComponent]"); } }
配置类
package com.example.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true") public class MyConfig { @Bean public MyComponent getMyComponent() { return new MyComponent(); } }
application.yml
custom:
myComponent:
enabled: true
运行结果:
[MyComponent#MyComponent]
若将application.yml的custom.myComponent.enabled去掉,或者设置为非true值,则不会输出上边的运行结果。
自定义Condition
自定义的condition的matches方法返回值为true时,才会创建bean。
条件类
//判断当前系统是否是Mac
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class MyCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return conditionContext.getEnvironment().getProperty("os.name").contains("Mac"); } }
@Configuration public class Config { @Conditional(MyCondition.class) @Bean public String condition() { System.err.println("This is mac"); return ""; } }
到此这篇关于Spring示例讲解条件注入方法的文章就介绍到这了,更多相关Spring条件注入内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!
如有侵权,请联系QQ:279390809 电话:15144810328
最新图文推荐
相关文章
-
Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法
什么是SkyWalking 查看官网https://skywalking.apache.org/ 分布式系统的应用程序性能监视工具,专为微服务、云原生架构和基于容器(Docker、K8s、Mesos)架构而设计。 安装 进入下载页面https://2020-06-18
-
成功解决IDEA2020 Plugins 连不上、打不开的方法
IntelliJ IDEA 2020.1 插件中心一直打不开,鉴于有部分同学反馈设置http proxy不能解决,所以可按以下顺序检查 一、设置 http proxy—勾上Auto-detect proxy setting,参照下图,加上地址 http://127.0.02020-06-25
-
IDEA2020 1.1中Plugins加载不出来的问题及解决方法
进入File-Setting 如图,取消勾选,点击确认后重启,点击了以后等一会就可以正常显示 ps:下面看下解决IDEA 2020.1.1 找不到程序包和符号 问题描述 IDEA 2020.1.1 maven项目build的时候报错,找2020-06-28
-
springboot + rabbitmq 如何实现消息确认机制(踩坑经验)
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 最近部门号召大伙多组织一些技术分享会,说是要活跃公司的技术氛围,但早就看穿一切的我知道,这 T M 就是为了刷2020-07-01
-
JetBrains IntelliJ IDEA 2020安装与使用教程详解
对于JetBrains IntelliJ IDEA 2020的认识 IntelliJ IDEA 2020是一款JAVA编程软件,捷克IntelliJ公司研发推出。该软件提供了一个非常强大的JAVA集成开发环境,不仅添加了对Records的完整代码洞察支持,2020-06-28