【问题标题】:"No qualifying bean of type" while using autowired in JPA Repository在 JPA 存储库中使用自动装配时 \"No qualifying bean of type\"
【发布时间】:2022-10-14 00:25:17
【问题描述】:

我正在尝试使用 Spring Boot 创建 REST 服务,但在服务和存储库之间使用 @Autowired 时遇到问题。 这是我的代码:

实体

package com.model;
import javax.persistence.*;

@Entity
@Table(name=Constants.USERS, schema = Constants.SCHEMA)
public class Users {


    @Column(name= "username", nullable=false)
    private String username;

    @Column(name="lastname", nullable = false)
    private String lastname;

    public Users() {
    }

    public Users(String username, String lastname) {
        this.username = username;
        this.lastname = lastname;
    }



    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

存储库

package com.repository;
import com.model.Users;

import java.util.List;

@Repository
public interface UsersRepository extends JPAConnector<Users,Integer> {

    public List<Users> findAll();
    public long count();
}

服务

package com.service;

import com.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class UsersService{

    @Autowired
    private final UsersRepository usersRepository;

    public UsersService(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }


    public long count() {
        long conteggio = usersRepository.count();
        return conteggio;
    }
}

这是回溯

Error creating bean with name 'usersService' .Unsatisfied dependency expressed through 
constructor paramet
er 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.repository.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}.
No qualifying bean of type 'com.intesasanpaolo.dsi.core.bear.ptrm0.connector.jpa.UsersRepository' available:

预计至少有 1 个 bean 有资格作为 autowire 候选者。依赖注释:{}

【问题讨论】:

  • @Autowired 从属性移动到UsersService 中的ctor。

标签: java spring spring-boot jpa spring-data-jpa


【解决方案1】:
    public UsersService(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

尝试删除此构造函数

【讨论】:

    【解决方案2】:

    您的用户表中似乎没有任何主键。

    JPA 要求每个实体都有一个 ID。所以不,不允许没有 ID 的实体。尝试添加 Integer 类型的主键,因为它在扩展到 JPa 存储库时是必需的,因为传递的第二个参数是主键的类型。您可以使用@Id 注释将字段定义为主键。您可以执行以下操作:

    包 com.model; 导入 javax.persistence.*;

    @Entity
    @Table(name=Constants.USERS, schema = Constants.SCHEMA)
    public class Users {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY) 
        private int id;   //<----like this
    @Column(name= "username", nullable=false)
    private String username;
    
    @Column(name="lastname", nullable = false)
    private String lastname;
    
    public Users() {
    }
    
    public Users(String username, String lastname) {
        this.username = username;
        this.lastname = lastname;
    }
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getLastname() {
        return lastname;
    }
    
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    

    }

    保持您的存储库相同

    此外,您不必在字段上编写@Autowire,因为构造函数会自动将 bean 注入到字段中。

    @Autowired   // this is not necessary you can remove this too
        private final UsersRepository usersRepository;
    
        public UsersService(UsersRepository usersRepository) {
            this.usersRepository = usersRepository;
        }
    

    【讨论】:

      【解决方案3】:

      删除这个:

       public Users() {
          }
      

      你已经定义了两次

      这个

      public UsersService(UsersRepository usersRepository) {
          this.usersRepository = usersRepository;
      }
      

       @Autowired
       private final UsersRepository usersRepository;
      
      public UsersService(UsersRepository usersRepository) {
              this.usersRepository = usersRepository;
          }
      

      因为您使用构造函数注入或字段注入

      【讨论】:

        猜你喜欢
        • 2022-12-27
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 2022-12-12
        • 2016-11-07
        • 1970-01-01
        • 1970-01-01
        • 2022-12-02
        相关资源
        最近更新 更多