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测试