java-在Spring 3.1中实例化bean时出错
作者:互联网
我有以下课程:
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
可能出什么问题了?
如果我删除了< aop:scoped-proxy />从bean的声明,它的工作原理.
更新:如果我将默认构造函数放在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类的子类,并将所有方法调用委托给目标bean的正确实例.因此,这里有两种不同的对象:
>每个会话/请求/范围的n个com.hba.MS3BusinessModule实例,以及
>动态生成的代理类的一个单例实例.
n个目标bean是使用带有参数的构造函数以及您的< constructor-arg>构造的.传递给它的值,但是代理类需要一个无参数的超类构造函数来调用(当然可以将其声明为protected而不是public).代理机制永远不会在代理实例上实际调用任何超类方法,因为所有调用都将转到目标实例,但是代理类需要扩展目标Bean类,以使代理成为对象的实例.正确的类型.
一种替代解决方案是提供M3BusinessModule实现的接口,并使其他bean对此接口的所有引用都使用接口类型而不是具体的类类型.这将允许Spring使用java.lang.reflect代理而不是CGLIB代理,从而无需扩展具体类即可实现接口.
标签:spring-aop,aop,spring,java 来源: https://codeday.me/bug/20191031/1972997.html