2012-02-15 164 views
12

我有我的春天控制器的問題 - 我越來越沒有默認構造函數發現 - 但他們確實有這我試圖通過創建applicationContext.xml的構造 - 繼承人的相關位:Spring MVC沒有找到默認構造函數?

<bean id="PcrfSimulator" class="com.rory.services.pcrf.simulator.PcrfSimulator" init-method="start"> 
</bean> 

<bean id="CacheHandler" class="com.rory.services.pcrf.simulator.handlers.CacheHandler"> 
    <constructor-arg index="0" type="com.rory.services.pcrf.simulator.CustomGxSessionIdCacheImpl"> 
     <bean factory-bean="PcrfSimulator" factory-method="getGxSessionIdCache"> 
     </bean> 
    </constructor-arg>  
</bean> 
即,

也就是說。我先創建一個bean,然後試圖將該bean的方法調用的結果傳遞給第二個bean的(CacheHandler)構造函數。

Here'e CacheHandler開始:

@Controller 
    public class CacheHandler { 

    private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

    public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) { 
     this.gxSessionIdCache = gxSessionIdCache; 
    } 

這裏是我得到的錯誤:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheHandler' defined in URL [jar:file:/users/rtorney/Documents/apache-tomcat-7.0.25/webapps/PCRFSimulator-4.0/WEB-INF/lib/PCRFSimulator-4.0.jar!/com/rory/services/pcrf/simulator/handlers/CacheHandler.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.rory.services.pcrf.simulator.handlers.CacheHandler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.rory.services.pcrf.simulator.handlers.CacheHandler.<init>() 

任何幫助,非常感謝!

回答

20

您應該定義你的bean在XML或註釋它們,而不是兩個(如果僅僅是爲了避免像你得到一個錯誤)。

這裏的問題是,你沒有自動裝配構造ARGS,所以春季不知道你的控制器做什麼。它知道它必須創建一個bean(@Controller註釋),但它不知道如何(沒有默認值,也沒有自動裝配構造函數)。

你可以試着這樣做:

@Controller 
public class CacheHandler { 

private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

@Autowired 
public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) { 
    this.gxSessionIdCache = gxSessionIdCache; 
} 

,然後在XML:

<bean id="gxSessionIdCache" 
    factory-bean="PcrfSimulator" 
    factory-method="getGxSessionIdCache"/> 

所以它會自動裝配構造函數的參數。

另一種選擇是簡單地創建默認構造函數和自動裝配gxSessionIdCache屬性。

+0

我們有着同樣的想法;-) – 2012-02-15 16:25:36

+0

大,感謝所有的答案! – Rory 2012-02-15 16:30:51

+1

很好的證實了這一點,spring需要默認的構造函數或者自動構造函數。 – lwpro2 2013-01-04 08:54:00

2

您必須添加一個空的默認構造函數:

@Controller 
    public class CacheHandler { 

    private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

    @Autowired 
    public CacheHandler(CustomGxSessionIdCacheImpl gxSessionIdCache) { 
     this.gxSessionIdCache = gxSessionIdCache; 
    } 

但要小心,因爲它看來你是混合基於註解配置(@Controller)和XML配置。在上面的示例中,它使用基於註釋的配置(所以請從XML文件中刪除bean聲明)。

+1

爲什麼默認的構造函數?這不是強制性的權利? – kosa 2012-02-15 16:12:37

+0

我剛剛編輯了我的帖子。 – 2012-02-15 16:14:24

+0

[Spring是否需要所有bean都有默認構造函數?](http://stackoverflow.com/questions/7492652/does-spring-require-all-beans-to-have-a-default-constructor):[*] *無**](http://stackoverflow.com/a/7492713/545127) – Raedwald 2014-12-11 17:00:05

0

如果你還沒有激活Spring的基於註解的配置,你也可以得到這個錯誤。在Spring的XML包含此:

<方面:註解配置/ >

0

其他海報指出,如果你與豆的顯式實例混合自動裝配/組件掃描即可獲得問題。我有一個類似的問題與一個Web應用程序那樣做。我能夠通過告訴組件掃描程序不要自動實例化關鍵類的bean來解決問題。就像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns=...> 

    <aop:aspectj-autoproxy /> 

    <import resource="repository.xml" /> 
    ... 

    <context:component-scan base-package="com.example.webserver"> 
     <context:exclude-filter type="regex" expression="MyRepositoryImpl" /> 
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> 
    </context:component-scan> 
</beans> 

其中repository.xml包括bean的實例:

<beans xmlns=...> 
    <bean id="password" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName" value="java:/comp/env/store/clientPassword" /> 
    </bean> 
    <bean id="repository" class="com.example.webserver.datalayer.MyRepositoryImpl"> 
     <constructor-arg ref="password" /> 
    </bean> 
... 
</beans> 
相關問題