spring 条件化配置

步骤一:

实现接口:org.springframework.context.annotation.Condition

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class MyJdbcTc implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
try {
    //你需要加载的类权限的名称,或者指定的某些条件
conditionContext.getClassLoader().loadClass("java.lang.Object");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}

步骤二:
再Bean方法或者类上使用org.springframework.context.annotation.Conditional注解
@Configuration
public class MyConfig {
@Bean
@Conditional(MyJdbcTc.class)
public User user(){
User user = new User();
user.setId(1);
user.setName("hello");
return user;
}
}
原文地址:https://www.cnblogs.com/chen--biao/p/10858072.html