#bean实例化条件配置项
conditionKey=1.0
#bean实例化条件配置项
conditionKey=1.0
// 仅当conditionKey==1.0的时候实例化这个类
@Component
@ConditionalOnProperty(name = "conditionKey", havingValue = "1.0")
public class Manage1Impl implements MyManage{
@Override
public void sayHello() {
System.out.println("我是实现类01");
}
@PostConstruct
public void init() {
this.sayHello();
}
}
// 仅当conditionKey==1.0的时候实例化这个类
name = "conditionKey", havingValue = "1.0") (
public class Manage1Impl implements MyManage{
public void sayHello() {
System.out.println("我是实现类01");
}
public void init() {
this.sayHello();
}
}
package com.zxy.config;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
/**
* 自定义条件注解
* @author ZENG.XIAO.YAN
* @version 1.0
* @Date 2019-04-15
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(CustomOnPropertyCondition.class)
public @interface CustomConditionalOnProperty {
/**
* 条件变量的name
*/
String name() default "";
/**
* havingValue数组,支持or匹配
*/
String[] havingValue() default {};
}
package com.zxy.config;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
/**
* 自定义条件注解
* @author ZENG.XIAO.YAN
* @version 1.0
* @Date 2019-04-15
*/
RetentionPolicy.RUNTIME) (
ElementType.TYPE, ElementType.METHOD}) ({
CustomOnPropertyCondition.class) (
public @interface CustomConditionalOnProperty {
/**
* 条件变量的name
*/
String name() default "";
/**
* havingValue数组,支持or匹配
*/
String[] havingValue() default {};
}
package com.zxy.config;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
/**
* 自定义条件注解的验证规则
* @author ZENG.XIAO.YAN
* @version 1.0
* @Date 2019-04-15
*/
public class CustomOnPropertyCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(CustomConditionalOnProperty.class.getName());
String propertyName = (String) annotationAttributes.get("name");
String[] values = (String[]) annotationAttributes.get("havingValue");
if (0 == values.length) {
return false;
}
String propertyValue = conditionContext.getEnvironment().getProperty(propertyName);
// 有一个匹配上就ok
for (String havingValue : values) {
if (propertyValue.equalsIgnoreCase(havingValue)) {
return true;
}
}
return false;
}
}
package com.zxy.config;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
/**
* 自定义条件注解的验证规则
* @author ZENG.XIAO.YAN
* @version 1.0
* @Date 2019-04-15
*/
public class CustomOnPropertyCondition implements Condition {
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(CustomConditionalOnProperty.class.getName());
String propertyName = (String) annotationAttributes.get("name");
String[] values = (String[]) annotationAttributes.get("havingValue");
if (0 == values.length) {
return false;
}
String propertyValue = conditionContext.getEnvironment().getProperty(propertyName);
// 有一个匹配上就ok
for (String havingValue : values) {
if (propertyValue.equalsIgnoreCase(havingValue)) {
return true;
}
}
return false;
}
}
(1)定义一个条件注解(2)定义一个条件的校验规则
原文:https://www.cnblogs.com/zeng1994/p/8c10310d8a042d56eddd40635afb6e93.html