Configuring the Environment
It is also convenient to use. As long as you add the following content to the Applicationcontext.xml, you can
Java code
-
<beans profile="Develop">
-
<context:property-placeholder location="classpath*:jdbc-develop.properties"/>
-
</beans>
-
<beans profile="Production">
-
<context:property-placeholder location="classpath*:jdbc-production.properties"/>
-
</beans>
-
<beans profile="Test">
-
<context:property-placeholder location="classpath*:jdbc-test.properties"/>
-
</beans>
The definition of profile must be at the bottom of the document, or there will be an exception. The structure of the whole XML is probably like this,
Java code
-
<beans xmlns= >
-
"DataSource" ... />
-
<bean ... />
-
<beans profile= >
-
<bean ...>
-
-
</beans>
I set different properties by giving different environments a different property, and you can define some special attributes directly in the bean, such as the bottom, when you initialize the database and the default data in test. (Excerpt from Code: Springside)
Java code
-
<!--unit test environment--
-
<beans profile="Test">
-
<context:property-placeholder ignore-resource-not-found="true"
-
Location= "Classpath*:/application.properties,
-
Classpath*:/application.test.properties "/>
-
-
<!--simple connection pool--
-
<bean id="DataSource" class="Org.springframework.jdbc.datasource.SimpleDriverDataSource ">
-
<property name="Driverclass" value="${jdbc.driver}" />
-
<property name="url" value="${jdbc.url}" />
-
<property name="username" value="${jdbc.username}" />
-
<property name="password" value="${jdbc.password}" />
-
</bean>
-
-
<!--initialize data table structure with default data--
-
<jdbc:initialize-database data-source="DataSource " ignore-failures="all">
-
<jdbc:script location="Classpath:sql/h2/schema.sql" />
-
<jdbc:script location="Classpath:data/import-data.sql" encoding="UTF-8"/>
-
</jdbc:initialize-database>
-
</beans>
switching environments
Add a Context-param in Web. XML to switch the current environment:
Java code
-
<context-param>
-
<param-name>spring.profiles.active</param-name>
-
<param-value>develop</param-value>
-
</context-param>
If the test class can use annotations to switch:
Java code
-
@ActiveProfiles ("Test")
The test class is probably like this:
Java code
-
@RunWith ( Springjunit4classrunner. @ Contextconfiguration (Locations = )
-
@ActiveProfiles ( "test" )
-
public extends abstracttransactionaljunit4springcontexttests
You can write a base class to write this annotation, and then let your test classes inherit this test base class, which is handy.
spring3.1 profile configuration for different environments