为什么为依赖bean混合Spring AOP代理机制(CGLIB和JDKDynmic)不起作用

我在Spring应用程序上下文中配置了两个公共类:

public class LoadErrorData{
  private ExceptionData exceptionData;
  public LoadErrorData() { } 
  // reminder
}

public class ExceptionData implements Serializable{
  private Resource exceptionDataResource;
  public ExceptionData() { } 
  // reminder
}

Spring applicationContext.xml:

<beans:bean id="loadErrorData" class="com.startup.LoadErrorData" init-method="startup">
  <beans:property name="exceptionData" ref="exceptionData"/> 
</beans:bean>

<beans:bean id="exceptionData" class="com.server.ExceptionData" init-method="startup">
  <beans:property name="exceptionDataResource" value="classpath:${exception.datafile.path}"/>
</beans:bean>

它在初始化时给出以下异常:

Oct 07, 2013 1:45:21 PM org.apache.catalina.core.StandardContext
listenerStart SEVERE: Exception sending context initialized event to
listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'loadErrorData' defined in class path resource
[spring/startup-config.xml]: Initialization of bean failed; nested
exception is
org.springframework.beans.ConversionNotSupportedException: Failed to
convert property value of type 'com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'com.server.IICExceptionData' for property
'exceptionData'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.server.IICExceptionData] for property
'exceptionData': no matching editors or conversion strategy found at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at
org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at
org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at
org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
at
org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
at
org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166) at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724) Caused by:
org.springframework.beans.ConversionNotSupportedException: Failed to
convert property value of type 'com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'com.server.IICExceptionData' for property
'exceptionData'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.server.IICExceptionData] for property
'exceptionData': no matching editors or conversion strategy found at
org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485)
at
org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
at
org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 19 more Caused by: java.lang.IllegalStateException: Cannot convert
value of type [com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.server.IICExceptionData] for property
'exceptionData': no matching editors or conversion strategy found at
org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
at
org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
... 25 more

但是当我放

<aop:scoped-proxy proxy-target-class="true"/>

在ExceptionData bean配置中或如果我删除

implements Serializable

来自ExceptionData

例外消失了.我的AOP切入点如下:

expression="execution(* com..*(..))"

看来Spring正在尝试为bean LoadErrorData创建CGLIB代理(因为它没有实现任何接口)和JDKdynamicProxy为其依赖ExceptionData创建(因为它实现了接口Serializable).它无法做到这一点.因为当我告诉spring为ExceptionData显式创建CGLIB代理时,它工作正常.为什么会这样?为什么它无法为bean(ExceptionData)创建JDKDynamic代理,它是bean(LoadErrorData)的依赖项,它正在尝试创建CGLIB代理?根据文档,它说Spring会自动检测它.

解决方法:

实际上,你描述的情况正是发生的事情.

LoadErrorData将成为CgLIB代理(由于没有接口.
ExceptionData将成为JDK动态代理.

但是,您的LoadErrorData需要一个类型为ExceptionData的bean,JDK动态代理只是一个Serializable(因为它只会代理接口而不是类).永远不可能将它转换为ExceptionData的实例,因此加载上下文将失败. (这也是堆栈跟踪从错误消息中告诉您的内容).

如果您强制执行基于类的代理或删除Serializable接口,它将起作用.无论哪种方式都将导致基于类的代理.

上一篇:java – @Async通过代理类在Spring 3.2中创建一个异常


下一篇:jdk动态代理和cglib动态代理的区别及分析