springboot的profile注解使用

@Profile

该注解配合@Configuration可以使用java代码指定不同profile的配置,可以注解在类上,也可以注解在@Bean的方法上。

注解在类上

dev

@Configuration
@Profile("dev")
public class CacheConfigDev {

    private static final Logger log = LoggerFactory.getLogger(CacheConfigDev.class);

    @Bean
        public CacheManager concurrentMapCacheManager() {
        log.debug("Cache manager is concurrentMapCacheManager");
                return new ConcurrentMapCacheManager("movieFindCache");
        }

}

prod

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

    private static final Logger log = LoggerFactory.getLogger(CacheConfigProd.class);

    @Bean
    public CacheManager cacheManager() {
        log.debug("Cache manager is ehCacheCacheManager");
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
        cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cmfb.setShared(true);
        return cmfb;
    }

}

注解在@Bean上

@Configuration
public class ProfileConfig {

    @Bean(name="dev")
    @Profile("dev")
     DevProfile devProfile() {
        return new DevProfile();
    }

    @Bean(name="prod")
    @Profile("prod")
     ProdProfile ProdProfile() {
        return new ProdProfile();
    }
}

docs

    原文作者:codecraft
    原文地址: https://segmentfault.com/a/1190000006684800
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