AOP:自定义注解进行拦截

可以参考:Spring 框架的 AOP

利用Spring AOP自定义注解解决日志和签名校验:https://www.cnblogs.com/shipengzhi/articles/2716004.html

execution表达式:https://blog.csdn.net/peng658890/article/details/7223046

AspectJ语法详解:execution,within,this,@Aspect:https://blog.csdn.net/sunlihuo/article/details/52701548

1,注解形式实现

 采用注解的方式实现例子:https://www.cnblogs.com/ssslinppp/p/4633496.html

采用注解方式后就不需要再配置文件中再做配置了,重复配置可能会导致拦截多次的问题(亲测)

 

@Aspect  //定义切面类
@Component  //依赖注入
public class LogManagement {/**
     * 后置通知
     * @param point 切入点
     * @param rvt 切入点返回值
     * @param rl 切入点注解
     */
    @AfterReturning(returning="rvt",pointcut="execution(* com.uih.xx.web.xxx..*.*Controller.*(..))&&@annotation(rl)")
    public void logReturn(JoinPoint point, Object rvt,SystemControllerLog rl) {

 

2,xml配置形式实现

在spring-mvc.xml中启动Aspect  

  <bean id="ilogService" class="com.uih.xxx.web.xxx.log.xxx"></bean> //定义切面类的bean    <!-- 启动对@AspectJ注解的支持 -->    
<aop:aspectj-autoproxy/> <!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller --> <aop:aspectj-autoproxy proxy-target-class="true"/> <aop:config> <aop:aspect id="dd" ref="ilogService"> <aop:pointcut id="myPointcut" expression="execution(* com.test.school.web..*.*Controller.*(..))"/> //定义切入点,即需要拦截的类或方法 <!-- <aop:before method="log" pointcut-ref="myPointcut"/> --> <!-- <aop:after method="logArg" pointcut-ref="myPointcut"/> -->
// function3:后置通知,目标方法执行完后执行的方法体
// rvt 目标方法返回的结果
// myPointcut 需要拦截的目标方法 <aop:after-returning method="function3" returning="rvt" pointcut-ref="myPointcut"/> <!-- <aop:after-throwing method="afterThrow" throwing="ex" pointcut-ref="myPointcut"/> --> </aop:aspect> </aop:config>
posted @ 2018-09-06 13:43  弱水三千12138  阅读(839)  评论(0编辑  收藏  举报