从AopAutoConfiguration看@ConditionalOnProperty
package org.springframework.boot.autoconfigure.aop;
// 忽略 import 行
/**
* org.springframework.boot.autoconfigure.EnableAutoConfiguration
* Auto-configuration for Spring's AOP support. Equivalent to enabling
* org.springframework.context.annotation.EnableAspectJAutoProxy in your
* configuration.
*
* The configuration will not be activated if spring.aop.auto=false. The
* proxyTargetClass attribute will be true, by default, but can be
* overridden by specifying spring.aop.proxy-target-class=false.
*
*/
@Configuration
// 仅在这些类存在于 classpath 时生效
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
AnnotatedElement.class })
// 仅在属性 spring.aop.auto 缺失或者明确指定为 true 时生效
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
// 在配置参数 spring.aop.proxy-target-class 值被明确设置为 false 时生效
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class",
havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
// 在配置参数 spring.aop.proxy-target-class 缺失或者值被明确设置为 true 时生效
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class",
havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration {
}
}
主要是havingValue和matchIfMissing容易混淆
- matchIfMissing
matchIfMissing 默认为false,当设置为true的时候代表,prefix和name缺失的时候条件表达式为true
- havingValue
设置的值需要,需被明确匹配