原文地址: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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 loader
and 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 |
|
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 |
|
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.