gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:53:47 25 4
gpt4 key购买 nike

我在 Spring 应用程序上下文中配置了两个公共(public)类,如下所示:

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 (因为它没有实现任何接口(interface)) 和 JDKdynamicProxy 为其依赖项 ExceptionData (因为它实现接口(interface) Serializable) 创建 CGLIB 代理。它不能这样做。因为当我告诉 spring 为 ExceptionData 显式创建 CGLIB 代理时,它工作正常。为什么会这样?为什么它不能为一个 bean(ExceptionData) 创建 JDKDynamic Proxy,它是它试图创建 CGLIB 代理的 bean (LoadErrorData) 的依赖项?根据文档,它说 Spring 会自动检测到它。

最佳答案

实际上,您描述的情况正是正在发生的事情。
LoadErrorData将成为 CgLIB 代理(由于没有接口(interface)。ExceptionData将成为 JDK 动态代理。

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

如果您对所有内容强制使用基于类的代理或删除 Serializable,它将起作用。界面。无论哪种方式都将导致基于类的代理。

关于spring - 为什么为依赖 bean 混合 Spring AOP 代理机制(CGLIB 和 JDKDynmic)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19220352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com