The program runs in several environments, such as the development environment, the test environment, the production environment, and so on.
In each environment, many things have different practices, such as databases used.
To avoid the need to make a large number of changes to the program while switching the operating environment, Spring provides a profile setting that enables the program to handle different environments.
With profile, you can make a deployment unit (such as a war package) available to different environments, and the internal beans are created according to the needs of the environment.
1, configuration Profile Beanjava configuration profile
Use @profile annotations to specify which profile the bean belongs to
For classes, it is indicated that all beans under this class are created when the corresponding profile is activated.
@Configuration @profile ("Dev") Public class datasourceconfig { = "Shutdown") public DataSource embeddeddatasource () { return New Embeddeddatabasebuilder () . Build ();} }
For the method, it shows that
@Configuration Public classdatasourceconfig {@Bean (Destroymethod= "Shutdown") @Profile ("Dev") PublicDataSource Embeddeddatasource () {return NewEmbeddeddatabasebuilder (). Build (); } @Bean @Profile ("Prod") PublicDataSource Jndidatasource () {Jndiobjectfactorybean Jndiobjectfactorybean=NewJndiobjectfactorybean ();return(DataSource) jndiobjectfactorybean.getobject (); }}
XML Configuration Profile
Set the entire XML file to belong to a profile, each environment to create an XML file, put these XML files in the deployment environment, automatically called according to the environment
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"Xmlns:jee= "Http://www.springframework.org/schema/jee"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd/http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JDBC http://www.springframework.org/schema/jdbc/spring-jdbc.xsd/HTTP Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " Profile= "Dev"> <Jdbc:embedded-databaseID= "DataSource"type= "H2"> <Jdbc:script Location= "Classpath:schema.sql" /> <Jdbc:script Location= "Classpath:test-data.sql" /> </Jdbc:embedded-database> </Beans>
You can also create different profile beans under the same XML file by nesting <beans> elements
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"Xmlns:jee= "Http://www.springframework.org/schema/jee"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd/http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JDBC http://www.springframework.org/schema/jdbc/spring-jdbc.xsd/HTTP Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <Beans Profile= "Dev"> <Jdbc:embedded-databaseID= "DataSource"type= "H2"> <Jdbc:script Location= "Classpath:schema.sql" /> <Jdbc:script Location= "Classpath:test-data.sql" /> </Jdbc:embedded-database> </Beans> <Beans Profile= "prod"> <Jee:jndi-lookupID= "DataSource"Lazy-init= "true"Jndi-name= "Jdbc/mydatabase"Resource-ref= "true"Proxy-interface= "Javax.sql.DataSource" /> </Beans></Beans>
2. Activate profile
1. A bean that does not have a profile specified will be created at any time
2, according to the Spring.profiles.active property, find the current active profile
3. If the active property is not set, find the Spring.profiles.default property and locate the default profile
4. If 2 values are not set, no beans defined in profile will be created
Spring.profiles.active,spring.profiles.default2 How to set parameters:
1, as the initialization parameters of Dispatcherservlet;
2, as the context of the Web application parameters;
3, as a jndi entry;
4, as environment variables;
5, as the system properties of the JVM;
6. On the integration test class, use the @activeprofiles annotation settings
The following examples will describe
3. Use profile for testing
Using @activeprofiles annotations
@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Classes=datasourceconfig.class) @ActiveProfiles ("Prod") Public Static classproductiondatasourcetest {@AutowiredPrivateDataSource DataSource; @Test Public voidShouldbeembeddeddatasource () {//should is null, because there isn ' t a datasource configured in JNDIAssertnull (DataSource); } }
"Spring" 3.1, Environment and profile