Spring中Aspectj和Schema-based AOP混用引起的错误

         前几天要在项目中增加一个新功能用来监控某些模块的运行情况,自然就想到了使用Spring的AOP来实现。之前已经有类似的AOP代码,使用的是Schema-based形式配置的,也就是在Spring的ApplicationContext.xml中加入了:

       <bean id="handlerBeanNameAutoProxyCreator"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>sampleService</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>sampleAdvice</value>
			</list>
		</property>
	</bean>        

其中sampleService和sampleAdvice都是通过:

<context:component-scan base-package="com.nokia.myapp"/>

自动引入的。

        这次要加的功能需要监控很多类,如果按照这个配置,就需要在beanNames的list中增加很多bean,并且还要修改原来的sampleAdvice代码判断是否是属于sampleService,无疑增加了程序的复杂度,这不是我想要的。这时就考虑使用AspectJ的Annotation,编程迅速,不会破坏现在的代码结构,也易于以后的维护。按照Spring Reference Document中@AspectJ Support章节的例子,很快写好了代码:

package com.nokia.myapp.aop.monitor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class NewAspectjAOP{
	private Logger logger = LoggerFactory.getLogger("profile");

	@Around("execution(* com.nokia.myapp.client..*.*(..))")
	public Object serviceProfile(ProceedingJoinPoint pjp) throws Throwable {
		//Do something before method execution

		Object ret = null;
		Throwable th = null;

		try {
			ret = pjp.proceed();
		} catch(Throwable thr){
			th = thr;
		}

		//Do something after method execution

		if(th != null){
			throw th;
		}

		return ret;
	}
}

提交代码到测试服务器上进行测试,结果启动时就遇到了第一个问题:

 ERROR [org.springframework.web.servlet.DispatcherServlet] [Thread-2] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service1' defined in file [/opt/myapp/bin/WEB-INF/classes/com/nokia/myapp/Service1.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.nokia.myapp.Service1]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'service2': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nokia.myapp.Service1 com.nokia.myapp.Service2.locationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'service1': Requested bean is currently in creation: Is there an unresolvable circular reference?
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
……

        根据提示查看代码,发现在Service2中通过@Autowired引入了Service1,而在Service1中也通过@Autowired引入了Service2。于是去掉了Service2在Service1中引用,而后在需要用的时候通过ApplicationContext.getBean(String beanName)获取Service2。

        修正了上面的错误,再次测试,程序可以正常启动,但是有些程序运行出现了奇怪的问题:每次ApplicationConext.getBeansOfType(Class class)获取sampleService时,总是会获取到其他的Bean,导致程序运行异常(不过本地Eclipse中运行测试没有问题)。使用Eclipse的Remote Debug功能连到开发服务器上调试,发现其他类getClass()获取的都是class com.nokia.myapp.Service2$$EnhancerByCGLIB$$ccadc5e,而SampleService却是$Proxy119。百思不得其解。

        最后请教了对Spring框架很熟悉的同事,终于发现了问题的原因:

由于@AspectJ声明的AOP和在Spring的配置文件中配置的AOP都监测了同一个类SampleService。于是在运行过程中,SampleService实现了CGLIB的三个接口,而根据Spring Reference Document中Proxying mechanisms

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice).

If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.

Spring又在实现了接口的SampleService的代理类外面包了JDK的代理实现,所以就造成了这个问题。
          找到了问题,修复就很容易了,只需要指定ProxyFactoryBean的proxyTargetClass属性:

<bean id="handlerBeanNameAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" p:proxyTargetClass="true">
时间: 2024-09-21 08:41:50

Spring中Aspectj和Schema-based AOP混用引起的错误的相关文章

Spring中使用AspectJ实现AOP

一,一些基本概念                Spring除了IOC容器之外,另一大核心就是AOP了.Spring 中AOP是通过AspectJ来实现的.                   首先来看下AOP 的相关概念:        1,Aspect                     对横切性关注点的模块化.         2,Advice                      对横切性关注点的具体实现        3,Cross Cutting Concern       

Spring中基于aop命名空间的AOP 一(一点准备工作和一个例子)

