For the contact application, we would like to have two profiles, as follows:
Development profile ("dev"): Profile with configuration for the development
·
environment. For example, in the development system, the backend H2 database
will have both the database creation and the initial data population scripts
executed.
Testing profile ("test"): Profile with configuration for the testing environment. For
·
example, in the testing environment, the backend H2 database will have only the
database creation script executed, while the data will be populated by the test case.
Let's configure the profile environment for the contact application. For the contact application, the
backend configuration (i.e., data source, JPA, transaction, and so on) was defined in the configuration
XML file /src/main/resources/datasource-tx-jpa.xml. We would like to configure the data source in the
file for dev profile only. To do this, we need to wrap the data source bean with the profile configuration.
Listing 19-5 shows the code snippet for the change required.
Listing 19-5. Configure Profile for Data Source
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<!-- Other code omitted -->
<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>
The change to apply is highlighted in bold. As shown in the code snippet, the dataSource bean is
wrapped with the <beans> tag and given the profile attribute with the value dev, which indicates that the
data source is applicable only for the development system.
To bootstrap the web application with the development profile, we add the parameter in the web
deployment descriptor (another way is to modify the web container startup script). Listing 19-6 shows
the code snippet.
Listing 19-6. Configure Profile in web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<context-param>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home