At this point, the project has been set up successfully. Note that, by default, the sample application
doesn't include any sample blog entries. Please log in to the application (click the Hint button under the
Login button for the default user accounts) and post some blog entries and comments to get some
hands-on experience with the application.
Switching Between the MySQL and H2 Databases
By default, the SpringBlog application will initialize an embedded H2 database and run the required
scripts accordingly, so no additional setup is required. However, the database scripts and application
also work fine with MySQL. This section lists the procedure for setting up the application with a MySQL
database so that you can keep track of the database records as you play around the application.
Listing 21-1 shows the content of the Spring configuration file /src/main/webapp/WEB-INF/spring
/datasource.xml, which stores the datasource configuration for the SpringBlog application.
Listing 21-1. The Data Source Configuration File
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<!-- For embedded JH2 database -->
<beans profile="h2">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:sql/schema.sql"/>
<jdbc:script location="classpath:/org/springframework/batch/core/schema-h2.sql"/>
<jdbc:script location="classpath:sql/initial-data.sql"/>
</jdbc:embedded-database>
</beans>
<!-- For MySQL database -->
<beans profile="mysql">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springblog" />
<property name="user" value="springblog" />
<property name="password" value="springblog" />
</bean>
</beans>
</beans>
In Listing 21-1, two different datasource beans are declared. Each bean is wrapped under the <beans>
tag with a profile attribute provided. This is the new profile feature introduced in Spring Framework 3.1.
So, depending on the active profile setting, Spring will initiate the dataSource bean accordingly.
When the profile is set to h2, the <jdbc:embedded-database> will initialize an embedded H2 database
and run the scripts provided within the bean definition. We need to run three scripts (note that the order
is important) for the SpringBlog application:
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home