スプリングプロファイルの例

Spring@Profileを使用すると、開発者は条件ごとにBeanを登録できます。 たとえば、アプリケーションが実行されているオペレーティングシステム(Windows、* nix)に基づいてBeanを登録したり、開発、テスト、ステージング、または運用環境で実行されているアプリケーションに基づいてデータベースプロパティファイルをロードします。
このチュートリアルでは、次のことを行うSpring@Profileアプリケーションを紹介します。
-
2つのプロファイルを作成します–
devとlive -
プロファイル「dev」が有効になっている場合は、単純なキャッシュマネージャーを返します–
ConcurrentMapCacheManager -
プロファイル「ライブ」が有効になっている場合は、高度なキャッシュマネージャーを返します–
EhCacheCacheManager
Note
-
バージョン3.1以降、Springは@Profileアノテーションをサポートしています
-
@Profileはspring-context.jar内にあります
使用ツール:
-
Spring 4.1.4.RELEASE
-
ehcache 2.9.0
-
JDK 1.7
1. Spring @Profileの例
この@Profileアノテーションは、クラスレベルまたはメソッドレベルで適用できます。
1.1 Normal Spring Configuration, enable caching, so that Spring will expect a cache manager at runtime.
AppConfig
package com.example.test;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
@ComponentScan({ "com.example.*" })
public class AppConfig {
}
1.2 A dev profile, which returns a simple cache manager concurrentMapCacheManager
CacheConfigDev.java
package com.example.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 A live profile, which returns ehCacheCacheManager
CacheConfigLive.java
package com.example.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;
}
}
2. @Profileを有効にする
Springプロファイルを有効にする方法を示すコードスニペットはほとんどありません。
2.1 For non-web application, you can enable a profile via the Spring context environment.
App.java
package com.example.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.example.test.CacheConfigDev - Cache manager is ehCacheCacheManager
または、このようなシステムプロパティを介して
App.java
package com.example.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.example.test.CacheConfigDev - Cache manager is concurrentMapCacheManager
2.2 For web application, defined a context parameter in web.xml
web.xml
spring.profiles.active live
2.3 For web application don’t have web.xml, like servlet 3.0+ container
MyWebInitializer.java
package com.example.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 For Unit Test, uses @ActiveProfiles
CacheManagerTest.java
package com.example.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. もっと…
3.1 Spring @Profile can apply at method level.
AppConfig.java
package com.example.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.example.*" })
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 You can enable multiple profiles.
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("live", "linux");
//or
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev, windows");
web.xml
spring.profiles.active stage, postgresql
@ActiveProfiles({"dev", "mysql","integration"})
((ConfigurableEnvironment)context.getEnvironment())
.setActiveProfiles(new String[]{"dev", "embedded"});
ソースコードをダウンロード
ダウンロード–Spring-Profiles-Example.zip(17 KB)