上海专业网站制作设计,大学生应届毕业生招聘官网,百度一下首页版,网站建设公司哪家好智搜宝通过Profilespring.profiles.active
spring.profiles.active#xff1a;官方解释是激活不同环境下的配置文件#xff0c;但是实际测试发现没有对应的配置文件也是可以正常执行的。那就可以把这个key当作一个参数来使用 Profile#xff1a;spring.profiles.active中激活某配…通过Profilespring.profiles.active
spring.profiles.active官方解释是激活不同环境下的配置文件但是实际测试发现没有对应的配置文件也是可以正常执行的。那就可以把这个key当作一个参数来使用 Profilespring.profiles.active中激活某配置则在spring中托管这个bean配合Component,Service、Controller、Repository等使用
Component
Profile(xx)
public class XxxTest extends BaseTest {public void test(){System.out.println(in XxxTest );}
}
Component
Profile(yy)
public class YyyTest extends BaseTest {public void test(){System.out.println(in YyyTest );}
}Service
public class MyService {Autowiredprivate BaseTest test;public void printConsole(){test.test();}
}//配置文件激活某个环境则test就会注入哪个bean
spring.profiles.activexx通过ConfigurationConditionalOnProperty
Configuration相当于原有的spring.xml,用于配置spring ConditionalOnProperty依据激活的配置文件中的某个值判断是否托管某个beanorg.springframework.boot.autoconfigure.condition包中包含很多种注解可以视情况选择
Configuration
public static class ContextConfig {
Autowired
private XxxTest xxTest;
Autowired
private YyyTest yyTest;Bean
ConditionalOnProperty(value myTest,havingValue xx)public BaseTest xxxTest() {return xxTest;}Bean
ConditionalOnProperty(value myTest,havingValue yy)public BaseTest yyyTest() {return yyTest;}
//配置文件中控制激活哪个bean
myTestxx
}