英文:
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
找不到实体的原因。
将该依赖添加到你的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.
Add that dependency to your pom and change the imports in your entities
Change import javax.persistence.*;
to import jakarta.persistence.*;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论