@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();
}
}