spring aop

 applicationContext-aop.xml 启用对@AspectJ的支持

<context:component-scan base-package="aop"/>
<!-- 启用对@AspectJ的支持 -->
<aop:aspectj-autoproxy/>
View Code

增强类 

package aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Advance {
//    /**增强方法(切入点:方法被调用前)*/
    @Before("execution(* service.*.*(..))")
    public void before(){
        //方法被调用前执行预处理
        System.out.println("预处理----------------------");
        
    }    
    
}
View Code

启动即可。

注意:

@Before("execution(* service.*.*(..))")针对service方法起作用,如果要切换成controller,直接改不生效,需要上网查找具体方法。可能是spring的bug

原文地址:https://www.cnblogs.com/yanan7890/p/8655982.html