2013-03-27 92 views
3

我有以下类别:错误在春季实例豆3.1

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中放置了一个默认构造函数,它将起作用。我不明白为什么需要默认构造函数的原因。请有人解释一下。

+0

'AbstractBusinessModule'是什么样的? – clav 2013-03-27 17:19:03

+0

当你有一个参数化的构造函数时,你不需要定义一个默认的构造函数吗? – SRy 2013-03-27 17:21:06

+0

@SrinivasR否,_if_你想要一个没有参数的构造函数_and_你也有一些需要参数的参数_then_你需要使no-arg一个显式化,但是对于一个没有默认构造函数的类来说,这很好。 – 2013-03-27 17:22:51

回答

4

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。所以,你有两种不同类型的对象这里的:

  • com.hba.MS3BusinessModule对于n情况下,每个会话/请求/什么范围,以及动态地生成代理类的
  • 一个单一实例。

ň目标豆使用接受参数,以传递给它的<constructor-arg>值构造函数构造,但代理类需要一个无参数的父类的构造函数调用(这当然可以被宣布protected而不是public)。代理机制永远不会实际调用代理实例上的任何超类方法,因为所有调用都会转到目标实例,但代理类需要扩展目标bean类以使代理成为instanceof正确的类型。

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