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">


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值