@Profile 区分不同环境下的配置
对象区分
@Component
@Profile("dev")//dev环境生效
public class myDev implements My(){}
@Component
@PRofile("!dev")//非 dev环境生效
public class myNotDev() implements My{}
获取当前环境
@Autowired
Environment environment;
for(final String profileName : environment.getActiveProfiles()){
//可以获取当前的 profile
}
环境设置 application.properties
spring.profiles.active=dev
测试 @ActiveProfiles
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguation(classes = app.class)
@ActiveProfiles("dev")//测试环境
public class test(){
@Autowired
My my;
......
}