当前位置:网站首页>@ConditionalOnProperty的简单理解与使用
@ConditionalOnProperty的简单理解与使用
2022-07-16 10:05:00 【bulingbuling^_^】
@ConditionalOnProperty注解控制组件是否注入spring的注解
@Configuration
@Slf4j
@EnableScheduling
//@Conditional(CheckEnvironmentCondition.class)
@ConditionalOnProperty(prefix = "demo", name = "conditional", havingValue = "true", matchIfMissing = true)
public class ConditionalConfigTest {
@Scheduled(cron = "*/5 * * * * ?")
private void updateJob(){
System.out.println("ConditionalOnProperty---Test:"+ LocalDateTime.now());
}
}配置参数匹配,成功注入

各属性作用 源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
/**
* Alias for {@link #name()}.
* @return the names
*/
String[] value() default {};
/**
* A prefix that should be applied to each property. The prefix automatically ends
* with a dot if not specified. A valid prefix is defined by one or more words
* separated with dots (e.g. {@code "acme.system.feature"}).
* @return the prefix
*/
String prefix() default "";
/**
* The name of the properties to test. If a prefix has been defined, it is applied to
* compute the full key of each property. For instance if the prefix is
* {@code app.config} and one value is {@code my-value}, the full key would be
* {@code app.config.my-value}
* <p>
* Use the dashed notation to specify each property, that is all lower case with a "-"
* to separate words (e.g. {@code my-long-property}).
* @return the names
*/
String[] name() default {};
/**
* The string representation of the expected value for the properties. If not
* specified, the property must <strong>not</strong> be equal to {@code false}.
* @return the expected value
*/
String havingValue() default "";
/**
* Specify if the condition should match if the property is not set. Defaults to
* {@code false}.
* @return if the condition should match if the property is missing
*/
boolean matchIfMissing() default false;
}havingValue:定义的值是否相等,决定是否注入。
matchIfMissing:如果没有设置这个属性,是否需要havingValue去匹配。
百度后,对于这两个组合使用时,解释的很绕,简单说来:
matchIfMissing为true 比较对象为null时,会注入。
一起使用时,以havingValue为准。
边栏推荐
猜你喜欢

模拟功放75610诊断功能的调试

Hcip ppp/hdlc, gre/mgre experiments

查找——二叉排序树(二)

解决问题的最佳实践路径

The professional standard of the new profession "database operation administrator" was launched and developed

OneFlow源码一览:GDB编译调试

JMeter如何使用MD5加密并且对body进行指纹签名

5 data packets and LSA information of OSPF

学习笔记——直流电机调速器

Atcoder beginer contest 259 partial solution
随机推荐
[vsctf2022]web topic recurrence
学习笔记——直流电机调速器
综合行政执法改革依据
Logback different packages (business logs) are output to different log files
Database daily question --- day 24: format department table
精度提升方法:自适应Tokens的高效视觉Transformer框架(已开源)
【刷题记录】12. 整数转罗马数字
Upgrade mysql5.5.27 to 5.7.35 under Windows Environment
Basis for comprehensive administrative law enforcement reform
[record of question brushing] 12 Integer to Roman numeral
Sword finger offer 09 Implementing queues with two stacks
Search - binary sort tree (2)
【分享】二层交换和三层交换转发
【生物信息】影像组学入门实践成长营
mysql进阶(四)聊聊mysql中的事务锁机制
安装无线网卡驱动
Logstash filtering rules for elk cluster deployment (9)
Debugging of diagnostic function of analog power amplifier 75610
[tensorflow2.9] Titanic survival prediction - structured data modeling process
行政处罚法 例外规定