Spring @Profile
允许开发人员按条件注册bean。例如,根据您的应用程序正在运行的操作系统(Windows,* nix)注册bean,或者根据在开发,测试,登台或生产环境中运行的应用程序加载数据库属性文件。
在本教程中,我们将向您展示一个Spring @Profile
应用程序,它执行以下操作:
dev
和live
ConcurrentMapCacheManager
EhCacheCacheManager
注意
使用的工具 :
此@Profile
批注可以在类级别或方法级别应用。
1.1普通的Spring配置,启用缓存,以便Spring在运行时期望缓存管理器。
AppConfig中
package com.mkyong.test;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
@ComponentScan({ "com.mkyong.*" })
public class AppConfig {
}
复制
1.2 dev
配置文件,它返回一个简单的缓存管理器concurrentMapCacheManager
CacheConfigDev.java
package com.mkyong.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@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");
}
}
复制
1.3 live
返回的配置文件ehCacheCacheManager
CacheConfigLive.java
package com.mkyong.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
@Configuration
@Profile("live")
public class CacheConfigLive {
private static final Logger log = LoggerFactory.getLogger(CacheConfigDev.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;
}
}
复制
很少有代码片段向您展示如何启用Spring配置文件。
2.1对于非Web应用程序,您可以通过Spring上下文环境启用配置文件。
App.java
package com.mkyong.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//Enable a "live" profile
context.getEnvironment().setActiveProfiles("live");
context.register(AppConfig.class);
context.refresh();
((ConfigurableApplicationContext) context).close();
}
}
复制
产量
DEBUG com.mkyong.test.CacheConfigDev - Cache manager is ehCacheCacheManager
复制
或者,通过这样的系统属性
App.java
package com.mkyong.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
public class App {
public static void main(String[] args) {
//Enable a "dev" profile
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
}
}
复制
产量
DEBUG com.mkyong.test.CacheConfigDev - Cache manager is concurrentMapCacheManager
复制
2.2对于Web应用程序,在中定义了上下文参数 web.xml
web.xml中
spring.profiles.active
live
复制
2.3对于Web应用程序没有web.xml
,像servlet 3.0+容器
MyWebInitializer.java
package com.mkyong.servlet3;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
//...
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "live");
}
}
2.4对于单元测试,使用 @ActiveProfiles
CacheManagerTest.java
package com.mkyong.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.cache.CacheManager;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class })
@ActiveProfiles("dev")
public class CacheManagerTest {
@Autowired
private CacheManager cacheManager;
@Test
public void test_abc() {
//...
}
}
复制
3.1 Spring @Profile
可以在方法级别应用。
AppConfig.java
package com.mkyong.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableCaching
@ComponentScan({ "com.mkyong.*" })
public class AppConfig {
private static final Logger log = LoggerFactory.getLogger(AppConfig.class);
@Bean
@Profile("dev")
public CacheManager concurrentMapCacheManager() {
log.debug("Cache manager is concurrentMapCacheManager");
return new ConcurrentMapCacheManager("movieFindCache");
}
@Bean
@Profile("live")
public CacheManager cacheManager() {
log.debug("Cache manager is ehCacheCacheManager");
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
@Profile("live")
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
cmfb.setShared(true);
return cmfb;
}
}
复制
3.2您可以启用多个配置文件。
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("live", "linux");
//or
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev, windows");
复制
web.xml中
<context-param>
<param-name>spring.profiles.activeparam-name>
<param-value>stage, postgresqlparam-value>
context-param>
复制
@ActiveProfiles({"dev", "mysql","integration"})
复制
((ConfigurableEnvironment)context.getEnvironment())
.setActiveProfiles(new String[]{"dev", "embedded"});