Unsatisfied dependency expressed through field dao、蛇に足を添えるな

3958 ワード

皆さん、蛇足を描かないでください.Springbootを使うとき.2つのデータベースで2つのテーブルをクエリーしてデータベースのパフォーマンスを向上させたい場合は、ぜひ見てください.
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'teacherGradeController': Unsatisfied dependency expressed through field 'service'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'teacherGradeServiceImpl': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.qf.dao.TeacherGradeDao' available: expected single matching bean but found 2: teacherGradeDaoImpl,teacherGradeDao

Description:

Field dao in com.qf.service.Impl.TeacherGradeServiceImpl required a single bean, but 2 were found:
	- teacherGradeDaoImpl: defined in file [E:\Protect\springboot\springbootInterceptor\target\classes\com\qf\dao\Impl\TeacherGradeDaoImpl.class]
	- teacherGradeDao: defined in file [E:\Protect\springboot\springbootInterceptor\target\classes\com\qf\dao\TeacherGradeDao.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

このエラーは、実はあなたの起動クラスにあります.一方で、どこに置いてあるかを見て、サブパッケージに入れたらスキャンしません.たとえば、(bean,service...)という親パッケージに置いておくと、彼はこれをスキャンします.もう1つは、起動クラスに@MapperScanを書き込むかどうかです.(dao層)これは、決して書かないでください.この注釈の意味はdao層パッケージのインタフェースをスキャンすることです.あなたがこの2つのデータベースを検索したとき、彼はdao層の実装クラスを持っています.daoインタフェースの注釈をスキャンしても見つからないので、再起動クラスに書いてある@MapperScanを削除することでOKです.
@Primaryこれはどれがメインライブラリであるかを示していますダブルデータベースであればdaoレイヤごとに@Mapperと書いたほうがいいです