Error: "org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name…" in Spring

huangapple go评论75阅读模式
英文:

Error: "org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name..." in Spring

问题

我尝试使用Spring Boot 3.0.2和Spring Cloud构建微服务。我试图构建一个微服务,该微服务连接到在Docker上运行的Postgres数据库。我遇到了以下错误:

> org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController' defined in file [/Users/.../customer/CustomerController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'customerService' defined in file [/Users/...customer/CustomerService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'customerRepository' defined in com.ioannispriovolos.customer.CustomerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.ioannispriovolos.customer.Customer

> Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.ioannispriovolos.customer.Customer

我在Stack Overflow上搜索了很多类似的问题,但没有一个给我解决方案。我提供了customerController

package com.ioannispriovolos.customer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/v1/customers")
public record CustomerController (CustomerService customerService){

    @PostMapping
    public void registerCustomer(@RequestBody CustomerRegistrationRequest customerRegistrationRequest) {
        log.info("new customer registration {}", customerRegistrationRequest);
        customerService.registerCustomer(customerRegistrationRequest);
    }
}

如果需要更多我的代码,请通知我,我将编辑问题。我提供了在评论中提到的Customer类。

package com.ioannispriovolos.customer;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Customer {
    @Id
    @SequenceGenerator(
            name = "customer_id_sequence",
            sequenceName = "customer_id_sequence"
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "customer_id_sequence"
    )
    private Integer id;
    private String firstName;
    private String lastname;
    private String email;
}

希望这些信息有所帮助。如果需要更多帮助,请随时告诉我。

英文:

I try to build microservices with Spring Boot 3.0.2 and Spring cloud. I try to build one Microservice that connects to a Postgres database running on docker. I get the following errors:

> org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController' defined in file [/Users/.../customer/CustomerController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'customerService' defined in file [/Users/...customer/CustomerService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'customerRepository' defined in com.ioannispriovolos.customer.CustomerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.ioannispriovolos.customer.Customer

> Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.ioannispriovolos.customer.Customer

I searched a lot for similar questions in stack overflow but none gave me the solution. I provide the customerController.

package com.ioannispriovolos.customer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/v1/customers")
public record CustomerController (CustomerService customerService){

    @PostMapping
    public void registerCustomer(@RequestBody CustomerRegistrationRequest customerRegistrationRequest) {
        log.info("new customer registration {}", customerRegistrationRequest);
        customerService.registerCustomer(customerRegistrationRequest);
    }
}

If you need more code from my side please inform me and I will edit the question.
I provide the Customer class after mentioned in the comments.

package com.ioannispriovolos.customer;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Customer {
    @Id
    @SequenceGenerator(
            name = "customer_id_sequence",
            sequenceName = "customer_id_sequence"
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "customer_id_sequence"
    )
    private Integer id;
    private String firstName;
    private String lastname;
    private String email;
}

答案1

得分: 2

Spring Boot JPA模块作为Spring Boot 3发布的一部分,已经开始使用Jakarta Persistence API而不是javax.persistence.api。这就是为什么@EntityScan找不到实体的原因。

阅读文档

Jakarta依赖

将该依赖添加到你的pom文件中,并将实体中的导入语句更改为:

import javax.persistence.*; 更改为 import jakarta.persistence.*;

英文:

Spring Boot JPA module as a part of the Spring Boot 3 release turned to work with Jakarta Persistence API rather than with javax.persistence.api. This is the reason why @EntityScan does not find the entities.

Read the docs

Jakarta Dependency

Add that dependency to your pom and change the imports in your entities

Change import javax.persistence.*; to import jakarta.persistence.*;

huangapple
  • 本文由 发表于 2023年2月14日 04:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定