Spring JPA 找不到存储库

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

Spring JPA 应用程序无法找到存储库。我尝试用@repository对其进行注释,将控制器、服务和存储库放入组件扫描等中,但我没有任何运气。有人发现我遗漏的那条线吗?我添加了一张图片来显示包的布局。

编译时提供的信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in member.services.MemberServiceImpl required a bean of type 'member.repositories.MemberRepository' that could not be found.


Action:

Consider defining a bean of type 'member.repositories.MemberRepository' in your configuration.

课程:

@SpringBootApplication
public class MemberApplication {

    public static void main(String[] args) {
        SpringApplication.run(MemberApplication.class, args);
    }
}
@RestController
@Slf4j
public class MemberController {
    private MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }
    
    @GetMapping("/retrieve")
    public String retrieveAllMembers() {
        memberService.retrieveAllMembers();

        return "Success";
    }
}
@Slf4j
@Service
public class MemberServiceImpl implements MemberService {
    MemberRepository memberRepository;

    @Autowired
    public MemberServiceImpl(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }

    public void addMemberToDatabase(Member member) {
        memberRepository.save(member);
    }

    public void retrieveAllMembers() {
        memberRepository.findAll();
    }
}
public interface MemberService {
    void retrieveAllMembers();
    void addMemberToDatabase(Member member);
}

/**
 * MemberRepository.
 */
public interface MemberRepository extends JpaRepository<Member, Long> {
}
@Entity
public class Member {
    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String LastName;

    public Member() {
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }
}

<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 
http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>backend</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Archetype - backend</name>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.1</version>
  </parent>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <profiles>
    <profile>
      <id>spring-data-next-releasetrain</id>
      <properties>
        <spring-data-bom.version>2024.0.0-SNAPSHOT</spring-data- 
  bom.version>
  </properties>
</profile>

<profile>
   <id>spring-data-next</id>
  <properties>
    <spring-data-bom.version>2023.1.2-SNAPSHOT</spring-data-bom.version>
  </properties>
</profile>
  </profiles>
  <dependencies>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.angus</groupId>
      <artifactId>angus-mail</artifactId>
      <version>2.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.opencsv</groupId>
      <artifactId>opencsv</artifactId>
      <version>5.3</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.30</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.orm</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>7.0.0.Alpha1</version>
    </dependency>
  </dependencies>
</project>
spring jpa repository javabeans autowired
1个回答
0
投票

最后我使用了@EnableJpaRepositories,但让它覆盖了所有比jpa更重要的存储库包:

@EnableJpaRepositories (basePackages = "member.repositories")

我将 @repository 从存储库中取出,因为不再需要它。实体更改为:

@Data
@Entity
@Table (name="member")
public class Member {

@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column (name="person_id")
private Long personId;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String LastName;

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<url>http://maven.apache.org</url>
<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>
<profiles>
    <profile>
        <id>spring-data-next-releasetrain</id>
        <properties>
            <spring-data-bom.version>2024.0.0-SNAPSHOT</spring-data-bom.version>
        </properties>
    </profile>

    <profile>
        <id>spring-data-next</id>
        <properties>
            <spring-data-bom.version>2023.1.2-SNAPSHOT</spring-data-bom.version>
        </properties>
    </profile>
</profiles>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

我认为这些版本可能会被整理并放入更好的版本,但目前这一切似乎都有效!

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