天天看点

spring jpa 事物配置

<?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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"

default-lazy-init="true">

<description>Spring公共配置</description>

<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->

<context:component-scan base-package="com.lq">

<context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

<context:exclude-filter type="annotation"

expression="org.springframework.web.bind.annotation.ControllerAdvice" />

</context:component-scan>

<aop:aspectj-autoproxy />

<!-- Jpa Entity Manager 配置 -->

<bean id="entityManagerFactory"

class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />

<property name="packagesToScan" value="com.lq" />

<property name="jpaProperties">

<props>

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory

</prop>

<prop key="net.sf.ehcache.configurationResourceName">cache/ehcache-hibernate-local.xml</prop>

<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>

<!-- 命名规则 My_NAME->MyName -->

<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>

</props>

</property>

</bean>

<bean id="hibernateJpaVendorAdapter"

class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

<property name="databasePlatform">

<bean factory-method="getDialect" class="Dialect">

<constructor-arg ref="dataSource" />

</bean>

</property>

</bean>

<!-- Spring Data Jpa配置 -->

<jpa:repositories base-package="com.lq"

transaction-manager-ref="transactionManager"

entity-manager-factory-ref="entityManagerFactory" />

<!-- Jpa 事务配置 -->

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

<property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<!-- 使用annotation定义事务 -->

<tx:annotation-driven transaction-manager="transactionManager"

proxy-target-class="true" />

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="order" value="1" />

<property name="ignoreUnresolvablePlaceholders" value="true" />

<property name="ignoreResourceNotFound" value="true" />

<property name="fileEncoding" value="utf-8" />

<property name="location">

<value>classpath:/application.properties</value>

</property>

</bean>

<!-- 数据源配置,使用应用内的Tomcat JDBC连接池 -->

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"

destroy-method="close">

<property name="driverClassName" value="${jdbc.driver}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

<property name="maxActive" value="${jdbc.pool.maxActive}" />

<property name="minIdle" value="${jdbc.pool.minIdle}" />

<property name="maxIdle" value="${jdbc.pool.maxIdle}" />

<property name="defaultAutoCommit" value="false" />

<!-- 连接Idle10分钟后超时,每1分钟检查一次 -->

<property name="timeBetweenEvictionRunsMillis" value="60000" />

<property name="minEvictableIdleTimeMillis" value="600000" />

<property name="testWhileIdle" value="true" />

<property name="validationQuery" value="select 1" />

</bean>

<!-- 线程执行器配置 -->

<bean id="springExecutor"

class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

<property name="corePoolSize" value="15" />

<property name="maxPoolSize" value="150" />

<property name="queueCapacity" value="1000" />

</bean>

</beans>