spring tx:advice 和 aop:config 配置事务

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="   
  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  11.     <bean id="testAction" class="test.action.Stuts2ActionTest">  
  12.         <property name="service" ref="templatesService"></property>  
  13.     </bean>  
  14.   
  15.     <bean id="templatesService"  
  16.         class="test.service.impl.TaoTemplatesServiceImpl">  
  17.         <property name="dao" ref="templatesDAO" />  
  18.     </bean>  
  19.   
  20.     <bean id="templatesDAO" class="test.dao.impl.TaoTemplatesDAOImpl">  
  21.         <property name="sessionFactory" ref="sessionFactory"></property>  
  22.     </bean>  
  23.   
  24.   
  25.     <!--定义数据源-->  
  26.     <bean id="dataSource"  
  27.         class="org.apache.commons.dbcp.BasicDataSource">  
  28.         <!--   定义数据库驱动-->  
  29.         <property name="driverClassName">  
  30.             <value>oracle.jdbc.driver.OracleDriver</value>  
  31.         </property>  
  32.         <!--   定义数据库url-->  
  33.         <property name="url">  
  34.             <value>jdbc:oracle:thin:@192.168.1.96:1521:yxdb</value>  
  35.         </property>  
  36.         <!--   定义数据库用户名-->  
  37.         <property name="username">  
  38.             <value>yxuser</value>  
  39.         </property>  
  40.         <!--   定义数据库密码-->  
  41.         <property name="password">  
  42.             <value>yxuser</value>  
  43.         </property>  
  44.     </bean>  
  45.   
  46.     <!--定义一个hibernate的SessionFactory-->  
  47.     <bean id="sessionFactory"  
  48.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  49.         <!--   定义SessionFactory必须注入DataSource-->  
  50.         <property name="dataSource">  
  51.             <ref local="dataSource" />  
  52.         </property>  
  53.         <property name="mappingResources">  
  54.             <list>  
  55.                 <!--以下用来列出所有的PO映射文件-->  
  56.                 <value>test/mapping/Tao_Templates.hbm.xml</value>  
  57.             </list>  
  58.         </property>  
  59.         <property name="hibernateProperties">  
  60.             <props>  
  61.                 <prop key="hibernate.dialect">  
  62.                     org.hibernate.dialect.Oracle10gDialect   
  63.                 </prop>  
  64.                 <prop key="hibernate.show_sql">true</prop>  
  65.                 <!--此处用来定义hibernate的SessionFactory的属性:   
  66.                     不同数据库连接,启动时选择create,update,create-drop -->  
  67.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  68.             </props>  
  69.         </property>  
  70.     </bean>  
  71.   
  72.     <bean id="transactionManager"  
  73.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  74.         <property name="sessionFactory">  
  75.             <ref bean="sessionFactory" />  
  76.         </property>  
  77.     </bean>  
  78.   
  79.     <!-- 事务通知 -->  
  80.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  81.         <tx:attributes>  
  82.             <tx:method name="add*" propagation="REQUIRED" />  
  83.             <tx:method name="del*" propagation="REQUIRED" />  
  84.             <tx:method name="mod*" propagation="REQUIRED" />  
  85.             <tx:method name="*" read-only="true" />  
  86.         </tx:attributes>  
  87.     </tx:advice>  
  88.   
  89.     <!-- Spring AOP config -->  
  90.     <aop:config >  
  91.         <!-- 切入点 -->  
  92.         <aop:pointcut id="newServicesPointcut"  
  93.             expression="execution(* test.dao.impl.*.*(..))" />  
  94.         <aop:pointcut id="newServicesPointcut2"  
  95.             expression="execution(* com.yx.news.model.*.*(..))" />  
  96.         <aop:advisor advice-ref="txAdvice"  
  97.             pointcut-ref="newServicesPointcut" />  
  98.         <aop:advisor advice-ref="txAdvice"  
  99.             pointcut-ref="newServicesPointcut2" />  
  100.     </aop:config>  
  101.   
  102. </beans>  

你可能感兴趣的:(spring,AOP,事务)