应用:根据配置文件的属性决定是否将类注册为bean
相关类与注解
@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;
}
说明:value与name等效,同时配置时,优先使用value;
匹配的key:prefix+"."+name,
若含有key,且设置了havingValue,若key对应的value=havingvalue,则匹配,若value与havingValue不等,则不匹配
若含有key,且没有设置havingValue,若key对应的value不为false,则匹配,若value为false,则不匹配
如不包含key,且设置了matchingIfMissing=true,则匹配,若matchingIfMissing=false,则不匹配
@Conditional
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
Class extends Condition>[] value(); //继承condition的类数组
}
Condition
@FunctionalInterface
public interface Condition {
boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}
OnPropertyCondition
@Order(-2147483608)
class OnPropertyCondition extends SpringBootCondition {
OnPropertyCondition() {
}
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
//获取匹配结果
List allAnnotationAttributes = this.annotationAttributesFromMultiValueMap(metadata.getAllAnnotationAttributes(ConditionalOnProperty.class.getName()));
List noMatch = new ArrayList();
List match = new ArrayList();
Iterator var6 = allAnnotationAttributes.iterator();
while(var6.hasNext()) {
AnnotationAttributes annotationAttributes = (AnnotationAttributes)var6.next();
ConditionOutcome outcome = this.determineOutcome(annotationAttributes, context.getEnvironment());
(outcome.isMatch() ? match : noMatch).add(outcome.getConditionMessage());
}
if (!noMatch.isEmpty()) {
return ConditionOutcome.noMatch(ConditionMessage.of(noMatch));
} else {
return ConditionOutcome.match(ConditionMessage.of(match));
}
}
private List annotationAttributesFromMultiValueMap(MultiValueMap multiValueMap) {
//将属性转换为List
List
示例
*******************
配置文件
application.yml
person:
enabled: true
name: 瓜田李下
age: 20
student:
name: 瓜田李下2
age: 20
*******************
pojo 层
Person
@Data
@Component
@ConditionalOnProperty(prefix = "person",value = {"enabled"},havingValue = "true")
@ConfigurationProperties("person")
public class Person {
private String name;
private Integer age;
}
Student
@Data
@Component
@ConfigurationProperties(prefix = "student")
@ConditionalOnProperty(prefix = "student",value = "enabled",matchIfMissing = true)
public class Student {
private String name;
private Integer age;
}
*******************
controller 层
HelloController
@RestController
public class HelloController {
@Resource
private Person person;
@Resource
private Student student;
@RequestMapping("/hello")
public String hello(){
System.out.println(person);
System.out.println(student);
return "success";
}
}
使用测试
localhost:8080/hello
2020-07-06 18:27:20.222 INFO 19064 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-07-06 18:27:20.228 INFO 19064 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
Person(name=瓜田李下, age=20)
Student(name=瓜田李下2, age=20)