想使用AOP Annotation配置Spring MVC的Controller进行拦截, 发现无法拦截Controller的方法, 却可以拦截Service层的方法.

一开始:

Spring的配置文件application.xml包含了 开启AOP自动代理,Service扫描配置,以及Aspect的自动扫描配置

代码1:application.xml

<aop:aspectj-autoproxy/>

<context:component-scan base-package="com.example.sdk.service">
<context:component-scan base-package="com.hodc.sdk.aspect"/>

Spring MVC的配置文件spring-mvc.xml主要内容是Controller层的自动扫描配置.

代码2:spring-mvc.xml

<context:component-scan base-package="com.hodc.sdk.controller" />

增强代码为如下:

代码3

全限定名:com.example.sdk.aspect.ControllerAspect

@Component
@Aspect
public class ControllerAspect {
@Around(value = "execution(* com.hodc.sdk.controller.json.hadoop.HadoopClusterManage.*JsonWithException(..))") //1 // @Around(value = "within(@org.springframework.stereotype.Controller *)") //2 // @Around(value = "execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))") //3
public Object test(ProceedingJoinPoint joint) { Result result = new Result(); System.out.println("in aspect");
result = (Result) joint.proceed();
return result; }

需要被拦截的方法如下:

代码4

全限定名:  com.example.sdk.controller.HadoopClusterManage#listVirtualHiveDBSJsonWithException

@RequestMapping(value = "listVirtualHiveDBS.do", produces = "application/json")
public
@ResponseBody
Result listVirtualHiveDBSJsonWithException(@RequestParam("clusterId") int clusterId) {
Result result = new Result();
List<Db> dbs = syncServiceForWeb.listVirtualHiveDBS(clusterId);
result.setData(dbs);
return result;
}

发现这样配置并没有用, 这个方法无法被拦截, 参照网上搜索的结果, 将@Around的PointCut改成代码3中2,3写法也没有作用.

参照http://stackoverflow.com/questions/17834958/spring-aop-is-not-working-in-with-mvc-structure?rq=1

因为Spring的Bean扫描和Spring-MVC的Bean扫描是分开的, 两者的Bean位于两个不同的Application, 而且Spring-MVC的Bean扫描要早于Spring的Bean扫描, 所以当Controller Bean生成完成后, 再执行Spring的Bean扫描,Spring会发现要被AOP代理的Controller Bean已经在容器中存在, 配置AOP就无效了.

同样这样的情况也存在于数据库事务中, 如果Service的Bean扫描配置在spring-mvc.xml中, 而数据库事务管理器配置在application.xml中, 会导致数据库事务失效, 原理一样.

所以这里 ,我们需要把AOP放置在Controller扫描配置的文件中.

Spring的配置文件application.xml包含了 开启AOP自动代理,Service扫描配置, 现在只包含了service的自动扫描配置

  1. 代码5:application.xml
    
    <context:component-scan base-package="com.example.sdk.service">

Spring MVC的配置文件spring-mvc.xml主要内容是Controller层的自动扫描配置,添加了开启AOP自动代理,以及Aspect的自动扫描配置

代码6:spring-mvc.xml
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.hodc.sdk.aspect"/>
<context:component-scan base-package="com.hodc.sdk.controller" />

这样配置情况下代码3的3种pointcut都是有效的,但是第一种是最精确的.

Spring AOP无法拦截Controller中的方法的更多相关文章

  1. Spring的Aspect切面类不能拦截Controller中的方法

    根本原因在于<aop:aspectj-autoproxy />这句话是在spring的配置文件内,还是在springmvc的配置文件内.如果是在spring的配置文件内,则@Control ...

