@Profile标识在类上,那么只有当前环境匹配,整个配置类才会生效
@Profile标识在Bean上 ,那么只有当前环境的Bean才会被激活
没有标志为@Profile的bean 不管在什么环境都可以被激活
激活切换环境的方法
- 通过运行时jvm参数来切换 -Dspring.profiles.active=test | dev | prod
- 通过代码的方式来激活
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
context.getEnvironment().setActiveProfiles("test","dev");