java 错误:“组织.Spring框架. bean.工厂.不满足的依赖关系异常:在Spring中创建名为...””的Bean时出错

xsuvu9jc  于 2023-02-14  发布在  Java
关注(0)|答案(1)|浏览(142)

我尝试使用Spring Boot 3.0.2和Spring cloud构建微服务。我尝试构建一个连接到Docker上运行的Postgres数据库的微服务。我收到以下错误:
未满足的依赖项异常:创建文件[/用户/.../客户/客户控制器. class]中定义的名为"customerController"的Bean时出错:通过构造函数参数0表示的未满足的依赖项:创建在文件[/Users/...customer/CustomerService.class]中定义了名称为"customerService"的Bean时出错:通过构造函数参数0表示的未满足的依赖项:创建在com. ioannispriovolos. customer中定义的名为"customerRepository"的Bean时出错。在JpaRepositoriesRegistrar上声明的@EnableJpaRepositories中定义的客户存储库。不是托管类型:类别com. ioannispriovolos.客户.客户
原因:java. lang.非法参数异常:不是托管类型:类别com. ioannispriovolos.客户.客户
我在堆栈溢出中搜索了很多类似的问题,但没有一个给我解决方案。我提供了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);
    }
}

如果你需要更多的代码从我这边请通知我,我会编辑的问题。我提供的客户类后,在评论中提到。

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;
}
rt4zxlrg

rt4zxlrg1#

Spring Boot JPA模块作为Spring Boot 3发行版的一部分,转而使用Jakarta Persistence API而不是javax.persistence.api,这就是@EntityScan找不到实体的原因。
阅读文档
Jakarta Dependency
将该依赖项添加到您的pom并更改实体中的导入
import javax.persistence.*;更改为import jakarta.persistence.*;

相关问题