2012-03-02 4 views
0

私はspring autowire機能に問題があります。これは同じタイプの2つのBeanを見つけましたが、実際にはこのBeanが実装している1つのBeanとインタフェースしかありません。私が手にこの設定でspring autowire 2 beansが例外を発見しました

package xxx.vs.dao.abs.yyy;  
public interface IntermedDao extends GenericDao<Intermed> { 
    // methods here 
} 

package xxx.vs.dao.yyy;  
import org.springframework.stereotype.Repository; 
@Repository 
public class IntermedDaoImpl extends GenericInboundDaoImpl<Intermed> implements IntermedDao { 
    // methods here 
} 

package xxx.vs.services.yyy; 
@Service 
@Transactional 
public class IntermedServiceImpl implements IntermedService { 

    @Autowired 
    IntermedDao dao; 

    public IntermedDao getDao() { 
     return dao; 
    } 

    public void setDao(IntermedDao dao) { 
     this.dao = dao; 
    }  
} 

<context:component-scan base-package="xxx.vs.services"/> 
<context:component-scan base-package="xxx.vs.dao"/> 
<context:annotation-config/> 

<bean id="intermedDao" class="xxx.vs.dao.yyy.IntermedDaoImpl" /> 

と:applicationContext.xmlをで

私はこれらの行を持っている

java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'intermedServiceImpl': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: xxx.vs.dao.abs.yyy.IntermedDao xxx.vs.services.yyy.IntermedServiceImpl.dao; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException:No unique bean of type [xxx.vs.dao.abs.yyy.AgentDao] is defined: expected single matching bean but found 2: [intermedDaoImpl, intermedDao] 

私のDAOのクラスが実装しているインターフェイスを含むパッケージをスキャンするので、これは起こりますか?

答えて

4

明示的にBeanを宣言した両方のため、これが起こっている:

<bean id="intermedDao" class="xxx.vs.dao.yyy.IntermedDaoImpl" /> 

だけでなく、コンポーネントスキャンによってピックアップされる@Repositoryを宣言:

@Repository 
public class IntermedDaoImpl 

あなたがいずれかを実行する必要がありますどちらか一方ではなく、他方である。 <bean>を削除することをおすすめします。

なお、エラーメッセージ:

expected single matching bean but found 2: [intermedDaoImpl, intermedDao]

が競合豆を言及しています。最初のものは@Repositoryです。ここで、Beanの名前はクラス名から自動生成されます。 2番目は<bean>です。

5

dao beanのインスタンスを2つ作成します.1つはXMLに、もう一度は@Repository注釈を使用して作成します。

エラーをよく見る:

expected single matching bean but found 2: [intermedDaoImpl, intermedDao] 

いずれかが唯一のBeanインスタンスを作成する(XMLまたは注釈ごとに)、またはそれが使用するかを指定するために配線するとき@Qualifierを使用します。

+0

確かに、私はxml beanを削除した後はすべてOKでした。ありがとう。 –

関連する問題