Spring的AOP应用 拦截器

spring创建的bean

 

package com.b.entity;

 

import org.springframework.stereotype.Controller;

 

publicclass TestAop {

 

    publicvoid afterF(){

       System.out.println("--------之前--处理方法-----------");

    }

    publicvoid beforeF(){

       System.out.println("--------之后--处理方法-----------");

    }

}

 

 

applicationContext.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"

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

   xsi:schemaLocation="

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

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

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

      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">

    

    

     <!-- spring管理hibernate -->

     <!-- 把数据源模板注入sessionfactory-->

      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

      </bean>

     

      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

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

      </bean>

     

     <!-- spring管理事务 -->

     <!-- 配置事务管理器 -->

      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

      </bean>

     

     

      <!-- 定义advice,配置传播特性、事务隔离级别、只读事务、回滚策略 -->

      <tx:advice id="txAdvice" transaction-manager="transactionManager">

        <tx:attributes>

           <!-- get*, load*, list*, find* 不启用回滚,只读事务保持多条sql数据一致-->

           <tx:method name="get*" read-only="true"/>

           <tx:method name="load*" read-only="true"/>

           <tx:method name="list*" read-only="true"/>

           <tx:method name="find*" read-only="true"/>

           <tx:method name="insert*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>

           <tx:method name="update*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>

           <tx:method name="delete*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>

           <tx:method name="save*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>

           <tx:method name="add*" propagation="REQUIRED"rollback-for="java.lang.Exception"/>

           <tx:method name="*" propagation="REQUIRED"read-only="true"/>

        </tx:attributes>

      </tx:advice>

 

      <!-- 定义那些类使用事务 -->

      <aop:config>

        <aop:advisor pointcut="execution( * com.bd.service..*.*(..))"advice-ref="txAdvice"/>

      </aop:config>

      <!-- 配置事务结束 -->

     

      <!-- 扫描组件 -->

      <context:component-scan base-package="com.bd.service" />

      <context:component-scan base-package="com.bd.dao" />

     

      <!-- 配置一个bean 这个bean让spring创建    这个bean有点类似于拦截器的类-->

      <bean id="navFk" class="com.bd.entity.TestAop"/>

        <aop:config>

           <!--  id在下面会引用  expression为拦截方式 -->

           <aop:pointcut id="pointcut" expression="execution( *com.bd.service..*.add(..))" />

           <!--  引用刚才spring配置的bean -->

           <aop:aspect ref="navFk">

               <!--  调用  什么方法之前调用什么之后调用什么    拦截方式引用上面的 pointcut的 id-->

               <aop:before method="beforeF"pointcut-ref="pointcut"/>

               <aop:after method="afterF"   pointcut-ref="pointcut"/>

           </aop:aspect>

        </aop:config>

</beans>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值