当前位置: 代码迷 >> 综合 >> SpringBoot:@Conditional注解集合
  详细解决方案

SpringBoot:@Conditional注解集合

热度:28   发布时间:2023-09-18 15:46:33.0
ConditionalOnBean:当容器里有指定的bean的条件下
ConditionalOnClass:当类路径下有指定的类的条件下
ConditionalOnExpression: 基于SpEL表达式作为判断条件
ConditionalOnJava:基于java版本作为判断条件
ConditionalOnJndi:在JNDI存在的条件下查找指定的位置
ConditionalOnMissingBean :当容器里没有指定的bean的情况下
ConditionalOnMissingClass: 当容器没有指定的类的情况下
ConditionalOnNoWenApplication:当前项目不是web项目的条件下
ConditionalOnProperty:指定的属性是否有指定的值
ConditionalOnResource:类路径是否有指定的值
ConditionalOnSingleCandidate:当指定的bean在容器中只有一个,或者虽然有多个但是指定首选bean
ConditionalOnWebApplication:当前项目是web项目的条件下

自动配置类必须在一定条件下才会生效。都通过@Conditional注解标签来配置实现。

我们可以通过启用配置属性debug=true,来使项目启动时,自动在控制台输出自动匹配报告。

Positive matches:(启用的自动配置类)
-----------------CodecsAutoConfiguration matched:- @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition)CodecsAutoConfiguration.JacksonCodecConfiguration matched:- @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper' (OnClassCondition)CodecsAutoConfiguration.JacksonCodecConfiguration#jacksonCodecCustomizer matched:- @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean 'jacksonObjectMapper' (OnBeanCondition)DispatcherServletAutoConfiguration matched:- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)- found 'session' scope (OnWebApplicationCondition)
.....Negative matches:(没有启用的自动配置类)
-----------------ActiveMQAutoConfiguration:Did not match:- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)AopAutoConfiguration:Did not match:- @ConditionalOnClass did not find required class 'org.aspectj.lang.annotation.Aspect' (OnClassCondition)ArtemisAutoConfiguration:Did not match:- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
..........