Spring Profiles示例

小灰灰 2023-02-15 05:07 22阅读 0赞

有条件的有条件的

Spring @Profile允许开发人员按条件注册Bean。 例如,根据您的应用程序正在运行的操作系统(Windows,* nix)注册Bean,或根据在开发,测试,登台或生产环境中运行的应用程序加载数据库属性文件。

在本教程中,我们将向您展示Spring @Profile应用程序,该应用程序执行以下操作:

  1. 创建两个配置文件devlive
  2. 如果启用了配置文件“ dev”,则返回一个简单的缓存管理器– ConcurrentMapCacheManager
  3. 如果启用了配置文件“实时”,则返回高级缓存管理器– EhCacheCacheManager

注意

  1. 从版本3.1开始,Spring已支持@Profile批注
  2. @Profile位于spring-context.jar中

使用的工具 :

  1. Spring4.1.4。发布
  2. 高速缓存2.9.0
  3. JDK 1.7

1. Spring @Profile示例

@Profile批注可以应用于类级别或方法级别。

1.1正常的Spring配置,启用缓存,以便Spring在运行时期望一个缓存管理器。

AppConfig

  1. package com.mkyong.test;
  2. import org.springframework.cache.annotation.EnableCaching;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. @EnableCaching
  7. @ComponentScan({ "com.mkyong.*" })
  8. public class AppConfig {
  9. }

1.2 dev配置文件,该配置文件返回一个简单的缓存管理器concurrentMapCacheManager

CacheConfigDev.java

  1. package com.mkyong.test;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.annotation.Profile;
  9. @Configuration
  10. @Profile("dev")
  11. public class CacheConfigDev {
  12. private static final Logger log = LoggerFactory.getLogger(CacheConfigDev.class);
  13. @Bean
  14. public CacheManager concurrentMapCacheManager() {
  15. log.debug("Cache manager is concurrentMapCacheManager");
  16. return new ConcurrentMapCacheManager("movieFindCache");
  17. }
  18. }

1.3 live配置文件,它返回ehCacheCacheManager

CacheConfigLive.java

  1. package com.mkyong.test;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.cache.ehcache.EhCacheCacheManager;
  6. import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.Profile;
  10. import org.springframework.core.io.ClassPathResource;
  11. @Configuration
  12. @Profile("live")
  13. public class CacheConfigLive {
  14. private static final Logger log = LoggerFactory.getLogger(CacheConfigDev.class);
  15. @Bean
  16. public CacheManager cacheManager() {
  17. log.debug("Cache manager is ehCacheCacheManager");
  18. return new EhCacheCacheManager(ehCacheCacheManager().getObject());
  19. }
  20. @Bean
  21. public EhCacheManagerFactoryBean ehCacheCacheManager() {
  22. EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
  23. cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
  24. cmfb.setShared(true);
  25. return cmfb;
  26. }
  27. }

2.启用@Profile

很少有代码片段向您展示如何启用Spring配置文件。

2.1对于非Web应用程序,可以通过Spring上下文环境启用配置文件。

App.java

  1. package com.mkyong.test;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6. public class App {
  7. public static void main(String[] args) {
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  9. //Enable a "live" profile
  10. context.getEnvironment().setActiveProfiles("live");
  11. context.register(AppConfig.class);
  12. context.refresh();
  13. ((ConfigurableApplicationContext) context).close();
  14. }
  15. }

输出量

  1. DEBUG com.mkyong.test.CacheConfigDev - Cache manager is ehCacheCacheManager

或者,通过这样的系统属性

App.java

  1. package com.mkyong.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. import org.springframework.core.env.AbstractEnvironment;
  5. public class App {
  6. public static void main(String[] args) {
  7. //Enable a "dev" profile
  8. System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
  9. ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  10. }
  11. }

输出量

  1. DEBUG com.mkyong.test.CacheConfigDev - Cache manager is concurrentMapCacheManager

