注解方式配置spring-aop


说明下:

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 的分享

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值