application.properties
env = pro //dev
@Component
public class DevCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String env = environment.getProperty("env");
if("dev".equals(env)){
return true;
}
return false;
}
}
@Component
public class ProConditon implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String env = environment.getProperty("env");
if("pro".equals(env)){
return true;
}
return false;
}
}
@Configuration
public class MainConfig {
@Bean
@Conditional(ProConditon.class)
public ProDatasource proDatasource(){
return new ProDatasource();
}
@Bean
@Conditional(DevCondition.class)
public DevDatasource devDatasource(){
return new DevDatasource();
}
}
public class DevDatasource {
public DevDatasource(){
System.out.println("=====dev初始化=====");
}
}
public class ProDatasource {
public ProDatasource(){
System.out.println("=====Pro初始化=====");
}
}