【问题标题】:Required a bean of type that could not be found REST API需要一个找不到 REST API 类型的 bean
【发布时间】:2020-10-23 19:33:43
【问题描述】:

我已经推荐了Why is bean not found during Spring Boot?'Field required a bean of type that could not be found.' error spring restful API using mongodb

package com.digitalhotelmanagement.digitalhotelmanagement;

@SpringBootApplication
/*
* (scanBasePackages={ "com.digitalhotelmanagement.digitalhotelmanagement",
* "com.digitalhotelmanagement.digitalhotelmanagement.service"})
*/

public class DigitalHotelManagementApplication {

   public static void main(String[] args) {
       SpringApplication.run(DigitalHotelManagementApplication.class, args);
   }

package com.digitalhotelmanagement.digitalhotelmanagement.controller;

@RestController
@RequestMapping("customer")
public class CustomerController {

   private static final Logger logger = Logger.getLogger("ClientController");
   @Autowired
   CustomerService customerService;

   /*
    * getCustomer getCustomerByEmail createCustomer updateCustomer deleteCustomer
    */
   @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
   public List<CustomerResponseModel> getCustomer(@RequestParam(value = "page", defaultValue = "1") int page,
           @RequestParam(value = "limit", defaultValue = "10") int limit) throws Exception {
       logger.info("Get All Admin List. Pagination parameters: page: " + page + " pageLimit: " + limit);
       List<CustomerResponseModel> returnValue = new ArrayList<>();
       List<CustomerDTO> customers = customerService.getCustomers(page, limit);
       .
       .
       .
       return returnValue;
   }
package com.digitalhotelmanagement.digitalhotelmanagement.service;

public interface CustomerService {
    CustomerDTO createCustomer(CustomerDTO customer) throws Exception;

    List<CustomerDTO> getCustomers(int page, int limit) throws Exception;
}
package com.digitalhotelmanagement.digitalhotelmanagement.implementation;

@Service
public abstract class CustomerServiceImplementation implements CustomerService {

    @Autowired
    CustomerRepository customerRepository;

    @Override
    public List<CustomerDTO> getCustomers(int page, int limit) throws Exception {

        List<CustomerDTO> returnValue = new ArrayList<>();
        Pageable pageableRequest = PageRequest.of(page, limit);
        Page<CustomerEntity> customerPage = customerRepository.findAll(pageableRequest);

        List<CustomerEntity> customerEntities = customerPage.getContent();

        if (customerEntities.isEmpty())
            throw new Exception(ErrorMessages.PAGE_ERROR.getErrorMessage());
        .
        .
        .
        return returnValue;
    }
package com.digitalhotelmanagement.digitalhotelmanagement.repository;

@Repository
public interface CustomerRepository extends PagingAndSortingRepository<CustomerEntity, Integer> {

    CustomerEntity findByEmail(String email);
    Optional<CustomerEntity> findById(Integer id);
}
2020-10-23 22:19:30.710  WARN 25688 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController': 
Unsatisfied dependency expressed through field 'customerServiceImplementation'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


Description:

Field customerService in com.digitalhotelmanagement.digitalhotelmanagement.controller.CustomerController required a bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' in your configuration.

CustomerService 只是一个接口,充当 CustomerController 和 CustomerServiceImplementation 之间的中间人,它实际上是在 CustomerRepository 的帮助下对数据库执行操作。

我正在尝试从 MYSQL 数据库中检索数据,我总共有 7 个实体,几乎具有不同功能的相同逻辑。

【问题讨论】:

  • 嗨。我认为这需要在描述您的场景以及您实际想要实现的目标方面进行大量改进。在您的问题中添加更多介绍(自写)。给出一些上下文并描述你的具体问题。将代码 sn-ps 减少到相关的代码。写一些信息,代码应该做什么以及这个上下文中的问题在哪里。
  • 我已经编辑了帖子,我上传了所有必要的类来全面了解操作,希望它可以帮助你更好地了解

标签: java spring mongodb spring-boot


【解决方案1】:

检查这一行:

@Service
public abstract class CustomerServiceImplementation implements CustomerService {...}

应该是这样的:

@Service
public class CustomerServiceImplementation implements CustomerService {...}

您必须在具体类中implement CustomerService,而不是抽象。因为,抽象类不能被实例化。

【讨论】:

  • 我什至不需要看我的程序,并意识到这是一个错误。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-02-05
  • 2017-09-17
  • 2020-07-06
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
相关资源
最近更新 更多