“AOP代理”遇到“双上下文”
最近有小伙伴儿遇到了一个问题来咨询我,问题大致如下:
他在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代理”遇到“双上下文”的更多相关文章
- [转]使用ProxyFactoryBean创建AOP代理
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ 7.5. 使用ProxyFactoryBean创建AOP代理 - Spring F ...
- spring源码 — 三、AOP代理生成
AOP代理生成 AOP就是面向切面编程,主要作用就是抽取公共代码,无侵入的增强现有类的功能.从一个简单的spring AOP配置开始: <?xml version="1.0" ...
- Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)
Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误) 问题: 今天在用AfterReturningAdvice时,a ...
- jdk动态代理与cglib代理、spring aop代理实现原理
原创声明:本博客来源与本人另一博客[http://blog.csdn.net/liaohaojian/article/details/63683317]原创作品,绝非他处摘取 代理(proxy)的定义 ...
- jdk动态代理与cglib代理、spring aop代理实现原理解析
原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...
- 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析
原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...
- JavaEE中的MVC(四)AOP代理
咱们来吹牛,JDK的动态代理在AOP(Aspect Oriented Programming,面向切面编程)中被称为AOP代理,而AOP是Spring框架中的重要组成部分. 代理模式 但是什么是代理模 ...
- AOP代理对象生成
AOP(Aspect-OrientedProgramming,面向方面编程)是OOP(Object-Oriented Programing,面向对象编程)的良好补充与完善,后者侧重于解决 从上到下的存 ...
- 记一次Spring的aop代理Mybatis的DAO所遇到的问题
由来 项目中需要实现某个订单的状态改变后然后推送给第三方的功能,由于更改状态的项目和推送的项目不是同一个项目,所以为了不改变原项目的代码,我们考虑用spring的aop来实现. 项目用的是spring ...
随机推荐
- 网友RHCE认证考试经历
2014年7月,为大力推广RedHat Enterprise Linux7操作系统,红帽将基RHCE认证由RHEL6转换到RHEL7上. 面对新的软件功能.新认证课程内容,初期认证学员的考试通过率一直 ...
- Windows常用的一些DOS命令整理
Windows常用的一些DOS命令整理.. MS DOS 命令大全 ---清空Dos屏幕使用命令:cls -- Dos切换当前目录到D盘:cd d: 一.基础命令 1 dir 无参数:查看当前所在目录 ...
- VMware Workstation 12 Pro 之安装林耐斯Ubuntu X64系统
VMware Workstation 12 Pro 之安装林耐斯Ubuntu X64系统... -------------- Linux依照国际音标应该是/'linэks/——类似于“里讷克斯&quo ...
- 【整合】input标签JS改变Value事件处理方法
某人需要在时间控件给文本框赋值时,触发事件函数.实现的效果: 1.文本框支持手工输入,通过用户输入修改值,手工输入结束后触发事件.阻塞在于失去焦点后才触发(输入过程中不触发事件) 2.通过JS方法修改 ...
- Requests抓取有道翻译结果
Requests比urllib更加方便,抓取有道翻译非常的简单. import requests class YouDao(): def __init__(self,parm): ...
- python+selenium自动化软件测试(第8章) :多线程
前戏:线程的基础 运行多个线程同时运行几个不同的程序类似,但具有以下优点:进程内共享多线程与主线程相同的数据空间,如果他们是独立的进程,可以共享信息或互相沟通更容易.线程有时称为轻量级进程,他们并不需 ...
- Android 7.0 Power 按键处理流程
Android 7.0 Power 按键处理流程 Power按键的处理逻辑由PhoneWindowManager来完成,本文只关注PhoneWindowManager中与Power键相关的内容,其他 ...
- Why I donot give up cnblogs for Jianshu
我为什么不放弃博客园使用简书 Why I donot give up cnblogs for Jianshu Chapter0 从2016年8月开始接触简书开始,就有些喜欢上简书了,因为简书支持 ma ...
- oracle时间范围查询
当时间精确到秒的指标和时间精确到日的时间进行对比是恒不等于的,但是可以判断大于或者等于的情况. 举个例子,指标[时间精确到秒] select 时间精确到秒 from table where 时间精确到 ...
- 【 DCOS 】织云 CMDB 管理引擎技术详解
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者 : 李琦 , 腾讯高级工程师 , 就职于网络平台部.曾负责公司海量运营系统的规划设计,如 TMP.Sniper.GSLB.IDCSp ...