springboot中的@Profile简单使用

222 阅读1分钟
在日常开发中难免会碰到环境切换的问题,springboot中提供了支持:@Profile、spring.profiles.active,这里只简单介绍一下@Profile的使用。

使用 @Profile 注解可以指定类或方法在特定的 Profile 环境生效。

  • 放在方法上

@Configuration
public class AppConfig {

    @Bean("dataSource")
    @Profile("dev")
    public DataSource database1() {
        //do something...
        return null;
    }
}

  • 放在类上

@Configuration
@Profile("prod")
public class JndiDataConfig {

    @Bean
    public DataSource dataSource() throws Exception {
        //do something...
        return null;
    }
}


  • 激活 profile