  2. spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)

    首先截取了网上的一张配置execution的图片 我在项目中关于aop的配置:如果拦截controller的方法,需要在spring-mvc.xml文件中加入(如果在spring.xml中加入则无法拦 ...

  3. spring aop无法拦截类内部的方法调用

    1.概念 拦截器的实现原理就是动态代理,实现AOP机制.Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的:二是基于 CGLIB 技术而实现的.如果目标对象实现 ...

  4. Spring AOP无法拦截Controller的原因

    因为Spring的Bean扫描和Spring-MVC的Bean扫描是分开的, 两者的Bean位于两个不同的Application, 而且Spring-MVC的Bean扫描要早于Spring的Bean扫 ...

  5. Spring AOP无法拦截Controller

    参考@参考文章中的评论 首先,应该打开aop代理 <aop:aspectj-autoproxy proxy-target-class="true"/> 其次,应该讲ao ...

  6. Spring AOP获取拦截方法的参数名称跟参数值

    注意:这种方式需要JDK1.8版本支持 开始:http://www.cnblogs.com/wing7319/p/9592184.html 1.aop配置: <aop:aspectj-autop ...

  7. Spring AOP开发时如何得到某个方法内调用的方法的代理对象?

    Spring AOP开发时如何得到某个方法内调用的方法的代理对象? 问题阅读起来拗口,看代码 在方法中调用其他方法很常见,也经常使用,如果在一个方法内部调用其他方法,比如 public class U ...

  8. ASP.NET MVC4在View中调用当前Controller中的方法

    调用当前Controller中的方法 @{ ((HomeController)ViewContext.Controller).Method1(); } 调用静态方法 @{ SomeClass.Meth ...

  9. Spring AOP不拦截从对象内部调用的方法原因

    拦截器的实现原理很简单,就是动态代理,实现AOP机制.当外部调用被拦截bean的拦截方法时,可以选择在拦截之前或者之后等条件执行拦截方法之外的逻辑,比如特殊权限验证,参数修正等操作. 但是最近在项目中 ...

随机推荐

  1. canvas-8searchLight3.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 照葫芦画瓢系列之Java --- eclipse下使用maven创建Struts 2项目

    一.创建Maven项目 http://www.cnblogs.com/zhanqun/p/8425571.html 二.添加struts2核心依赖包以及其他依赖项 打开pom.xm配置界面 点击Add ...

  3. Python 标准类库-日期类型之datetime模块

    标准类库-日期类型之datetime模块    by:授客 QQ:1033553122 可用类型 3 实践出真知 4 timedelta对象 4 class datetime.timedelta(da ...

  4. vue 构建项目vue-cli

    1.首先得有node和npm的环境,node的下载:http://nodejs.org/download/.安装node之后,npm也自动生成了,显示版本号就意味着安装成功 2.接下来就是安装vue- ...

  5. UE3多参数函数实现

    基础宏定义 #define VARARG_EXTRA(A) A, #define VARARG_NONE #define VARARG_PURE =0 static inline DWORD Chec ...

  6. [20181214]open file using O_DIRECT.txt

    [20181214]open file using O_DIRECT.txt --//因为一个测试需要,需要写一个测试小例子,验证使用O_DIRECT打开文件每次都是从磁盘读取.--//没想到浪费1个 ...

  7. mssql sqlserver存储过程专题

    MSSQL存储过程简介及创建方式 mssql_DB_存储过程类型简介   MSSQL sql存储过程创建简介及应用举例 MSSQL SQl server 2008 CLR 存储过程创建举例 MSSQL ...

  8. C# DBHelper类 参考

    using System;using System.Collections.Generic;using System.Text;using System.Configuration;using Sys ...

  9. 看到一个想收藏的的AJAX小列子

    用户登录的验证可以使用 form 表单提交,也可以使用 ajax 技术异步提交. AJAX 即 Asynchronous Javascript And XML(异步 JavaScript 和 XML) ...

  10. May 29. 2018 Week 22nd Tuesday

    Nothing is more terrible than ignorance in action. 最可怕的事情莫过于无知而行动. In today's digital age, we can ru ...