springboot中使用@Configuration注解完成一个配置类的配置,被该注解标注的类会在程序启动时自动的加载到IOC容器,但如果我们想通过一些条件来控制被@Configuraion注解标注的配置类的生效与否,可以使用springboot提供的@Conditional注解来对条件进行控制,通过该注解指定哪些条件下才能自动加载配置,如果不满足条件,则不加载。以下是常见的@Conditional注解的类型:
1、@ConditionalOnClass
条件是:类路径下,如果存在指定的类,则该配置类生效。类路径即classpath,也就是src路径和pom引入的依赖External Library路径下
@ConditionalOnClass({EnableWebSecurity.class})
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnClassCondition.class})
public @interface ConditionalOnClass {
Class<?>[] value() default {};
String[] name() default {};
}
2、@ConditionalOnMissingClass
条件是:类路径下,如果不存在指定的类,则该配置类生效
@ConditionalOnMissingClass("org.springframework.cloud.gateway.config.GatewayAutoConfiguration")
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnClassCondition.class})
public @interface ConditionalOnMissingClass {
String[] value() default {};
}
3、@ConditionalOnBean
条件是:如果当前应用IOC容器存在指定的Bean,则该配置类生效。
@ConditionalOnBean(ResourceServerConfiguration.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnBeanCondition.class})
public @interface ConditionalOnBean {
Class<?>[] value() default {};
String[] type() default {};
Class<? extends Annotation>[] annotation() default {};
String[] name() default {};
SearchStrategy search() default SearchStrategy.ALL;
Class<?>[] parameterizedContainer() default {};
}
4、@ConditionalOnMissingBean
条件是:如果当前应用IOC容器不存在指定的Bean,则该配置类生效。
@ConditionalOnMissingBean(
name = {"springSecurityFilterChain"}
)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnBeanCondition.class})
public @interface ConditionalOnMissingBean {
Class<?>[] value() default {};
String[] type() default {};
Class<?>[] ignored() default {};
String[] ignoredType() default {};
Class<? extends Annotation>[] annotation() default {};
String[] name() default {};
SearchStrategy search() default SearchStrategy.ALL;
Class<?>[] parameterizedContainer() default {};
}
5、@ConditionalOnProperty
条件是:如果当前应用的配置文件中存在指定的属性配置,则该配置类生效。
@ConditionalOnProperty(
prefix = "spring.aop",
name = {"proxy-target-class"},
havingValue = "true",
matchIfMissing = true
)
@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; // 如果该属性配置不存在是否生效,false不生效,true生效
}
6、@ConditionalOnWebApplication
条件是:如果当前应用是web应用时,则该配置类生效。
@ConditionalOnWebApplication(type = SERVLET)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional({OnWebApplicationCondition.class})
public @interface ConditionalOnWebApplication {
ConditionalOnWebApplication.Type type() default ConditionalOnWebApplication.Type.ANY;
public static enum Type {
ANY, // 任何web环境
SERVLET, // servlet环境
REACTIVE; // reactive环境
private Type() {
}
}
}
7、@ConditionalOnExpression
条件是:如果满足指定表达式时,则该配置类生效。
@ConditionalOnExpression("!'${security.oauth2.client.clientId}'.isEmpty()")
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({OnExpressionCondition.class})
public @interface ConditionalOnExpression {
String value() default "true";
}