No unique bean of type
2747 ワード
Webx開発で何度かこのようなエラーに遭遇したことがあるので、まとめてみましょう.
アプリケーションContext.xmlにタイマタスクの構成を追加します.
これ
bean
既存の
Service
に表示されます.
上記のように、注記@Serviceを使用すると、springはこのクラスを自動的にロードします.コンパイルの結果、次のエラーが発生しました.
2015-06-08 17:22:03.777:WARN::Failed startup of context runjettyrun.HSFJettyWebAppContext@19c2921d{/,src/main/webapp}
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'module.screen.MachineApply': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [com.taobao.scm.tenv.service.interfaces.UnitService] is defined:
expected single matching bean but found 2: [unitServiceImpl, unitService]
エラー情報に基づいてmoduleにナビゲートする.screen.MachineApplyというファイルでは、次の定義が見つかりました.
当
spring
初期化
bean
初期化する
bean
同じ名前の
bean,applicationContext.xmlの名前はunitServiceで、MachineApplyです.JAvaの中はunitserviceなので、このbeanは2回ロードされ、衝突をもたらします.解決策は次のとおりです.
1、MachineApply.JAvaにはunitserviceを仕様型に変更したunitServiceが入っています.(推奨)
2,アプリケーションContext.xmlのunitServiceをunitserviceに変更しました.(推奨しないでjavaファイルの他の場所でもこれが役に立つ場合は、統一的に変更するので、規範的なネーミングが重要です)
今回命名された不規範は大きな穴を踏んだと同時に、このbeanのロード過程を少し理解した.ネット上の分析と結びつけて、以下のようにまとめます.
1.@Serviceはbeanを宣言するときに名前を指定しないと、クラス名のアルファベット小文字でデフォルトで命名されます.
2.springがbeanを初期化すると、初期化するbeanに同じ名前のbeanが既に存在するかどうかを監視します.
3.spring初期化beanの順序はxmlで構成した順序に依存します.
4.spring beanを初期化するときは名前の一貫性のみを維持します.
別のファイルの分析を添付します.http://mixer-b.iteye.com/blog/1563851
アプリケーションContext.xmlにタイマタスクの構成を追加します.
<bean name="applicationService" class="com.taobao.scm.tenv.monitor.service.impl.ApplicationServiceImpl" />
<bean name="productService" class="com.taobao.scm.tenv.monitor.service.impl.ProductServiceImpl" />
<bean name="buService" class="com.taobao.scm.tenv.monitor.service.impl.BuServiceImpl" />
<bean name="unitService" class="com.taobao.scm.tenv.service.impl.UnitServiceImpl" />
の最後の行には、unitServiceのbeanが追加されています.これ
bean
既存の
Service
に表示されます.
@Service
public class UnitServiceImpl implements UnitService {
@Resource
UnitDAO unitDao;
public List<UnitDO> findUnitByCondition(UnitDO unitDO){
return unitDao.findListByExample(unitDO);
}
}
上記のように、注記@Serviceを使用すると、springはこのクラスを自動的にロードします.コンパイルの結果、次のエラーが発生しました.
2015-06-08 17:22:03.777:WARN::Failed startup of context runjettyrun.HSFJettyWebAppContext@19c2921d{/,src/main/webapp}
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'module.screen.MachineApply': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [com.taobao.scm.tenv.service.interfaces.UnitService] is defined:
expected single matching bean but found 2: [unitServiceImpl, unitService]
エラー情報に基づいてmoduleにナビゲートする.screen.MachineApplyというファイルでは、次の定義が見つかりました.
public class MachineApply {
@Resource
UserService userService;
@Resource
MachineService machineService;
@Resource
MirrorService mirrorService;
@Resource
SpecificationService specificationService;
@Resource
UnitService unitservice;
最後の行、@ResourceはUnitServer unitserviceを示します.問題はここのネーミングです当
spring
初期化
bean
初期化する
bean
同じ名前の
bean,applicationContext.xmlの名前はunitServiceで、MachineApplyです.JAvaの中はunitserviceなので、このbeanは2回ロードされ、衝突をもたらします.解決策は次のとおりです.
1、MachineApply.JAvaにはunitserviceを仕様型に変更したunitServiceが入っています.(推奨)
2,アプリケーションContext.xmlのunitServiceをunitserviceに変更しました.(推奨しないでjavaファイルの他の場所でもこれが役に立つ場合は、統一的に変更するので、規範的なネーミングが重要です)
今回命名された不規範は大きな穴を踏んだと同時に、このbeanのロード過程を少し理解した.ネット上の分析と結びつけて、以下のようにまとめます.
1.@Serviceはbeanを宣言するときに名前を指定しないと、クラス名のアルファベット小文字でデフォルトで命名されます.
2.springがbeanを初期化すると、初期化するbeanに同じ名前のbeanが既に存在するかどうかを監視します.
3.spring初期化beanの順序はxmlで構成した順序に依存します.
4.spring beanを初期化するときは名前の一貫性のみを維持します.
別のファイルの分析を添付します.http://mixer-b.iteye.com/blog/1563851