spring.profiles.active: 官方解釋是激活不一樣環境下的配置文件,可是實際測試發現沒有對應的配置文件也是能夠正常執行的。那就能夠把這個key看成一個參數來使用html
@Profile: spring.profiles.active中激活某配置則在spring中託管這個bean,配合@Component,@Service、@Controller、@Repository等使用java
@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 { @Autowired private BaseTest test; public void printConsole(){ test.test(); } } //配置文件激活某個環境則test就會注入哪一個bean spring.profiles.active=xx
@Configuration: 至關於原有的spring.xml,用於配置spring @ConditionalOnProperty: 依據激活的配置文件中的某個值判斷是否託管某個bean,org.springframework.boot.autoconfigure.condition包中包含不少種註解,能夠視狀況選擇spring
@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 myTest=xx }
http://www.javashuo.com/article/p-hamsvymi-dy.html https://www.javacodegeeks.com/2013/10/spring-4-conditional.html測試