使用 JPARepository 的 Spring 应用程序需要 bean

问题描述 投票:0回答:1

我在 bean 实例化方面遇到问题。

table.football.play.service.impl.PlayerServiceImpl
中构造函数的参数0需要一个
'table.football.play.repository.PlayerRepository'
类型的bean,但无法找到。

error

这是我的课

package table.football.play.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import table.football.play.entity.Player;

@Repository
public interface PlayerRepository extends JpaRepository<Player,Long> {
  
}

这是服务实现:

package table.football.play.service.impl;

import static table.football.play.util.ConvertEntityToDto.convertListPlayerEntityToListPlayerDto;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import table.football.play.dto.PlayerDTO;
import table.football.play.entity.Player;
import table.football.play.repository.PlayerRepository;
import table.football.play.service.interf.PlayerService;

@Service
public class PlayerServiceImpl implements PlayerService{
  
  private PlayerRepository playerRepository;
  
  @Autowired
  public PlayerServiceImpl(PlayerRepository playerRepository){
    this.playerRepository = playerRepository;
  }
  
  @Override
  public List<PlayerDTO> findAll(){
    List<Player> listPlayer = playerRepository.findAll();
    List<PlayerDTO> playerDTOList = convertListPlayerEntityToListPlayerDto(listPlayer);
    return playerDTOList;
  }


}

最后这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>table.football</groupId>
    <artifactId>play</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>game</name>
    <description>calcio balilla project</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>3.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${project.parent.version}</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我也在现场尝试@Autowired(这是一个更好的做法?或者在构造函数上发布@Autowired很好?或者是一样的?)

我读过一些人谈论创建默认构造函数...... 但是Repository是一个接口,我该如何解决它?

有人也可以向我解释这个错误背后的理论吗?对我来说仅仅有效还不够,我想了解。

非常感谢!

java spring spring-data-jpa spring-data pom.xml
1个回答
0
投票

问题出在您在 Service 类中使用的依赖注入。

代替:

private PlayerRepository playerRepository;
  
  @Autowired
  public PlayerServiceImpl(PlayerRepository playerRepository){
    this.playerRepository = playerRepository;
  }

编写字段注入

@Autowired
private PlayerRepository playerRepository;

或者构造函数注入:

private final PlayerRepository playerRepository;
  
  
  public PlayerServiceImpl(PlayerRepository playerRepository){
    this.playerRepository = playerRepository;
  }

它将解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.