2013-03-27 62 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代理,實現接口而不需要擴展具體類。