最近有小伙伴儿遇到了一个问题来咨询我,问题大致如下:

他在Service层利用Aspect设置了一个Spring AOP代理,在单元测试以及在service层代码上添加代理的时候均没有发现问题,但是在web服务中的controller层代码添加代理的时候却不成功。

其代码大概如下:

@Component
public class CoreBusiness {
public void doSomething() {
System.out.println("I did something");
}
} @Controller
public class CoreController {
public void doSomething() {
System.out.println("I did something");
}
} @Component
@Aspect
public class CrossCuttingConcern {
@Before("execution(* com.test.CoreBusiness.*(..)) || execution(* com.test.CoreController.*(..))")
public void doCrossCutStuff(){
System.out.println("Doing the cross cutting concern now");
}
}

同时在Service层有如下的配置:

<aop:aspectj-autoproxy expose-proxy="true"/>

其实要弄清楚这个问题需要明白两点:

1、双上下文的概念

2、AOP Proxy的创建机制

我们先来看看什么情况下我们会用到双上下文

场景1:

现在假设我们有一个客户端程序,

private static ApplicationContext context = new ClassPathXmlApplicationContext("client.xml");
context.getBean(name);

在上面的程序中,我们会通过一个上下文加载所有的bean,所以不会涉及到双上下文的问题。

场景2:

接下来,我们需要开发一个web应用程序,并且使用Tomcat容器,在web.xml中我们添加了如下的配置:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

添加该配置后,web应用在启动时会加载applicationContext.xml中定义的所有bean,这里也不会涉及到双上下文。

场景3:

我们在web应用程序中引入了Spring MVC框架,并在web.xml中进行了如下的配置,

<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

如此一来,tomcat启动的时候会将springweb-servlet.xml中定义的bean进行初始化。

这个场景下依然没有双上下文的介入。

场景4:

这一次我们希望解耦web框架与底层的业务逻辑框架,因此我们又对web.xml进行了如下的修改,

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

如此一来,就会出现双上下文的情形。

因为tomcat启动时,ContextLoaderListener会初始化所有在applicationContext.xml中定义的beans。

而FrameworkServlet会初始化springweb-servlet.xml中定义的beans。

这时就出现了两个应用上下文,那么这两个上下文是什么关系呢?

我们来看看FrameworkServlet是如何初始化servlet所需要的上下文的。

	protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null; if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
......
}

我们可以看到,initWebApplicationContext方法中的第一句就是获取根上下文,有兴趣的同学可以继续跟踪下去,你会发现这个根上下文就是通过ContextLoaderListener加载的应用上下文,到这里我们可以知道这两个上下文是父子的关系。

而且,根据Spring的官方文档,我们也可以获悉到如下的信息:

As detailed in Section 3.13, “Additional Capabilities of the ApplicationContext”, ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given servlet instance.

到这里我们已经弄清楚了第一个问题,那么既然所有的child applicationcontext都可以继承root applicationcontext,为什么AOP代理在Controller层不生效呢?

其实原因很简单,因为Spring AOP Proxy是在上下文初始化的时候通过BeanPostProcessor这个扩展点创建的。而aspectj autoproxy的配置是放在Service层的,那么也就是根上下文初始化的时候会创建相应的AOP proxy,当Controller层代码所在的servlet webapplicationcontext初始化时,AOP proxy已经创建完毕了。这时servlet webapplicationcontext并不会感知到根上下文中创建的AOP proxy。到这里为止就出现了文中开始处提到问题。

问题的原因已经找到了,那么我们该如何进行处理呢?还是像以往一样,留给读者自己思考吧。

												

“AOP代理”遇到“双上下文”的更多相关文章

  1. [转]使用ProxyFactoryBean创建AOP代理

    http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ 7.5. 使用ProxyFactoryBean创建AOP代理 - Spring F ...

  2. spring源码 — 三、AOP代理生成

    AOP代理生成 AOP就是面向切面编程,主要作用就是抽取公共代码,无侵入的增强现有类的功能.从一个简单的spring AOP配置开始: <?xml version="1.0" ...

  3. Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)

    Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误) 问题: 今天在用AfterReturningAdvice时,a ...

  4. jdk动态代理与cglib代理、spring aop代理实现原理

    原创声明:本博客来源与本人另一博客[http://blog.csdn.net/liaohaojian/article/details/63683317]原创作品,绝非他处摘取 代理(proxy)的定义 ...

  5. jdk动态代理与cglib代理、spring aop代理实现原理解析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  6. 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  7. JavaEE中的MVC(四)AOP代理

    咱们来吹牛,JDK的动态代理在AOP(Aspect Oriented Programming,面向切面编程)中被称为AOP代理,而AOP是Spring框架中的重要组成部分. 代理模式 但是什么是代理模 ...

  8. AOP代理对象生成

    AOP(Aspect-OrientedProgramming,面向方面编程)是OOP(Object-Oriented Programing,面向对象编程)的良好补充与完善,后者侧重于解决 从上到下的存 ...

  9. 记一次Spring的aop代理Mybatis的DAO所遇到的问题

    由来 项目中需要实现某个订单的状态改变后然后推送给第三方的功能,由于更改状态的项目和推送的项目不是同一个项目,所以为了不改变原项目的代码,我们考虑用spring的aop来实现. 项目用的是spring ...

随机推荐

  1. .NET平台微服务项目汇集

    最近博客园出现了一篇文章<微服务时代之2017年五军之战:Net PHP谁先死>,掀起了一波撕逼,作者只是从一个使用者的角度来指点江山,这个姿势是不对的..NET Core就是专门针对模块 ...

  2. LaTeX的表格插入与排版

    关于LaTex中的图表问题的几篇有用的文章 Graphics and Colour with LaTeX  有关在LaTex中插图和使用颜色的在线指导. Figure'ing and Picture' ...

  3. 如何为ubuntu server 版本 安装图形界面

    强烈建议使用命令行的交互方式,就算你在server上装了图形界面,但是在Linux上,大部分重要的操作还是要通过命令行的方式进行操作. 如何为ubuntu server 版本 安装图形界面 Ubunt ...

  4. python+selenium自动化软件测试(第13章):selenium面试题

    前言最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下 一.selenium中如何判断元素是否存在?expected_conditions模块提供了16种判断方法 ...

  5. 第一篇--认识Jmeter

    Jmeter是Apache组织开发的基于Java的压力测试工具,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文件.Java 小服务程序.CGI 脚 ...

  6. javascript如何用递归写一个简单的树形结构

    现在有一个数据,需要你渲染出对应的列表出来: var data = [ {"id":1}, {"id":2}, {"id":3}, {&qu ...

  7. spring mvc:Error scanning entry module-info.class from jar错误

    项目从jdk1.6升级到jdk1.8,启动的时候出现如下错误: java.lang.RuntimeException: Error scanning entry module-info.class f ...

  8. 关于querySelector 和 document.getElementsByTagName 选中集合问题

    本文解决的问题是 :运用for..of..循环时,edge浏览器报Object doesn't support property or method 'symbol.iterator'问题 以及 符号 ...

  9. Kafka中操作topic时 Error:Failed to parse the broker info from zookeeper

      Kafka中操作topic时 Error: Failed to parse the broker info from zookeeper 1.问题描述   2.问题原因     kafka在启动后 ...

  10. Maven简述

    一.前言     以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar 包的.最近一段时间在项目过程中使用Maven,用Maven构建的web项目,其项目结构只停留在 ...