自定义逻辑
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* 环境识别
*/
public class EnvironmentJudge implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String environment = context.getEnvironment().getProperty("environment");
if ("qa".equals(environment) || "prd".equals(environment)) {
return true;
}
return false;
}
}
使用方式
@Component
@Conditional(EnvironmentJudge.class)
public class Config {
@PostConstruct
private void test() {
System.out.println("注册进来了.....");
}
}