步骤一:

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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;
}
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