Configuring a datasource

In Spring, you can configure a JDBC datasource either in Java code or in the XML configuration (context) file. Before we see how to configure a datasource, we need to add some dependencies in Maven. In this chapter, we will use Apache's Commons DBCP component for connection pooling (recall that in Chapter 4, Creating JEE Database Applications, we selected the Hikari connection pool). Visit https://commons.apache.org/proper/commons-dbcp/ for details on Apache DBCP. In addition to adding a dependency for Apache DBCP, we need to add dependencies for Spring JDBC and the MySQL JDBC driver. Add the following dependencies to the pom.xml of the project:

<!-- Spring JDBC --> 
<dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-jdbc</artifactId> 
  <version>${org.springframework-version}</version> 
</dependency> 
 
<!-- Apache DBCP --> 
<dependency> 
  <groupId>commons-dbcp</groupId> 
  <artifactId>commons-dbcp</artifactId> 
  <version>1.4</version> 
</dependency> 
 
<!-- MySQL --> 
<dependency> 
  <groupId>mysql</groupId> 
  <artifactId>mysql-connector-java</artifactId> 
  <version>8.0.9-rc</version> 
</dependency> 

If you want to create a datasource in Java code, you can do so as follows:

DriverManagerDataSource dataSource = new 
DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/course_management"); dataSource.setUsername("your_user_name"); dataSource.setPassword("your_password");

However, we will configure a datasource in an XML configuration file. Open servlet-context.xml (you will find it in the src/main/webapp/WEB-INF/spring/appServlet folder) and add the following bean:

<beans:bean id="dataSource" 
  class="org.apache.commons.dbcp.BasicDataSource" destroy- method="close"> 
    <beans:property name="driverClassName" 
value="com.mysql.jdbc.Driver"/> <beans:property name="url"
value="jdbc:mysql://localhost:3306/course_management" /> <beans:property name="username" value="your_user_name"/> <beans:property name="password" value="your_password"/> </beans:bean>

If you are wondering what bean means, it is the same as the component that we created in the examples earlier in the chapter. We have so far created a component using annotations, but the component and the bean can be declared in an XML file too. In fact, this is how it used to be in earlier versions, till support for annotations was added in Spring. In a real-world application, you may want to encrypt database passwords before specifying them in a configuration file. One way to decrypt a password before sending it to the database is to create a wrapper class for the datasource (in the previous example, create a wrapper for org.apache.commons.dbcp.BasicDataSource) and override the setPassword method, where you can decrypt the password.

If you want to keep the database connection parameters separate from the Spring configuration, then you can use a properties file. Spring provides a consistent way to access resources such as a properties file. Just as you can access web URLs using the http protocol prefix or the file URL using the file protocol prefix, Spring allows you to access resources in the classpath using the classpath prefix. For example, if we create a jdbc.properties file and save it in one of the folders in the classpath, then we could access it as classpath:jdbc.properties.

Visit https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#resources for detailed information on accessing resources using Spring. The Spring resource URL formats can be used in configuration files or Spring APIs where the resource location is expected.

Spring also provides a convenient tag to load property files in context config XML. You can access the values of properties in a property file in the config XML using the ${property_name} syntax.

We will move the database connection properties to a file in this example. Create jdbc.properties in the src/main/resources folder. Maven makes this folder available in the classpath, so we can access it using the Spring resource format in the XML configuration file:

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/course_management 
jdbc.username=your_user_name 
jdbc.password=your_password 

We will load this properties file from servlet-context.xml using the property-placeholder tag:

<context:property-placeholder location="classpath:jdbc.properties"/> 

Notice that the location of the property file is specified using the Spring resource format. In this case, we ask Spring to look for the jdbc.properties file in the classpath. Further, because the src/main/resources folder is in the classpath (where we saved jdbc.properties), it should be loaded by Spring.

Let's now modify the datasource bean declaration in servlet-context.xml to use the property values:

<beans:bean id="dataSource" 
  class="org.apache.commons.dbcp.BasicDataSource" destroy- method="close"> 
    <beans:property name="driverClassName" 
value="${jdbc.driverClassName}"/> <beans:property name="url" value="${jdbc.url}" /> <beans:property name="username" value="${jdbc.username}"/> <beans:property name="password" value="${jdbc.password}"/> </beans:bean>

Note that the order of the property-placeholder tag and where the properties are used does not matter. Spring loads the entire XML configuration file before replacing property references with their values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.159.223