日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring Profiles example--转载

發(fā)布時(shí)間:2025/4/5 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Profiles example--转载 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:http://www.mkyong.com/spring/spring-profiles-example/

Spring?@Profile?allow developers to register beans by condition. For example, register beans based on what operating system (Windows, *nix) your application is running, or load a database properties file based on the application running in development, test, staging or production environment.

In this tutorial, we will show you a Spring?@Profile?application, which does the following stuff :

  • Create two profiles –?dev?and?live
  • If profile “dev” is enabled, return a simple cache manager –?ConcurrentMapCacheManager
  • If profile “l(fā)ive” is enabled, return an advanced cache manager –?EhCacheCacheManager
  • Note

    ?

  • Spring has supported @Profile annotation since version 3.1
  • @Profile is inside spring-context.jar
  • Tools used :

  • Spring 4.1.4.RELEASE
  • Ehcache 2.9.0
  • JDK 1.7
  • 1. Spring @Profile Examples

    This?@Profile?annotation can be applied at class level or method level.

    1.1 Normal Spring Configuration, enable caching, so that Spring will expect a cache manager at runtime.

    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 A?dev?profile, which returns a simple cache manager?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 A?live?profile, which returns?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; } }

    2. Enable @Profile

    Few code snippets to show you how to enable a Spring profile.

    2.1 For non-web application, you can enable a profile via the Spring context environment.

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

    Output

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

    Or, via the system property like this

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

    Output

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

    2.2 For web application, defined a context parameter in?web.xml

    web.xml <context-param><param-name>spring.profiles.active</param-name> <param-value>live</param-value> </context-param>

    2.3 For web application don’t have?web.xml, like servlet 3.0+ container

    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 For Unit Test, uses?@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. More…

    3.1 Spring?@Profile?can apply at method level.

    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 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 <context-param><param-name>spring.profiles.active</param-name> <param-value>stage, postgresql</param-value> </context-param> @ActiveProfiles({"dev", "mysql","integration"}) ((ConfigurableEnvironment)context.getEnvironment()) .setActiveProfiles(new String[]{"dev", "embedded"});

    轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/5008206.html

    總結(jié)

    以上是生活随笔為你收集整理的Spring Profiles example--转载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 免费黄网站在线 | 爱操综合| 国产色播av在线 | 性av免费| 每日在线观看av | 亚洲男人天堂av | 狠狠操网址 | 国产伦理一区 | 国内精品视频在线 | 你懂的av在线 | 日韩一级黄 | 日日干夜夜爽 | 少妇高潮淫片免费观看 | 激情小说欧美色图 | 日韩欧美黄色大片 | 欧美丰满熟妇bbb久久久 | 亚洲视频观看 | av高清不卡| 午夜精品一区二区三区在线 | 国产少妇一区二区 | 蜜桃视频在线观看网站 | 91老女人| 国产香蕉在线视频 | 青青在线视频观看 | 涩涩涩涩涩涩涩涩涩涩 | 亚洲大胆视频 | 四虎中文字幕 | 亚洲自拍偷拍一区二区三区 | 精品99久久久久成人网站免费 | 夫の上司に犯波多野结衣853 | 免费成人福利视频 | 黄毛片在线观看 | 国产精品久久久久久久久久小说 | 香蕉久久久久久 | 黄色小视频免费观看 | wwwxxx日本免费 | 欧美精品不卡 | 中文字幕精品一区二区精 | 无码久久av一区二区三区 | 日韩污视频在线观看 | 伊人av一区 | 国产一区二区三区麻豆 | 黄色综合 | 少妇精品一区二区三区 | av第一区| 超碰在线图片 | 国产在线观看第一页 | 秋霞影院午夜老牛影院 | 毛片av在线观看 | 欧美伦理一区二区 | 久久久天堂国产精品女人 | 欧美人体一区二区三区 | 丁香婷婷网 | 黄色三级在线 | 乳女教师の诱惑julia | 亚洲国产精品无码久久久久高潮 | 久久视频免费 | 亚州av在线| 国产精品女人和拘 | 欧美熟妇7777一区二区 | 操操日日 | 国产在线视频二区 | 黄色一级图片 | 亚洲最新视频 | 少妇不卡视频 | 超碰在线| 久久综合狠狠综合久久综合88 | 国产精品久久久久久久久久久久久久久 | 欧美视频在线观看 | 色妞网站| 操丝袜美女视频 | 中国女人内96xxxxx | 亚洲美女视频在线 | 一级片免费在线观看 | 亚洲精品日韩在线 | 亚洲av鲁丝一区二区三区 | 国产欧美在线观看不卡 | 久久av在线播放 | 亚洲无卡视频 | 日韩羞羞 | 狠狠人妻久久久久久综合蜜桃 | 日韩裸体视频 | 精品一区二区三区无码按摩 | 日本高清视频在线播放 | 女人18毛片水真多18精品 | 三年中文在线观看中文版 | 亚洲天堂2024 | 黄色wwwww| 男女视频一区 | 最新av不卡 | 色欲久久久天天天综合网 | 樱花影院电视剧免费 | 蜜臀久久99精品久久久久宅男 | 久久社区视频 | 黄色精品免费 | 四虎黄色影院 | 天天色天天射综合网 | 蜜桃aaa | 亚洲资源网站 |