一、條件注解分類
常見的@ConditionalOnxxx
開頭的注解我們稱之為條件注解,常見的條件注解有
- class條件注解:@ConditionalOnClass
- bean條件注解:@ConditionalOnBean
- 屬性條件注解:@ConditionalOnProperty
- …
@ConditionalOnProperty:如果有指定的配置,條件生效;
@ConditionalOnBean:如果有指定的Bean,條件生效;
@ConditionalOnMissingBean:如果沒有指定的Bean,條件生效;
@ConditionalOnMissingClass:如果沒有指定的Class,條件生效;
@ConditionalOnWebApplication:在Web環境中條件生效;
@ConditionalOnExpression:根據表達式判斷條件是否生效。
這幾個注解通常會結合使用,一般都是在配置類中使用,SpringBoot各種xxxxAutoCconfiguration
都用到了這些注解,這也是SpringBoot自動裝配的重要工具。
二、@ConditionalOnProperty注解
簡單來講,一般是在配置類上或者是@Bean修飾的方法上,添加此注解表示一個類是否要被Spring上下文加載,若滿足條件則加載,若不滿足條件則不加載。
我們在application.properties中配置的各種配置,添加配置之后即生效,就是這么控制的。
prefix:配置文件中的前綴。
name:配置的名字。
havingValue:它的值與配置文件的值對比,當兩個值相同,類會被加載到spring的IOC容器中。
matchIfMissing:缺少該配置時是否可以加載,如果為true,沒有該配置屬性時也會正常加載,反之則不會生效。
@ConditionalOnProperty源碼
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented @Conditional({OnPropertyCondition.class}) public @interface ConditionalOnProperty { String[] value() default {}; String prefix() default ""; String[] name() default {}; String havingValue() default "";
boolean matchIfMissing() default false; }
首先看matchIfMissing屬性,用來指定如果配置文件中未進行對應屬性配置時的默認處理:默認情況下matchIfMissing為false,也就是說如果未進行屬性配置,則自動配置不生效。如果matchIfMissing為true,則表示如果沒有對應的屬性配置,則自動配置默認生效。
如:http編碼的自動配置類中,當配置文件中沒有配置spring.http.encoding.enabled,自動配置仍然會生效。
@Configuration @EnableConfigurationProperties({HttpProperties.class}) @ConditionalOnWebApplication( type = Type.SERVLET ) @ConditionalOnClass({CharacterEncodingFilter.class}) @ConditionalOnProperty( prefix = "spring.http.encoding", value = {"enabled"}, matchIfMissing = true ) public class HttpEncodingAutoConfiguration {...}
三、使用@ConditionalOnProperty注解
@ConditionalOnProperty注解是控制被注解的類中其他注解(@Component和@Configuration兩個注解)是否生效。
1、在配置類中控制@Configuration是否生效
在spring boot中有時候需要控制配置類中的Bean是否生效,可以使用@ConditionalOnProperty注解來控制@Configuration是否生效.
@Configuration被注解的類內部包含有一個或多個被@Bean注解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。
@Configuration @ConditionalOnProperty(prefix = "filter",name = "loginFilter",havingValue = "true") public class FilterConfig { @Bean public FilterRegistrationBean getFilterRegistration() { FilterRegistrationBean filterRegistration = new FilterRegistrationBean(new LoginFilter()); filterRegistration.addUrlPatterns("/*"); return filterRegistration; } }
配置文件中代碼
filter.loginFilter=true
通過@ConditionalOnProperty控制配置類是否生效,可以將配置與代碼進行分離,實現了更好的控制配置。
@ConditionalOnProperty實現是通過havingValue與配置文件中的值對比,相同則配置類生效,反之失效。
2、在切面類中控制@Component是否生效
@Slf4j @Aspect @Component @ConditionalOnProperty(prefix = "system.log", name = "enabled", havingValue = "true") public class LogAspect { //成功的標志 private final static boolean SUCCESS = true; @Resource private HttpServletRequest request; @Autowired private JWTService jwtService; @Autowired private SystemLogService systemLogService; /** * 配置切點,攔截Controller中添加@Log注解的方法 */ @Pointcut("@annotation(com.ljxx.common.annotation.Log)") public void logPointCut() { } /** * 前置通知 * * @param joinPoint */ @Before("logPointCut()") public void doBeforeAdvice(JoinPoint joinPoint) { } /** * 后置通知 * * @param obj * @throws IOException */ @AfterReturning(returning = "obj", pointcut = "logPointCut()") public void doAfterReturning(JoinPoint joinPoint, Object obj) throws Exception { handleLog(joinPoint, obj, null); } /** * 異常通知 * * @param e */ @AfterThrowing(value = "logPointCut()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Exception e) throws Exception { handleLog(joinPoint, null, e); } /** * 日志處理 * * @param result * @param e */ private void handleLog(JoinPoint joinPoint, Object result, Exception e) throws Exception { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); ... systemLogService.addLog(systemLog); } /** * 截取字符串 * * @param val * @return */ private String getText(String val) { return StrUtil.sub(val, 0, 4000); } }
配置中的代碼
#開啟日志存儲 system.log.enabled=true
@ConditionalOnProperty是通過havingValue與配置文件中的值對比,如果返回為true則@Component注解生效,反之失效。
3、在配置類中控制@Component是否生效
(1)、配置文件配置
zs.name=張三 zs.age=14 config.isActive=1
(2)、配置類
@Component @ConfigurationProperties(prefix = "zs") @ConditionalOnProperty(prefix = "config",name = "isActive",havingValue = "1") @Data public class ConfigurationBean { private String name; private Integer age; }
@ConditionOnPropertiy注解的意思是:如果配置文件中config.isActive=1,則該@Component注解生效。
通過注解@ConfigurationProperties(prefix="配置文件中的key的前綴")可以將配置文件中的配置自動與實體進行映射
(3)、測試配置類的@Component注解是否生效
@RestController public class HelloController { @Autowired private ConfigurationBean config; @GetMapping("/config") private String testConfigurationProperties(){ System.out.println(config); return "SUCCESS!!!"; } }
啟動項目,瀏覽器訪問:http://localhost:8080/config,控制台打印如下:
ConfigurationBean(name=張三, age=14)
修改配置文件如下:
config.isActive=2
啟動項目,發現啟動失敗,如下所示:
即spring的IOC容器中沒有找到ConfigurationBean對象。