使用placeholder实现动态配置persistence.xml

项目用到spring data jpa 和 hibernate, 正常情况下会在src/resources下创建META-INF目录, 并在其中创建persistence.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <persistence version="2.0"
 3              xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 5             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
 6 
 7     <persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL">
 8         <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 9         <properties>
10             <!--配置Hibernate方言 -->
11             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
12             <!--配置数据库驱动 -->
13             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
14             <!--配置数据库用户名 -->
15             <property name="hibernate.connection.username" value="root" />
16             <!--配置数据库密码 -->
17             <property name="hibernate.connection.password" value="root" />
18             <!--配置数据库url -->
19             <property name="hibernate.connection.url"
20                       value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
21             <!--设置外连接抓取树的最大深度 -->
22             <property name="hibernate.max_fetch_depth" value="3" />
23             <property name="hibernate.jdbc.fetch_size" value="18" />
24             <property name="hibernate.jdbc.batch_size" value="10" />
25             <!--自动输出schema创建DDL语句 -->
26             <property name="hibernate.hbm2ddl.auto" value="update" />
27             <property name="hibernate.show_sql" value="true" />
28             <property name="hibernate.format_sql" value="true" />
29             <property name="javax.persistence.validation.mode" value="none" />
30         </properties>
31     </persistence-unit>
32 </persistence>

applicationContext.xml中

<!-- 定义实体管理器工厂 -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="myJPA" />
    </bean>

但是现在需要在applicationContext.xml中配置 dataSource, 而其中用到了 数据库地址,用户名这些信息, 为了方便, 统一使用 properties 文件保存信息, 并做如下修改

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
            http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL"/>
</persistence>

在 src/resources 下创建 config.properties 文件, 并输入配置项

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/lottery?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8
database.username=root
database.password=root

applicationContext.xml

 1 <context:property-placeholder location="classpath:config.properties" system-properties-mode="OVERRIDE"/>
 2 
 3     <!--  使用spring 加载jdbc驱动  -->
 4     <bean id="dataSourceTemplate" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 5         <property name="driverClassName" value="${database.driver}" />
 6         <property name="url" value="${database.url}" />
 7         <property name="username" value="${database.username}" />
 8         <property name="password" value="${database.password}" />
 9     </bean>
10 
11     <!--tomcat jdbc pool数据源配置-->
12     <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
13         <!--  通过引用加载数据库信息  -->
14         <property name="dataSource" ref="dataSourceTemplate"/>
15 
16         <property name="jmxEnabled" value="true"/>
17         <property name="testWhileIdle" value="true"/>
18         <property name="testOnBorrow" value="true"/>
19         <property name="testOnReturn" value="false"/>
20         <property name="validationInterval" value="30000"/>
21         <property name="validationQuery" value="SELECT 1"/>
22         <property name="timeBetweenEvictionRunsMillis" value="30000"/>
23         <property name="initialSize" value="10"/>
24         <property name="maxActive" value="40"/>
25         <property name="minIdle" value="10"/>
26         <property name="maxIdle" value="20"/>
27         <property name="maxWait" value="10000"/>
28         <property name="minEvictableIdleTimeMillis" value="30000"/>
29         <property name="logAbandoned" value="false"/>
30         <property name="removeAbandoned" value="true"/>
31         <property name="removeAbandonedTimeout" value="60"/>
32         <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/>
33     </bean>
34 
35 
36     <!-- 定义实体管理器工厂 -->
37     <bean id="entityManagerFactory"
38           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
39         <property name="persistenceUnitName" value="myJPA" />
40         <property name="dataSource" ref="dataSource" />
41         <property name="jpaVendorAdapter">
42             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
43                 <property name="showSql" value="true"/>
44                 <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
45                 <property name="generateDdl" value="true" />
46                 <property name="database" value="MYSQL" />
47             </bean>
48         </property>
49         <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
50         <property name="jpaProperties">
51             <props>
52                 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
53                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
54                 <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
55                 <prop key="hibernate.connection.url">${database.url}</prop>
56                 <prop key="hibernate.connection.username">${database.username}</prop>
57                 <prop key="hibernate.connection.password">${database.password}</prop>
58                 <prop key="hibernate.max_fetch_depth">3</prop>
59                 <prop key="hibernate.jdbc.fetch_size">18</prop>
60                 <prop key="hibernate.jdbc.batch_size">10</prop>
61                 <prop key="hibernate.hbm2ddl.auto">update</prop>
62                 <prop key="hibernate.show_sql">true</prop>
63                 <prop key="hibernate.format_sql">true</prop>
64                 <prop key="javax.persistence.validation.mode">none</prop>
65             </props>
66         </property>
67     </bean>

dataSourceTemplate 和 dataSource 这么定义是为解决 Tomcat jdbc pool 加载时找不到 Driver 类的错误. http://www.cnblogs.com/ykt8465279130/p/3669355.html

原文地址:https://www.cnblogs.com/ykt8465279130/p/3669397.html