2010.04.08(4)———spring aop 事务控制

2010.04.08(4)———spring aop 事务控制

参考:http://pftx.spaces.live.com/blog/cns!A34683C50FDB4291!571.entry
http://www.blogjava.net/robbie/archive/2009/04/05/264003.html
http://blog.csdn.net/phoenixLotus/archive/2009/03/24/4020452.aspx

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <!-- 这是我们将要配置并使它具有事务性的Service对象 -->  
  <bean id="fooService" class="x.y.service.DefaultFooService"/>
  
 //这个是事务语义
  <tx:advice id="txAdvice" transaction-manager="transactionManager">

    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->      
      <tx:method name="get*" read-only="true"/>

      <!-- other methods use the default transaction settings (see below) -->      
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>

  <!-- ensure that the above transactional advice runs for any execution
      of an operation defined by the FooService interface -->      
  <aop:config>
    <aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
  </aop:config>

  <!-- don't forget the DataSource -->  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
    <property name="username" value="scott"/>
    <property name="password" value="tiger"/>
  </bean>
  
  <!-- similarly, don't forget the (particular) PlatformTransactionManager -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>  
</beans>



如果是spring+hibernate,xml如下



<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

  <!-- 这是我们将要配置并使它具有事务性的Service对象 -->  
  <bean id="fooService" class="x.y.service.DefaultFooService"/>
  
 //这个是事务语义
  <tx:advice id="txAdvice" transaction-manager="transactionManager">

    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->      
      <tx:method name="get*" read-only="true"/>

      <!-- other methods use the default transaction settings (see below) -->      
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>

  <!-- ensure that the above transactional advice runs for any execution
      of an operation defined by the FooService interface -->      
  <aop:config proxy-target-class="false">
    <aop:advisor
		pointcut="execution(* com.huitu.khms.dao.*.*(..))"
		advice-ref="txAdvice" />
	</aop:config>
  </aop:config>

  <!-- don't forget the DataSource -->  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
    <property name="username" value="scott"/>
    <property name="password" value="tiger"/>
  </bean>

  <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
        <property name="dataSource" ref="dataSource" />    
        <property name="hibernateProperties">   
            <props>   
                <prop key="hibernate.dialect">   
                    org.hibernate.dialect.OracleDialect   
                </prop>   
                <prop key="show_sql">true</prop>   
                <prop key="hibernate.format_sql">true</prop>   
                <prop key="hibernate.use_sql_comments">true</prop>   
            </props>   
        </property>   
    </bean>
  
  <!-- similarly, don't forget the (particular) PlatformTransactionManager -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>  
</beans>













你可能感兴趣的:(spring,AOP,Hibernate,xml,.net)