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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 3.1 Environment Profiles--转载

發布時間:2025/4/5 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 3.1 Environment Profiles--转载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://gordondickens.com/wordpress/2012/06/12/spring-3-1-environment-profiles/

Profiles

Spring 3.1 now includes support for the long awaited environment aware feature called profiles. Now we can activate profiles in our application, which allows us to define beans by deployment regions, such as “dev”, “qa”, “production”, “cloud”, etc.

We also can use this feature for other purposes: defining profiles for performance testing scenarios such as “cached” or “lazyload”.

Essential Tokens

Spring profiles are enabled using the case insensitive tokens?spring.profiles.active?orspring_profiles_active.

This token can be set as:

  • an Environment Variable
  • a JVM Property
  • Web Parameter
  • Programmatic

Spring also looks for the token,?spring.profiles.default, which can be used to set the default profile(s) if none are specified with?spring.profiles.active.

Grouping Beans by Profile

Spring 3.1 provides nested bean definitions, providing the ability to define beans for various environments:

?
1 2 3 4 <beans profiles="dev,qa"> ??<bean id="dataSource" class="..."/> ??<bean id="messagingProvider" class="..."/> </beans>

Nested?<beans>?must appear last in the file.?
Beans that are used in all profiles are declared in the outer?<beans>?as we always have, such as Service classes.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ???????xmlns:c="http://www.springframework.org/schema/c" ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ???????xsi:schemaLocation="http://www.springframework.org/schema/beans ???????http://www.springframework.org/schema/beans/spring-beans.xsd"> ????<bean id="businessService" ???????class="com.c...s.springthreeone.business.SimpleBusinessServiceImpl"/> ????<beans profile="dev,qa"> ????????<bean id="constructorBean" ??????????class="com.gordondickens.springthreeone.SimpleBean" ??????????????c:myString="Constructor Set"/> ????????<bean id="setterBean" ??????????class="com.gordondickens.springthreeone.SimpleBean"> ????????????<property name="myString" value="Setter Set"/> ????????</bean> ????</beans> ????<beans profile="prod"> ????????<bean id="setterBean" ??????????class="com.gordondickens.springthreeone.SimpleBean"> ????????????<property name="myString" value="Setter Set - in Production YO!"/> ????????</bean> ????</beans> </beans>

If we put a single?<bean>?declaration at below any nested?<beans>?tags we will get the exception?org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'bean'.

Multiple beans can now share the same XML “id”?
In a typical scenario, we would want the DataSource bean to be called?dataSource?in both all profiles. Spring now allow us to create multiple beans within an XML file with the same ID providing they are defined in different?<beans>?sets. In other words, ID uniqueness is only enforced within each?<beans>?set.

Automatic Profile Discovery (Programmatic)

We can configure a class to set our profile(s) during application startup by implementing the?appropriate interface. For example, we may configure an application to set different profiles based on where the application is deployed – in CloudFoundry or running as a local web application. In the?web.xml?file we can include an Servlet context parameter,contextInitializerClasses,?to bootstrap this class:

?
1 2 3 4 <context-param> ??<param-name>contextInitializerClasses</param-name> ??<param-value>com.gordondickens.springthreeone.services.CloudApplicationContextInitializer</param-value> </context-param>

The Initializer class

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.gordondickens.springthreeone.services; import org.cloudfoundry.runtime.env.CloudEnvironment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; public class CloudApplicationContextInitializer implements ??ApplicationContextInitializer<ConfigurableApplicationContext> { ????? ??private static final Logger logger = LoggerFactory ????.getLogger(CloudApplicationContextInitializer.class); ??@Override ??public void initialize(ConfigurableApplicationContext applicationContext) { ????CloudEnvironment env = new CloudEnvironment(); ????if (env.getInstanceInfo() != null) { ??????logger.info("Application running in cloud. API '{}'", ????????env.getCloudApiUri()); ??????applicationContext.getEnvironment().setActiveProfiles("cloud"); ??????applicationContext.refresh(); ????} else { ??????logger.info("Application running local"); ??????applicationContext.getEnvironment().setActiveProfiles("dev"); ????} ??} }

?

Annotation Support for JavaConfig

If we are are using JavaConfig to define our beans, Spring 3.1 includes the?@Profileannotation for enabling bean config files by profile(s).

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.gordondickens.springthreeone.configuration; import com.gordondickens.springthreeone.SimpleBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("dev") public class AppConfig { ??@Bean ??public SimpleBean simpleBean() { ????SimpleBean simpleBean = new SimpleBean(); ????simpleBean.setMyString("Ripped Pants"); ????return simpleBean; ??} }

?

Testing with XML Configuration

With XML configuration we can simply add the annotation?@ActiveProfiles?to the JUnit test class. To include multiple profiles, use the format?@ActiveProfiles(profiles = {"dev", "prod"})

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.gordondickens.springthreeone; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @ActiveProfiles(profiles = "dev") public class DevBeansTest { ??@Autowired ??ApplicationContext applicationContext; ??@Test ??public void testDevBeans() { ????SimpleBean simpleBean = ??????applicationContext.getBean("constructorBean", SimpleBean.class); ????assertNotNull(simpleBean); ??} ??@Test(expected = NoSuchBeanDefinitionException.class) ??public void testProdBean() { ????SimpleBean prodBean = applicationContext.getBean("prodBean", SimpleBean.class); ????assertNull(prodBean); ??} }

