说明下:
1. 本博文讨论的技术应用点也并非很高深的东西,其他很多地方也能找到与此近似的资料,而我写这篇也并非全部原创,也借鉴了他资料而形成,只是认为整理得稍微比较全面一点,描述中若有疑点欢迎指出:)
2. 标题其中的函数可以说指的是 MVC任意一层接口的函数;
3. 下面演示案例是“拦截web入口的控制层方法”为例。
一、首先spring aop需要的配置也需具备传统spring的配置内容,因案例是拦截 控制层方法所以 spring mvc需要的配置内容我也加上了,这些常规的配置内容不在这列出来了,大家可以在网上可以找到,但在常规配置中下面地方容易疏忽所以特别说明下:
1. spring 配置文件下边红框:
2. 与spring aop注解方式的jar包需要如下:
二、 添加aop的实现类:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Aspect
public class HttpReqeustAop {
@Pointcut("execution(* com.controller.TotalController.*(..))")
private void anyMothod() {
System.out.println("=======anyMothod=======");
}
@Before("anyMothod()")
public void before(JoinPoint joinPoint) throws Throwable {
System.out.println("====before====");
System.out.println(joinPoint);
}
@Around("anyMothod()")
public Object processs(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
Object[] args = proceedingJoinPoint.getArgs();
HttpServletRequest request = (HttpServletRequest)args[0];
String username= request.getParameter("username"); // http携带的请求参数
Object returnValue = proceedingJoinPoint.proceed(); // 方法的返回值
Object target = proceedingJoinPoint.getTarget(); //
return null;
}
}
参考的博文:
http://blog.csdn.net/a352193394/article/details/7345860
http://blog.csdn.net/cleverutd/article/details/8607491
https://my.oschina.net/itblog/blog/211693 // 其中多谢评论中 kinnonZhan 的分享