2012-02-15 20 views
12

私は春のコントローラとの問題がある - 私にはデフォルトコンストラクタが見つからない取得しています - しかし、彼らは私がapplicationContext.xmlを介して作成しようとしていますコンストラクタ持っている - HERESに関連するビット: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から2番目の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

xmlでBeanを定義するか、両方にアノテーションを付けてください(取得したようなエラーを避ける場合のみ)。

ここでの問題は、コンストラクタargsをautowiringしていないということです。そのため、春はコントローラで何をするのか分かりません。 Bean(@Controller注釈)を作成する必要があることはわかっていますが、how(デフォルトもautowiredなコンストラクタもありません)はわかりません。

あなたのような何かやろうとすることができる:XMLで

@Controller 
public class CacheHandler { 

private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

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

、その後:

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

だから、コンストラクタのパラメータをautowireれます。

デフォルトのコンストラクタを作成し、gxSessionIdCacheというプロパティを設定するだけです。

+0

我々は同じ考え;-)すべての答えを –

+0

グレート、感謝を共有:repository.xmlは、明示的な豆のインスタンス化を含め

<?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> 

! – Rory

+1

これを確認するには素晴らしいですが、springにはデフォルトコンストラクタか自動ワイヤコンストラクタが必要です。 – lwpro2

2

あなたは空のデフォルトコンストラクタを追加する必要があります。

@Controller 
    public class CacheHandler { 

    private final CustomGxSessionIdCacheImpl gxSessionIdCache; 

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

をしかし、あなたが、アノテーションベースの設定(@Controller)とXMLコンフィギュレーションを混合しているようですので、気をつけなります。上記の例では、アノテーションベースの設定を使用しています(XMLファイルからBean宣言を削除してください)。

+1

なぜデフォルトのコンストラクタですか?それは必須ではない? – kosa

+0

私は自分の投稿を編集しました。 –

+0

[春にはすべてのBeanにデフォルトのコンストラクタが必要ですか?](http://stackoverflow.com/questions/7492652/does-spring-require-all-beans-to-have-a-default-constructor):[* * no **](http://stackoverflow.com/a/7492713/545127) – Raedwald

0

Springの注釈ベースの設定を有効にしていない場合でもこのエラーが発生します。

<コンテキスト:あなたの春XMLでこれを含める注釈-config設定/ >

0

他のポスターは、あなたが豆の明示的なインスタンスとオートワイヤリング/コンポーネントスキャニングを混ぜる場合は、問題を得ることができることを指摘しています。私はそれをしたWebアプリケーションで同様の問題を抱えていました。私は、コンポーネントスキャナが重要なクラスの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> 
関連する問題