Spring 服务名称与接口名称冲突
Spring Service name conflicting with Interface name
我有一个接口名称
public interface ScoreDao {
public int storeScore(OverallScore overallScore);
public void storeIndividualScore(ScoreTO scoreTO);
}
实现 class 如下所示
@Repository("scoreDao")
public class ScoreDaoImpl implements ScoreDao {
@Override
public int storeScore(OverallScore overallScore) {
//Implementation
}
@Override
public void storeIndividualScore(ScoreTO scoreTO){
//Implementation
}
}
来电者正在使用如下服务
@Service("scoreService")
public class scoreServiceImpl implements IScoreService {
@Autowired
private ScoreDao scoreDao;
@Override
public int storeScore(OverallScore overallScore) {
return scoreDao.storeOverallScore(overallScore);
}
@Override
public void storeIndividualScore(ScoreTO scoreTO) {
scoreDao.storeIndividualScore(scoreTO);
}
}
我正在使用 spring 4.x,在部署时出现 bean 冲突错误,如下所示。
Caused by: java.lang.RuntimeException:
org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'ScoreDao' for bean class [ScoreDao]
conflicts with existing, non-compatible bean definition of same name
and class [ScoreDaoImpl]
当我将接口名称更改为 IScoreDao 时,它可以正常工作。是因为服务名称 @Repository("scoreDao") 与接口名称相同吗?
简单的答案是是,这是因为您有一个名为ScoreDao
的接口,并且您将其实现为@Repository("scoreDao")
两种解决方法:
将 ScoreDao
重命名为其他名称
将 @Repository("scoreDao")
更改为 @Repository
以便它将使用默认名称
还有另一种解决方案在您的接口上提及@Repository,这样您就不需要在您的实现中提及class,因为您实现了该接口。所以它会使用接口名称自动创建 bean。
我有一个接口名称
public interface ScoreDao {
public int storeScore(OverallScore overallScore);
public void storeIndividualScore(ScoreTO scoreTO);
}
实现 class 如下所示
@Repository("scoreDao")
public class ScoreDaoImpl implements ScoreDao {
@Override
public int storeScore(OverallScore overallScore) {
//Implementation
}
@Override
public void storeIndividualScore(ScoreTO scoreTO){
//Implementation
}
}
来电者正在使用如下服务
@Service("scoreService")
public class scoreServiceImpl implements IScoreService {
@Autowired
private ScoreDao scoreDao;
@Override
public int storeScore(OverallScore overallScore) {
return scoreDao.storeOverallScore(overallScore);
}
@Override
public void storeIndividualScore(ScoreTO scoreTO) {
scoreDao.storeIndividualScore(scoreTO);
}
}
我正在使用 spring 4.x,在部署时出现 bean 冲突错误,如下所示。
Caused by: java.lang.RuntimeException: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'ScoreDao' for bean class [ScoreDao] conflicts with existing, non-compatible bean definition of same name and class [ScoreDaoImpl]
当我将接口名称更改为 IScoreDao 时,它可以正常工作。是因为服务名称 @Repository("scoreDao") 与接口名称相同吗?
简单的答案是是,这是因为您有一个名为ScoreDao
的接口,并且您将其实现为@Repository("scoreDao")
两种解决方法:
将
ScoreDao
重命名为其他名称将
@Repository("scoreDao")
更改为@Repository
以便它将使用默认名称
还有另一种解决方案在您的接口上提及@Repository,这样您就不需要在您的实现中提及class,因为您实现了该接口。所以它会使用接口名称自动创建 bean。