Advanced Assembly 1.bean of: Chapter III
profile: development environment (dev), QA environment (qa), production (prod)
1. Configure profile bean:
1.1 Annotation-based form:
@Profile annotation can specify a bean belongs Profile, @Profile only add notes in class on Spring3.1 time, Spring3.2 began to be applied to the method
@Configuration public class DataSourceConfig { @Bean(destroyMethod = "shutdown") @Profile("dev") public DataSource embeddedDataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .addScript("classpath:schema.sql") .addScript("classpath:test-data.sql") .build(); } @Bean @Profile("prod") public DataSource jndiDataSource() { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("jdbc/myDS"); jndiObjectFactoryBean.setResourceRef(true); jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class); return (DataSource) jndiObjectFactoryBean.getObject(); } }
1.2.xml form:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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-database id="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-lookup id="dataSource" lazy-init="true" jndi-name="jdbc/myDatabase" resource-ref="true" proxy-interface="javax.sql.DataSource" /> </beans> <beans profile="qa"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:url="" p:driverClassName="" p:username="" /> </beans> </beans>
2. How to activate?
Two properties: spring.profiles.default and spring.profiles.active
First, find the active spring of profile, if not then find the default, if not there is no active profile.
There are several ways to set these two attributes:
- As the initialization parameter of DispatcherServlet;
- As the web application context parameter;
- As JNDI entries;
- As an environment variable;
- As the JVM system properties;
- In the integrated test class, using @ActiveProfiles annotation configuration.
1. As DispatcherServlet initialization parameter init-param
<!-- 在上下文中设置profile的默认值 --> 16 <context-param> 17 <param-name>spring.profiles.default</param-name> 18 <param-value>dev</param-value> 19 </context-param>
21 <listener> 22 <listener-class> 23 org.springframework.web.context.ContextLoaderListener 24 </listener-class> 25 </listener> 26 27 <servlet> 28 <servlet-name>appServlet</servlet-name> 29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 30 <!-- 在servlet中设置profile的默认值 --> 31 <init-param> 32 <param-name>spring.profiles.default</param-name> 33 <param-value>dev</param-value> 34 </init-param> 35 <load-on-startup>1</load-on-startup> 36 </servlet>
37 <servlet-mapping> 38 <servlet-name>appServlet</servlet-name> 39 <url-pattern>/</url-pattern> 40 </servlet-mapping>
Use profile tests:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=TestConfig.class) @ActiveProfiles("dev") public class MagicExistsTest { ........ }