Java 托管人; 私人最终客户映射客户映射; @凌驾 @交易的 公共页getAll(可分页可分页){ List dbCustomers=customerRepository.findAll(); List listCustomers=null; dbCustomers.forEach(dbCustomer->{ 添加(customerMapper.toRsDto(dbCustomer)); }); Page rsCustomers=新的PageImpl(列表客户); 回报客户; } }

Java 托管人; 私人最终客户映射客户映射; @凌驾 @交易的 公共页getAll(可分页可分页){ List dbCustomers=customerRepository.findAll(); List listCustomers=null; dbCustomers.forEach(dbCustomer->{ 添加(customerMapper.toRsDto(dbCustomer)); }); Page rsCustomers=新的PageImpl(列表客户); 回报客户; } },java,spring,spring-boot,Java,Spring,Spring Boot,依赖于客户服务的客户服务mpl似乎不正确。 从共享代码片段来看,似乎customerserviceinpl是CustomerService的唯一实现,因此它依赖于CustomerService类型的bean是不正确的。这似乎是它首先抱怨日志中存在循环依赖的原因 看来意图是, 依赖于CustomerRepository 从CustomerRepository CustomerServiceImpl应该更改为依赖于CustomerRepository我天真地认为,得益于Spring引导的魔力,get

依赖于
客户服务的
客户服务mpl
似乎不正确。 从共享代码片段来看,似乎
customerserviceinpl
CustomerService
的唯一实现,因此它依赖于
CustomerService
类型的bean是不正确的。这似乎是它首先抱怨日志中存在循环依赖的原因

看来意图是,

  • 依赖于
    CustomerRepository
  • CustomerRepository

  • CustomerServiceImpl
    应该更改为依赖于
    CustomerRepository

    我天真地认为,得益于Spring引导的魔力,getAll可以调用findAll。现在我返回的可能是正确的对象。但这没用。在此之前,我也只是尝试将null返回给getAll。对代码的更改标记在问题标题中。@HtmlMan,我建议查看代码。更新后的代码使用分配给
    null
    listCustomers
    。当尝试添加
    客户时,更新的代码现在应该抛出
    NullPointerException
    。代码还在
    CustomerRepository
    上执行
    findAll
    ,如果有大量客户,这可能会有问题,因为它将尝试获取内存中的所有内容。Spring提供了很好的文档,因此请参考Spring.io/learn和。
    public interface CustomerRepository extends JpaRepository<Customer, Long> {}
    
    public interface CustomerService {
        Page<CustomerRsDto> getAll(Pageable pageable);
    }
    
    @Service
    @RequiredArgsConstructor
    public class CustomerServiceImpl implements CustomerService {
        private final CustomerService customerService;
        
        @Override
        @Transactional
        public Page<CustomerRsDto> getAll(Pageable pageable) {
            Page<CustomerRsDto> rsCustomers = customerService.getAll(pageable);
            return rsCustomers;
        }
    }
    
    @RestController
    @RequiredArgsConstructor
    @RequestMapping("/customers")
    @Api(tags = "customers", value = "Customres")
    public class CustomersController {
        private CustomerService customerService;
    
        @GetMapping
        @ApiOperation(tags = "customers", value = "Get all")
        public Page<CustomerRsDto> getAll(Pageable pageable) {
            if (isNull(customerService)) {
                System.out.println("Not linked bean?");
            }
            return customerService.getAll(pageable);
        }
    }
    
    WARN 8928 --- [main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerServiceImpl' defined in file [<My Patch>\service\impl\CustomerServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'customerServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
    
    ERROR 8928 --- [main] o.s.b.d.LoggingFailureAnalysisReporter: 
    
    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    The dependencies of some of the beans in the application context form a cycle:
    
    ┌─────┐
    |  customerServiceImpl defined in file [<My Patch>\service\impl\CustomerServiceImpl.class]
    └─────┘
    
    Process finished with exit code 1
    
    @Service
    public class CustomerServiceImpl implements CustomerService {
        private final CustomerService customerService;
    
        public CustomerServiceImpl(@Lazy CustomerService customerService) {
            super();
            this.customerService = customerService;
        }
    
        @Override
        @Transactional
        public Page<CustomerRsDto> getAll(Pageable pageable) {
            Page<CustomerRsDto> rsCustomers = customerService.getAll(pageable);
            return rsCustomers;
        }
    }
    
    2020-11-11 17:59:29.826  INFO 1236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms
    Not linked bean?
    2020-11-11 17:59:30.308 ERROR 1236 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
    
    java.lang.NullPointerException: null
        at <My Package>.rest.CustomersController.getAll(CustomersController.java:31) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    
    @Service
    @RequiredArgsConstructor
    public class CustomerServiceImpl implements CustomerService {
        private final CustomerRepository customerRepository;
        private final CustomerMapper customerMapper;
    
        @Override
        @Transactional
        public Page<CustomerRsDto> getAll(Pageable pageable) {
            List<Customer> dbCustomers = customerRepository.findAll();
            List<CustomerRsDto> listCustomers = null;
    
            dbCustomers.forEach(dbCustomer -> {
                listCustomers.add(customerMapper.toRsDto(dbCustomer));
            });
    
            Page<CustomerRsDto> rsCustomers = new PageImpl<CustomerRsDto>(listCustomers);
    
            return rsCustomers;
        }
    }