java - Spring 服务名称与接口(interface)名称冲突

标签 java spring spring-annotations

我有一个接口(interface)名称

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

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]

当我将接口(interface)名称更改为 IScoreDao 时,它可以正常工作。是因为服务名@Repository("scoreDao") 和接口(interface)名一样吗?

最佳答案

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

两种解决方法:

  1. ScoreDao 重命名为其他名称

  2. @Repository("scoreDao") 更改为 @Repository 以便它将使用默认名称

关于java - Spring 服务名称与接口(interface)名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678002/

相关文章:

java - Spring GET 和 POST 映射在不同的类中

java - 使用 Spring bean 的惰性对象构建器

java - 使用 Spring Integration 命令总线启动/停止通过注释定义的 AbstractEndpoints

spring - 如何使用 Kotlin 在 Spring Boot 中处理自定义注解?

java - 修改 Spring @RolesAllowed 的行为

java - WAR部署失败

java - 更改上下文根后,前端角度项目无法与 Spring boot 项目一起使用

Java - 将多个字符串转换为图像格式

java - jni 调用之间在 native 代码中分配的内存

java - 这是在多模块项目(即 Spring)中复制 Maven 依赖项吗?