在某些时候,我们工程中使用的JDK 不一定就是1.5 以上,也就是说可能不支持Annotation 注解,这时自然也就不能使用@AspectJ 注解驱动的AOP 了,那么如果我们仍然想使用AspectJ 灵活的切入点表达式,那么该如何呢?Spring 为我们提供了基于xml schematic 的aop 命名空间,它的使用方式和@AspectJ 注解类似,不同的是配置信息从注解中转移到了Spring 配置文件中.在这里,我们将详细介绍如何使用Spring 提供的<aop:config/> 标签

Spring中的AOP详解

本文摘自pandonix的博客.   此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,采用AOP来解决.一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习Spring AOP相关的内容.本文是权当本人的自己AOP学习笔记,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智. 面对需求: ①对部分函数的调用进行日志记录,用于观察特定问题在运行过程中的函数调用情况. ②监控部分重要函数,若抛出指定的异

Spring中的AOP(三)——基于Annotation的配置方式(一)

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的编译器或者织入器,底层依然使用SpringAOP来实现,依然是在运行时动态生成AOP代理,因此不需要增加额外的编译,也不需要AspectJ的织入器支持.而AspectJ采用编译时增强,所以AspectJ需要使用自己的编译器来编译Java文件,还需要织入器.     为了启用Spring对@A

Spring中的AOP(七)——基于XML配置文件方式的AOP

    除了前面介绍的基于JDK1.5的注解方式来定义切面,切入点和增强处理外,Spring AOP也允许直接使用XML配置文件来管理它们.在JDK1.5之前,只能使用配置文件的方式来管理,在Spring2.X后提供了一个新的aop命名空间来定义切面.切入点和增强处理.     相比之下,使用XML配置文件方式有如下优点: 如果没有使用JDK1.5以上版本,只能使用XML配置文件的方式 对早期的Spring用于来说更加习惯,而且这种方式允许使用纯粹的POJO来支持AOP 采用XML配置方式时,我

Spring中的AOP(二)——AOP基本概念和Spring对AOP的支持

AOP的基本概念     AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤,希望以更好的方式来组合业务逻辑的各个步骤.AOP框架并不与特定的代码耦合,AOP框架能处理程序执行中特定切入点,而不与具体某个类耦合(即在不污染某个类的情况下,处理这个类相关的切点).下面是一些AOP的一些术语:     切面(Aspect):业务流程运行的某个特定步骤,也就是应用运行过程的关注点,关注点通常会横切多个对象,因此常被称为横切关注点     连接点(JoinPoi

Spring中的AOP(六)——定义切入点和切入点指示符

定义切入点     在前文(点击查看)中使用到的AdviceTest类中同一个切点(即* com.abc.service.*.advice*(..)匹配的连接点)却重复定义了多次,这显然不符合软件设计的原则,为了解决这个问题,AspectJ和Spring都提供了切入点的定义.所谓定义切入点,其实质就是为一个切入点表达式起一个名称,从而允许在多个增强处理中重用该名称.     Spring AOP只支持以Spring Bean的方法执行组作为连接点,所以可以把切入点看作所有能和切入表达式匹配的Be

Spring中的AOP(五)——定义切入点和切入点指示符

定义切入点     在前文(点击查看)中使用到的AdviceTest类中同一个切点(即* com.abc.service.*.advice*(..)匹配的连接点)却重复定义了多次,这显然不符合软件设计的原则,为了解决这个问题,AspectJ和spring都提供了切入点的定义.所谓定义切入点,其实质就是为一个切入点表达式起一个名称,从而允许在多个增强处理中重用该名称.     Spring AOP只支持以Spring Bean的方法执行组作为连接点,所以可以把切入点看作所有能和切入表达式匹配的Be

谈谈Spring中的IOC和AOP概念

  IOC和AOP是spring中的两个核心的概念,下面谈谈对这两个概念的理解. 1. IOC(Inverse of Control):控制反转,也可以称为依赖倒置.         所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果不倒置,意思就是A主动获取B的实例:B b = new B(),这就是最简单的获取B实例的方法(当然还有各种设计模式可以帮助你去获得B的实