2.2对于Web应用程序,在web.xml定义了上下文参数

web.xml

  1. <context-param>
  2. <param-name>spring.profiles.active</param-name>
  3. <param-value>live</param-value>
  4. </context-param>

2.3对于Web应用程序,没有servlet 3.0+容器之类的web.xml

MyWebInitializer.java

  1. package com.mkyong.servlet3;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.ServletException;
  4. public class MyWebInitializer extends
  5. AbstractAnnotationConfigDispatcherServletInitializer {
  6. //...
  7. @Override
  8. public void onStartup(ServletContext servletContext) throws ServletException {
  9. super.onStartup(servletContext);
  10. servletContext.setInitParameter("spring.profiles.active", "live");
  11. }
  12. }

2.4对于单元测试,使用@ActiveProfiles

CacheManagerTest.java

  1. package com.mkyong.test;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.test.context.ActiveProfiles;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import org.springframework.cache.CacheManager;
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @ContextConfiguration(classes = { AppConfig.class })
  12. @ActiveProfiles("dev")
  13. public class CacheManagerTest {
  14. @Autowired
  15. private CacheManager cacheManager;
  16. @Test
  17. public void test_abc() {
  18. //...
  19. }
  20. }

3.更多…

3.1 Spring @Profile可以在方法级别应用。

AppConfig.java

  1. package com.mkyong.test;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.cache.annotation.EnableCaching;
  6. import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
  7. import org.springframework.cache.ehcache.EhCacheCacheManager;
  8. import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Profile;
  13. import org.springframework.core.io.ClassPathResource;
  14. @Configuration
  15. @EnableCaching
  16. @ComponentScan({ "com.mkyong.*" })
  17. public class AppConfig {
  18. private static final Logger log = LoggerFactory.getLogger(AppConfig.class);
  19. @Bean
  20. @Profile("dev")
  21. public CacheManager concurrentMapCacheManager() {
  22. log.debug("Cache manager is concurrentMapCacheManager");
  23. return new ConcurrentMapCacheManager("movieFindCache");
  24. }
  25. @Bean
  26. @Profile("live")
  27. public CacheManager cacheManager() {
  28. log.debug("Cache manager is ehCacheCacheManager");
  29. return new EhCacheCacheManager(ehCacheCacheManager().getObject());
  30. }
  31. @Bean
  32. @Profile("live")
  33. public EhCacheManagerFactoryBean ehCacheCacheManager() {
  34. EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
  35. cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
  36. cmfb.setShared(true);
  37. return cmfb;
  38. }
  39. }

3.2您可以启用多个配置文件。

  1. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  2. context.getEnvironment().setActiveProfiles("live", "linux");
  3. //or
  4. System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev, windows");

web.xml

  1. <context-param>
  2. <param-name>spring.profiles.active</param-name>
  3. <param-value>stage, postgresql</param-value>
  4. </context-param>
  5. @ActiveProfiles({"dev", "mysql","integration"})
  6. ((ConfigurableEnvironment)context.getEnvironment())
  7. .setActiveProfiles(new String[]{"dev", "embedded"});

下载源代码

下载它– Spring-Profiles-Example.zip (17 KB)

参考文献

  1. Spring概况–环境抽象
  2. Spring缓存和Ehcache示例
  3. Spring MVC –设置活动配置文件

标签: 弹簧 弹簧轮廓

翻译自: https://mkyong.com/spring/spring-profiles-example/

发表评论

表情:
评论列表 (有 0 条评论,22人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Profile

    一、简介 Profile的意思是配置,对于应用程序来说,不同的环境需要不同的配置。 比如: 开发环境,应用需要连接一个可供调试的数据库单机进程 生产环境

    相关 Spring注解--@Profile

    @Profile Profile的意思是配置,对于应用程序来说,不同的环境需要不同的配置。 比如: 开发环境,应用需要连接一个可供调试的数据库单机进程