?

Testing with JavaConfig

JavaConfig allows us to configure Spring with or without XML configuration. If we want to test beans that are defined in a Configuration class we configure our test with the?loaderand?classes?arguments of the?@ContextConfiguration?annotation.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.gordondickens.springthreeone.configuration; import com.gordondickens.springthreeone.SimpleBean; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class) @ActiveProfiles(profiles = "dev") public class BeanConfigTest { ??@Autowired ??SimpleBean simpleBean; ??@Test ??public void testBeanAvailablity() { ????assertNotNull(simpleBean); ??} }

?

Declarative Configuration in WEB.XML

If we desire to set the configuration in?WEB.XML, this can be done with parameters onContextLoaderListener.

Application Context

?
1 2 3 4 5 6 7 8 <context-param> ??<param-name>contextConfigLocation</param-name> ??<param-value>/WEB-INF/app-config.xml</param-value> </context-param> <context-param> ??<param-name>spring.profiles.active</param-name> ??<param-value>DOUBLEUPMINT</param-value> </context-param>

Log Results

DEBUG PropertySourcesPropertyResolver - Found key 'spring.profiles.active' in [servletContextInitParams] with type [String] and value 'DOUBLEUPMINT'

Environment Variable/JVM Parameter
Setting an environment variable can be done with either?spring_profiles_default?orspring_profiles_active. In Unix/Mac it would be?export SPRING_PROFILES_DEFAULT=DEVELOPMENT?for my local system.

We can also use the JVM “-D” parameter which also works with Maven when using Tomcat or Jetty plugins.

Note: Remember the tokens are NOT case sensitive and can use periods or underscores as separators. For Unix systems, you need to use the underscore, as above.

Logging of system level properties?DEBUG PropertySourcesPropertyResolver - Found key 'spring.profiles.default' in [systemProperties] with type [String] and value 'dev,default'

Summary

Now we are equipped to activate various Spring bean sets, based on profiles we define. We can use? traditional, XML based configuration, or the features added to support JavaConfig originally introduced in Spring 3.0.

轉載于:https://www.cnblogs.com/davidwang456/p/4429058.html

總結

以上是生活随笔為你收集整理的Spring 3.1 Environment Profiles--转载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 人妖性生活视频 | 色天堂在线视频 | 亚洲大逼 | 天天色成人网 | 国产美女裸体无遮挡免费视频 | 午夜三区 | 香蕉大久久 | 国产成人无码一区二区三区在线 | 青青精品视频 | 国产福利视频一区 | 亚洲女人被黑人巨大进入 | 色777| 国产免费黄色网址 | 欧美91看片特黄aaaa | 亚洲国产成人精品视频 | 视频在线国产 | av嫩草 | 中文字幕麻豆 | 日韩在线精品视频 | 久久精品免费电影 | 色综合狠狠操 | 熟女毛毛多熟妇人妻aⅴ在线毛片 | 久久精品一二区 | 国产 日韩 欧美在线 | 黄色茄子视频 | 蜜桃视频一区二区三区在线观看 | 日本高清视频一区二区三区 | 又色又爽又高潮免费视频国产 | 久久黄色网络 | 带aaa级的网名 | 成人精品久久久午夜福利 | avtt2015| 国产123区在线观看 91国产一区二区 | 小泽玛利亚一区二区三区视频 | 91精品播放 | 两性午夜视频 | 日色视频| 麻豆入口 | 韩国伦理在线 | 国产亚洲精品电影 | 男人操女人的网站 | 91精品国产综合久久久蜜臀粉嫩 | 18色av| 欧美成年人视频在线观看 | 色戒av| 在线一区二区三区四区五区 | 亚洲一区| 人人爱爱| 在线观看亚洲一区 | 五月视频 | 黑人玩弄人妻一区二区三区 | 相亲对象是问题学生在线观看 | 国产一线av | 久久成人综合 | 国产婷婷色一区二区三区 | 久久观看 | 91影院在线播放 | 欧美人与动牲交a欧美精品 欧美三级在线看 | 午夜精品福利影院 | 88福利视频 | 奶罩不戴乳罩邻居hd播放 | 成人视屏在线 | 日本亚洲精品 | 国产一线二线三线在线观看 | 一曲二曲三曲在线观看中文字幕动漫 | 欧美日韩在线观看免费 | 亚洲天堂不卡 | 午夜精品视频一区 | 91成人福利视频 | 亚洲大胆人体 | 97看片吧 | 在线国产网站 | aaa国产视频 | 天堂中文在线资 | 天天综合网天天综合色 | jizzjizz美国 | 污漫网站 | 一区二区三区视频网 | 亚洲欧美大片 | 久久不雅视频 | 激情黄色小说网站 | 国产三级国产精品国产国在线观看 | 麻豆蜜桃视频 | 五月激情久久 | 伊人福利| 午夜肉体高潮免费毛片 | 欧美tv | 日本bbwbbw| 91大神精品在线 | jizz视频在线观看 | 久久午夜精品人妻一区二区三区 | 国产精品探花一区二区三区 | 免费av不卡| 中文视频一区 | 亚av| 国产美女在线精品 | 超碰成人免费电影 | 欧美粗大猛烈老熟妇 | 成年人在线观看网站 |