gpt4 book ai didi

java - Spring 3.1实例化bean报错

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:46 25 4
gpt4 key购买 nike

我有以下类(class):

public abstract class AbstractBusinessModule {

}

public class MS3BusinessModule extends AbstractBusinessModule
{
public MS3BusinessModule(SomeOtherClass value)
{

}
}

以及以下 bean 声明:

<bean id="ms3BusinessModule" class="com.hba.MS3BusinessModule" >
<constructor-arg index="0">
<ref bean="someOtherBeanID"/>
</constructor-arg>
<aop:scoped-proxy />
</bean>

启动我的应用程序时出现以下错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ms3BusinessModule' defined in BeanDefinition defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.hba.MS3BusinessModule]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
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:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.hba.EhCacheTest.main(EhCacheTest.java:16)
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.hba.MS3BusinessModule]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:212)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
at org.springframework.aop.scope.ScopedProxyFactoryBean.setBeanFactory(ScopedProxyFactoryBean.java:109)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1408)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 11 more
Caused by: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:721)
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:200)
... 16 more

可能出了什么问题?

如果我从 bean 声明中删除 <aop:scoped-proxy/>,它就可以工作。

更新:如果我在 MS3BusinessModule 中放置一个默认构造函数,它就可以工作。我不明白为什么需要默认构造函数的原因。有人可以解释一下吗。

最佳答案

If I put a default constructor in MS3BusinessModule, it works. I do not understand the reason why a default constructor is required. can somebody explain please.

<aop:scoped-proxy/> 的工作方式是用不同的名称隐藏“真正的”bean,并创建一个 CGLIB 代理类,它是真正的 bean 类的子类,它将所有方法调用委托(delegate)给目标 bean 的正确实例.所以这里有两种不同的对象:

  • ncom.hba.MS3BusinessModule 实例,用于每个 session /请求/无论范围是什么,以及
  • 一个动态生成的代理类的单例实例。

n 目标 bean 是使用带参数的构造函数构造的,将 <constructor-arg> 值传递给它,但是代理类需要一个无参数的父类(super class)构造函数来调用(当然可以是声明为 protected 而不是 public )。代理机制实际上永远不会调用代理实例上的任何父类(super class)方法,因为所有调用都转至目标实例,但是代理类需要扩展目标 bean 类才能使代理成为 instanceof正确的类型。

另一种解决方法是提供 M3BusinessModule 实现的接口(interface),并使其他 bean 对此 bean 的所有引用都使用接口(interface)类型而不是具体类类型。这将允许 Spring 使用 java.lang.reflect 代理而不是 CGLIB 代理,实现接口(interface)而不需要扩展具体类。

关于java - Spring 3.1实例化bean报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15664996/

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