001profile条件化创建bean

01、类级别条件创建

@Configuration
@Profile("dev")
public class Aclass{}---->影响整个类,包括类的注解。开发环境,类中的配置才生效

02、方法级别条件创建

@Configuration
poublic class AClass{
    @Bean
    @Profile("dev")---->与@Bean一起使用,仅仅影响整个方法
    public DataSource createDataSource(){
        ....
    }
}

03、profile激活

  spring.profiles.active="pro"  //激活"dev" profile
  spring.profiles.default="dev"  //不配置spring.profiles.active时,"pro"默认生效

  开发环境:使用DispatcherServlet参数将spring.profiles.default设置为开发环境的profile.在Servlet上下文中进行设置
  QA/生产环境:负责部署的人,使用系统属性 / 环境变量 / JNDI设置spring.profiles.active即可

原文地址:https://www.cnblogs.com/geniushuangxiao/p/7300951.html