Spring 在使用JPARepository的Spring应用程序中需要的bean
问题描述
我在bean实例化方面遇到了问题。
在table.football.play.service.impl.PlayerServiceImpl
的构造函数的参数0中,需要一个类型为'table.football.play.repository.PlayerRepository'
的bean,但找不到该bean。
错误
这是我的类
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;
}
}
有一个实体类
package table.football.play.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="players")
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
}
最后这是我的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是一个接口,我怎么解决它呢?
有人能解释一下这个错误背后的理论吗?对我来说,仅仅知道它如何工作还不够,我想要理解原理。
解决方案
问题出现在你在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;
}
还有第二个问题在这里
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
因为你正在使用Spring Boot> 3.x,它使用的是Jakarta.persistence而不是javax,所以只需删除该依赖项并按照下一步操作即可。 修复方法如下:请将以下内容替换为:
<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-starter-data-jpa</artifactId>
<version>3.1.5</version>
</dependency>
在Entity
类中,将所有的javax.
导入替换为jakarta.
这将解决问题。