Spring framework (3) - Spring configuration data source

Common data source (connection pooling) have: DBCP, C3P0, Druid, and so on.

In C3P0 data source (C3P0 connection pool), for example, demonstrates Spring configuration "non-custom object".

Spring configuration steps C3P0 link pool:

1. Import Driver coordinate data source coordinates and the database

 1 <!-- C3P0连接池 -->
 2 <dependency>
 3     <groupId>c3p0</groupId>
 4     <artifactId>c3p0</artifactId>
 5     <version>0.9.1.2</version>
 6 </dependency>
 7 
 8 <!-- mysql驱动 -->
 9 <dependency>
10     <groupId>mysql</groupId>
11     <artifactId>mysql-connector-java</artifactId>
12     <version>5.1.39</version>
13 </dependency>

2. Set the data source basic data link

Divided into pools and manually create links C3P0 C3P0 connection pooling using Spring configuration:

  Use Spring configuration: Configuration in the core configuration file as follows:

1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
2     <property name="driverClass" value="com.mysql.jdbc.Driver"/>
3     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
4     <property name="user" value="root"/>
5     <property name="password" value="root"/>
6 </bean>

  Optimization configured to: extract the value of the value as a new profile jdbc.properties

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/test
3 jdbc.username=root
4 jdbc.password=root

  Then modify the kernel configuration file contents: the introduction of the profile in the core 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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">
      
    <!- introduction of the classpath jdbc.properties file ->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>


</beans>

  Manual: the test write operation class, generally do not use this method.

. 1  @Test
 2  public  void testC3P0 () throws Exception {
 . 3      // create a data source 
. 4      ComboPooledDataSource the dataSource = new new ComboPooledDataSource ();
 . 5      // set the database connection parameters 
. 6      dataSource.setDriverClass ( " com.mysql.jdbc.Driver " );
 . 7      dataSource.setJdbcUrl ( " JDBC: MySQL: // localhost: 3306 / Test " );
 . 8      dataSource.setUser ( " the root " );
 . 9      dataSource.setPassword ( " the root ");
 10      // get connection object 
. 11      Connection Connection = dataSource.getConnection ();
 12 is      the System. OUT .println (Connection);
 13 is }

3. Create a data source object, using a data source to obtain connection resources and the return of connection resources

  Written in the test class:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);

  You can create a data source experiment was successful.

Guess you like

Origin www.cnblogs.com/j9527/p/12031560.html