You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot启动报错:NotificationService依赖的NotificationDAO Bean未找到

问题排查:Spring无法找到NotificationDAO Bean

我有如下项目结构:
[图片链接]

启动应用时出现错误:

notifications.service.NotificationService的构造器第0个参数需要一个类型为'notifications.dao.NotificationDAO'的Bean,但未找到。

试过多种Lombok构造器注解组合都无效,相关代码如下:

服务类

package notifications.service;

import lombok.RequiredArgsConstructor;
import notifications.dao.NotificationDAO;
import notifications.model.NotificationEntity;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NotificationService {


  private final NotificationDAO notificationDAO;

  public List<NotificationEntity> findByUserId(Long userId) {
    return notificationDAO.findAllByUserId(userId,Sort.by(Sort.Direction.ASC, "id"));
 }

  public List<NotificationEntity> findAllByUserIdAndApplicationId(Long userId, Long applicationId) {
    return notificationDAO.findAllByUserIdAndApplicationId(userId, applicationId, Sort.by(Sort.Direction.ASC, "id"));
 }

  public List<NotificationEntity> findAllByUserIdAndDocumentId(Long userId, Long documentId) {
    return notificationDAO.findAllByUserIdAndDocumentId(userId, documentId, Sort.by(Sort.Direction.ASC, "id"));
 }
}

DAO类

package notifications.dao;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import notifications.model.NotificationEntity;

import java.util.List;


@Repository
public interface NotificationDAO extends JpaRepository<NotificationEntity, Long> {

    List<NotificationEntity> findAllByUserId(Long userId, Sort sort);

    List<NotificationEntity> findAllByUserIdAndApplicationId(Long userId, Long applicationId, Sort sort);

    List<NotificationEntity> findAllByUserIdAndDocumentId(Long userId, Long documentId, Sort sort);

}

主类

package notifications;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

@SpringBootApplication
@EnableR2dbcRepositories
public class NotificationsApplication {

    public static void main(String[] args) {
        SpringApplication.run(NotificationsApplication.class, args);
    }
}

问题根源

你的DAO继承的是JpaRepository(JPA规范的Repository),但主类上却添加了@EnableR2dbcRepositories注解——这是用来启用**R2DBC(响应式关系数据库连接)**类型Repository的,两者属于完全不同的技术栈,无法兼容。

Spring会根据@EnableR2dbcRepositories的配置去扫描并创建R2DBC类型的Repository Bean,但你的DAO是JPA实现的,所以Spring不会将其注册为容器中的Bean,最终导致Service依赖注入时找不到对应的实例。

解决办法

根据你的实际技术栈二选一调整:

  1. 如果使用JPA(如Hibernate):

    • 移除主类上的@EnableR2dbcRepositories注解,替换为@EnableJpaRepositories(若DAO不在主类所在包或子包,需通过basePackages参数指定DAO所在包路径;通常@SpringBootApplication会自动扫描当前包下的JPA Repository,可根据情况选择是否添加该注解)。
  2. 如果使用R2DBC:

    • 修改DAO类,将继承的JpaRepository<NotificationEntity, Long>替换为R2dbcRepository<NotificationEntity, Long>,同时调整查询方法的签名(R2DBC的Repository方法规则与JPA存在差异,比如Sort的使用方式)。

另外需同步确认NotificationEntity的注解:使用JPA则添加@Entity、@Id等JPA注解;使用R2DBC则添加@Table、@Id等R2DBC相关注解。

内容的提问来源于stack exchange,提问作者mtmx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 20:22:06