---
title: "电子商务网站的Spring+hibernate配置文件(AOP事务)"
author: "liuye"
date: "2008-08-06T09:15:26Z"
tags: []
slug: "spring-hibernate-aop"
draft: false
lastmod: "2008-08-06T09:15:26Z"
id: 224855
url: "https://liuye.iteye.com/blog/224855"
---

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) "  
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance) "  
    xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) "  
    xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) "  
    xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
           [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.0.xsd](http://www.springframework.org/schema/aop/spring-aop-2.0.xsd)  
           [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.0.xsd](http://www.springframework.org/schema/tx/spring-tx-2.0.xsd) "  
    default-autowire="byName" default-lazy-init="true"&gt;

&lt;!-- 数据源定义,使用Apache DBCP 连接池 --&gt;  
&lt;bean id="baseDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;  
   &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;  
   &lt;property name="url" value="${jdbc.url}"/&gt;  
   &lt;property name="username" value="${jdbc.username}"/&gt;  
   &lt;property name="password" value="${jdbc.password}"/&gt;  
   &lt;property name="maxActive" value="${manage.datasource.maxActive}" /&gt;  
   &lt;property name="maxIdle" value="30" /&gt;  
   &lt;property name="maxWait" value="1000" /&gt;  
   &lt;property name="defaultAutoCommit" value="true" /&gt;  
   &lt;property name="removeAbandoned" value="true" /&gt;  
   &lt;property name="removeAbandonedTimeout" value="60" /&gt;  
   &lt;property name="validationQuery" value="select 1 "/&gt;  
   &lt;property name="testOnBorrow" value="true"/&gt;  
&lt;/bean&gt;

&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/&gt;

&lt;!-- applicationContext.xml --&gt;  
&lt;!-- 属性文件读入 --&gt;  
&lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;  
   &lt;property name="locations"&gt;  
    &lt;list&gt;  
     &lt;value&gt;classpath\*:config/jdbc.properties&lt;/value&gt;  
     &lt;value&gt;classpath\*:config/hibernate.properties&lt;/value&gt;  
     &lt;value&gt;classpath\*:config/manage-config.properties&lt;/value&gt;  
    &lt;/list&gt;  
   &lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- 支持 @Transactional 标记 --&gt;  
&lt;tx:annotation-driven/&gt;

&lt;!-- 支持 @AspectJ 标记--&gt;  
&lt;aop:aspectj-autoproxy/&gt;

&lt;!-- 以AspectJ方式 定义 AOP --&gt;  
&lt;aop:config proxy-target-class="true"&gt;  
   &lt;aop:advisor pointcut="execution(* com.sitechasia.webshop..\*ServiceImpl.\*(..))"  
      advice-ref="txAdvice"/&gt;  
&lt;/aop:config&gt;

&lt;!-- 基本事务定义,使用transactionManager作事务管理,默认get\*方法的事务为readonly,其余方法按默认设置.  
    默认的设置请参考Spring文档事务一章. --&gt;  
&lt;tx:advice id="txAdvice"&gt;  
   &lt;tx:attributes&gt;  
    &lt;tx:method name="get\*" read-only="true" /&gt;  
    &lt;tx:method name="find\*" read-only="true" /&gt;  
    &lt;tx:method name="load\*" read-only="true" /&gt;  
    &lt;tx:method name="search\*" read-only="true" /&gt;  
    &lt;tx:method name="\*" propagation="REQUIRED"/&gt;  
   &lt;/tx:attributes&gt;  
&lt;/tx:advice&gt;

&lt;!--多数据源配置 --&gt;  
&lt;!--  
&lt;aop:config proxy-target-class="true"&gt;  
   &lt;aop:advisor pointcut="execution(* javax.sql.DataSource.getConnection(..))"  
      advice-ref="connectionInterceptor"/&gt;  
&lt;/aop:config&gt;  
&lt;bean id="connectionInterceptor" class="com.sitechasia.webshop.common.persistence.ConnectionInterceptor"&gt;  
    &lt;property name="excludes" &gt;  
     &lt;list&gt;  
      &lt;value&gt;${commondb.common}&lt;/value&gt;      
     &lt;/list&gt;  
    &lt;/property&gt;  
&lt;/bean&gt;  
\--&gt;  
&lt;bean id="cacheConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;  
   &lt;property name="locations"&gt;  
    &lt;list&gt;  
     &lt;value&gt;classpath\*:config/cache.properties&lt;/value&gt;  
    &lt;/list&gt;  
   &lt;/property&gt;  
   &lt;property name="placeholderPrefix" value="#{"/&gt;  
    &lt;/bean&gt;

&lt;!-- dataAcessContext-hibernate.xml --&gt;  
&lt;bean id="dataSource" parent ="baseDataSource"/&gt;

&lt;bean id="databaseMapping" class=" com.sitechasia.webshop.common.persistence.DomainDatabaseMapping"&gt;  
&lt;/bean&gt;  
&lt;bean id="webshopDS" class="com.sitechasia.webshop.common.persistence.MultiDataSource"&gt;  
   &lt;property name="dataSource" ref="dataSource"/&gt;  
   &lt;property name="databaseMapping" ref="databaseMapping"/&gt;  
&lt;/bean&gt;  
&lt;!--Hibernate SessionFatory--&gt;

&lt;bean id="sessionFactory" class="com.sitechasia.webx.core.dao.hibernate3.NamedMoudleHbmLocalSessionFactoryBean"&gt;  
   &lt;property name="dataSource" ref="webshopDS"/&gt;  
   &lt;property name="configLocations"&gt;  
    &lt;list&gt;  
    &lt;/list&gt;  
   &lt;/property&gt;  
   &lt;property name="hibernateProperties"&gt;  
    &lt;props&gt;  
     &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;  
     &lt;prop key="hibernate.show\_sql"&gt;${hibernate.show\_sql}&lt;/prop&gt;  
     &lt;prop key="hibernate.cache.use\_query\_cache"&gt;${hibernate.cache.use\_query\_cache}&lt;/prop&gt;  
     &lt;prop key="hibernate.cache.provider\_class"&gt;${hibernate.cache.provider\_class}&lt;/prop&gt;  
     &lt;prop key="hibernate.connection.release\_mode"&gt;${hibernate.connection.release\_mode}&lt;/prop&gt;  
       &lt;prop key="memcached.servers"&gt;#{memcached.servers}&lt;/prop&gt;  
     &lt;prop key="memcached.failover"&gt;#{memcached.failover}&lt;/prop&gt;  
     &lt;prop key="memcached.initConn"&gt;#{memcached.initConn}&lt;/prop&gt;  
     &lt;prop key="memcached.minConn"&gt;#{memcached.minConn}&lt;/prop&gt;  
     &lt;prop key="memcached.maxConn"&gt;#{memcached.maxConn}&lt;/prop&gt;  
     &lt;prop key="memcached.maintSleep"&gt;#{memcached.maintSleep}&lt;/prop&gt;  
     &lt;prop key="memcached.nagle"&gt;#{memcached.nagle}&lt;/prop&gt;  
     &lt;prop key="memcached.socketTO"&gt;#{memcached.socketTO}&lt;/prop&gt;  
     &lt;prop key="memcached.aliveCheck"&gt;#{memcached.aliveCheck}&lt;/prop&gt;  
    &lt;/props&gt;  
   &lt;/property&gt;  
   &lt;property name="mappingResourceServices"&gt;  
     &lt;value&gt;true&lt;/value&gt;  
   &lt;/property&gt;  
&lt;/bean&gt;

&lt;!--Hibernate TransactionManager--&gt;  
&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
   &lt;property name="sessionFactory" ref="sessionFactory"/&gt;  
&lt;/bean&gt;

&lt;!-- for common db --&gt;  
&lt;bean id="baseDSDBCP2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;  
   &lt;property name="driverClassName" value="${commondb.jdbc.driverClassName}" /&gt;  
   &lt;property name="url" value="${commondb.jdbc.url}" /&gt;  
   &lt;property name="username" value="${commondb.jdbc.username}" /&gt;  
   &lt;property name="password" value="${commondb.jdbc.password}" /&gt;  
   &lt;property name="maxActive" value="${manage.commonDatasource.maxActive}" /&gt;  
   &lt;property name="maxIdle" value="30" /&gt;  
   &lt;property name="maxWait" value="1000" /&gt;  
   &lt;property name="defaultAutoCommit" value="true" /&gt;  
   &lt;property name="removeAbandoned" value="true" /&gt;  
   &lt;property name="removeAbandonedTimeout" value="60" /&gt;  
   &lt;property name="validationQuery" value="select 1 "/&gt;  
   &lt;property name="testOnBorrow" value="true"/&gt;  
&lt;/bean&gt;  
&lt;bean id="baseDataSource2" class="com.sitechasia.webx.components.datasource.proxy.BeanModeDomainAwareDataSource"&gt;  
   &lt;property name="defaultDataSource" ref="baseDSDBCP2" /&gt;  
&lt;/bean&gt;

&lt;bean id="commonDS" parent="baseDataSource2"/&gt;

&lt;bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
   &lt;property name="applicationContextSchedulerContextKey"&gt;  
     &lt;value&gt;applicationContext&lt;/value&gt;  
    &lt;/property&gt;  
    &lt;property name="dataSource"&gt;  
     &lt;ref bean="commonDS"/&gt;  
    &lt;/property&gt;  
    &lt;property name="autoStartup" value="${ProductMode}"/&gt;  
&lt;/bean&gt;

&lt;!-- Transaction Interceptor set up to do PROPOGATION\_REQUIRED on all methods --&gt;  
&lt;bean id="matchAllTxInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
   &lt;property name="transactionManager"&gt;  
    &lt;ref bean="transactionManager2" /&gt;  
   &lt;/property&gt;  
   &lt;property name="transactionAttributes"&gt;  
    &lt;props&gt;  
     &lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
     &lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
     &lt;prop key="load\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
     &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
    &lt;/props&gt;  
   &lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="autoProxyCreator2" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
   &lt;property name="interceptorNames"&gt;  
    &lt;list&gt;  
     &lt;idref local="matchAllTxInterceptor2" /&gt;  
    &lt;/list&gt;  
   &lt;/property&gt;  
   &lt;property name="proxyTargetClass"&gt;  
    &lt;value&gt;true&lt;/value&gt;  
   &lt;/property&gt;  
   &lt;property name="beanNames"&gt;  
    &lt;list&gt;  
     &lt;value&gt;com.sitechasia.webshop.rsscommon.service.\*\*&lt;/value&gt;      
    &lt;/list&gt;  
   &lt;/property&gt;  
&lt;/bean&gt;

    
&lt;!--Hibernate SessionFatory--&gt;  
&lt;bean id="sessionFactory2" class="com.sitechasia.webx.core.dao.hibernate3.NamedMoudleHbmLocalSessionFactoryBean"&gt;  
   &lt;property name="dataSource" ref="commonDS" /&gt;  
   &lt;property name="mappingResources"&gt;  
    &lt;list&gt;  
     &lt;value&gt;modules/rsscommon/hibernate-conf/DsRssSourceDO.hbm.xml&lt;/value&gt;  
     &lt;value&gt;modules/rsscommon/hibernate-conf/RssSource.hbm.xml&lt;/value&gt;  
     &lt;value&gt;modules/rsscommon/hibernate-conf/RssInfo.hbm.xml&lt;/value&gt;  
    &lt;/list&gt;  
   &lt;/property&gt;  
   &lt;property name="hibernateProperties"&gt;  
    &lt;props&gt;  
     &lt;prop key="hibernate.dialect"&gt;${commondb.hibernate.dialect}&lt;/prop&gt;  
     &lt;prop key="hibernate.show\_sql"&gt;${commondb.hibernate.show\_sql}&lt;/prop&gt;  
     &lt;prop key="hibernate.cache.use\_query\_cache"&gt;${commondb.hibernate.cache.use\_query\_cache}&lt;/prop&gt;  
     &lt;prop key="hibernate.cache.provider\_class"&gt;${commondb.hibernate.cache.provider\_class}&lt;/prop&gt;  
       &lt;prop key="memcached.servers"&gt;#{memcached.servers}&lt;/prop&gt;  
     &lt;prop key="memcached.failover"&gt;#{memcached.failover}&lt;/prop&gt;  
     &lt;prop key="memcached.initConn"&gt;#{memcached.initConn}&lt;/prop&gt;  
     &lt;prop key="memcached.minConn"&gt;#{memcached.minConn}&lt;/prop&gt;  
     &lt;prop key="memcached.maxConn"&gt;#{memcached.maxConn}&lt;/prop&gt;  
     &lt;prop key="memcached.maintSleep"&gt;#{memcached.maintSleep}&lt;/prop&gt;  
     &lt;prop key="memcached.nagle"&gt;#{memcached.nagle}&lt;/prop&gt;  
     &lt;prop key="memcached.socketTO"&gt;#{memcached.socketTO}&lt;/prop&gt;  
     &lt;prop key="memcached.aliveCheck"&gt;#{memcached.aliveCheck}&lt;/prop&gt;  
    &lt;/props&gt;  
   &lt;/property&gt;  
   &lt;property name="mappingResourceServices"&gt;  
    &lt;value&gt;false&lt;/value&gt;  
   &lt;/property&gt;  
&lt;/bean&gt;

&lt;!--Hibernate TransactionManager--&gt;  
&lt;bean id="transactionManager2"          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
   &lt;property name="sessionFactory" ref="sessionFactory2" /&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;