我有一个接口名称

public interface ScoreDao {

   public int storeScore(OverallScore overallScore);

   public void storeIndividualScore(ScoreTO scoreTO);
}


实现类如下

@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冲突错误。


  造成原因:java.lang.RuntimeException:
  org.springframework.context.annotation.ConflictingBeanDefinitionException:
  Bean类[ScoreDao]的注释指定的Bean名称“ ScoreDao”
  与现有的同名不兼容的Bean定义冲突
  和类[ScoreDaoImpl]


当我将接口名称更改为IScoreDao时,它正在工作。是否因为服务名称@Repository(“ scoreDao”)与接口名称相同?

最佳答案

简单的答案是,是因为您有一个名为ScoreDao的接口,并且将其实现为@Repository("scoreDao")

两种解决方法:


ScoreDao重命名为其他名称
@Repository("scoreDao")更改为@Repository,以便它将使用默认名称

09-26 05:45