需要找到一种无法找到的类型的 bean REST API。

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

Required a bean of type that could not be found REST API

问题

我已经参考了https://stackoverflow.com/questions/55064074/why-is-bean-not-found-during-spring-boot和https://stackoverflow.com/questions/42907553/field-required-a-bean-of-type-that-could-not-be-found-error-spring-restful-ap?noredirect=1&lq=1

  1. package com.digitalhotelmanagement.digitalhotelmanagement;
  2. @SpringBootApplication
  3. /*
  4. * (scanBasePackages={ "com.digitalhotelmanagement.digitalhotelmanagement",
  5. * "com.digitalhotelmanagement.digitalhotelmanagement.service"})
  6. */
  7. public class DigitalHotelManagementApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DigitalHotelManagementApplication.class, args);
  10. }
  11. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.controller;
  2. @RestController
  3. @RequestMapping("customer")
  4. public class CustomerController {
  5. private static final Logger logger = Logger.getLogger("ClientController");
  6. @Autowired
  7. CustomerService customerService;
  8. /*
  9. * getCustomer getCustomerByEmail createCustomer updateCustomer deleteCustomer
  10. */
  11. @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
  12. public List<CustomerResponseModel> getCustomer(@RequestParam(value = "page", defaultValue = "1") int page,
  13. @RequestParam(value = "limit", defaultValue = "10") int limit) throws Exception {
  14. logger.info("Get All Admin List. Pagination parameters: page: " + page + " pageLimit: " + limit);
  15. List<CustomerResponseModel> returnValue = new ArrayList<>();
  16. List<CustomerDTO> customers = customerService.getCustomers(page, limit);
  17. .
  18. .
  19. .
  20. return returnValue;
  21. }
  22. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.service;
  2. public interface CustomerService {
  3. CustomerDTO createCustomer(CustomerDTO customer) throws Exception;
  4. List<CustomerDTO> getCustomers(int page, int limit) throws Exception;
  5. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.implementation;
  2. @Service
  3. public abstract class CustomerServiceImplementation implements CustomerService {
  4. @Autowired
  5. CustomerRepository customerRepository;
  6. @Override
  7. public List<CustomerDTO> getCustomers(int page, int limit) throws Exception {
  8. List<CustomerDTO> returnValue = new ArrayList<>();
  9. Pageable pageableRequest = PageRequest.of(page, limit);
  10. Page<CustomerEntity> customerPage = customerRepository.findAll(pageableRequest);
  11. List<CustomerEntity> customerEntities = customerPage.getContent();
  12. if (customerEntities.isEmpty())
  13. throw new Exception(ErrorMessages.PAGE_ERROR.getErrorMessage());
  14. .
  15. .
  16. .
  17. return returnValue;
  18. }
  19. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.repository;
  2. @Repository
  3. public interface CustomerRepository extends PagingAndSortingRepository<CustomerEntity, Integer> {
  4. CustomerEntity findByEmail(String email);
  5. Optional<CustomerEntity> findById(Integer id);
  6. }
  1. 2020-10-23 22:19:30.710 WARN 25688 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
  2. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController':
  3. Unsatisfied dependency expressed through field 'customerServiceImplementation'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  4. 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)}
  5. Description:
  6. Field customerService in com.digitalhotelmanagement.digitalhotelmanagement.controller.CustomerController required a bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' that could not be found.
  7. The injection point has the following annotations:
  8. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  9. Action:
  10. Consider defining a bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' in your configuration.

CustomerService只是一个接口,充当CustomerController和CustomerServiceImplementation之间的中介,实际上是通过CustomerRepository执行与数据库的操作。

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

英文:

I have already referred https://stackoverflow.com/questions/55064074/why-is-bean-not-found-during-spring-boot and https://stackoverflow.com/questions/42907553/field-required-a-bean-of-type-that-could-not-be-found-error-spring-restful-ap?noredirect=1&lq=1

  1. package com.digitalhotelmanagement.digitalhotelmanagement;
  2. @SpringBootApplication
  3. /*
  4. * (scanBasePackages={ "com.digitalhotelmanagement.digitalhotelmanagement",
  5. * "com.digitalhotelmanagement.digitalhotelmanagement.service"})
  6. */
  7. public class DigitalHotelManagementApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DigitalHotelManagementApplication.class, args);
  10. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.controller;
  2. @RestController
  3. @RequestMapping("customer")
  4. public class CustomerController {
  5. private static final Logger logger = Logger.getLogger("ClientController");
  6. @Autowired
  7. CustomerService customerService;
  8. /*
  9. * getCustomer getCustomerByEmail createCustomer updateCustomer deleteCustomer
  10. */
  11. @GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
  12. public List<CustomerResponseModel> getCustomer(@RequestParam(value = "page", defaultValue = "1") int page,
  13. @RequestParam(value = "limit", defaultValue = "10") int limit) throws Exception {
  14. logger.info("Get All Admin List. Pagination parameters: page: " + page + " pageLimit: " + limit);
  15. List<CustomerResponseModel> returnValue = new ArrayList<>();
  16. List<CustomerDTO> customers = customerService.getCustomers(page, limit);
  17. .
  18. .
  19. .
  20. return returnValue;
  21. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.service;
  2. public interface CustomerService {
  3. CustomerDTO createCustomer(CustomerDTO customer) throws Exception;
  4. List<CustomerDTO> getCustomers(int page, int limit) throws Exception;
  5. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.implementation;
  2. @Service
  3. public abstract class CustomerServiceImplementation implements CustomerService {
  4. @Autowired
  5. CustomerRepository customerRepository;
  6. @Override
  7. public List<CustomerDTO> getCustomers(int page, int limit) throws Exception {
  8. List<CustomerDTO> returnValue = new ArrayList<>();
  9. Pageable pageableRequest = PageRequest.of(page, limit);
  10. Page<CustomerEntity> customerPage = customerRepository.findAll(pageableRequest);
  11. List<CustomerEntity> customerEntities = customerPage.getContent();
  12. if (customerEntities.isEmpty())
  13. throw new Exception(ErrorMessages.PAGE_ERROR.getErrorMessage());
  14. .
  15. .
  16. .
  17. return returnValue;
  18. }
  1. package com.digitalhotelmanagement.digitalhotelmanagement.repository;
  2. @Repository
  3. public interface CustomerRepository extends PagingAndSortingRepository<CustomerEntity, Integer> {
  4. CustomerEntity findByEmail(String email);
  5. Optional<CustomerEntity> findById(Integer id);
  6. }
  1. 2020-10-23 22:19:30.710 WARN 25688 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
  2. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController':
  3. Unsatisfied dependency expressed through field 'customerServiceImplementation'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  4. 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)}
  5. Description:
  6. Field customerService in com.digitalhotelmanagement.digitalhotelmanagement.controller.CustomerController required a bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' that could not be found.
  7. The injection point has the following annotations:
  8. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  9. Action:
  10. Consider defining a bean of type 'com.digitalhotelmanagement.digitalhotelmanagement.service.CustomerService' in your configuration.

CustomerService is just an interface which acts as a middleman between CustomerController and CustomerServiceImplementation, which actually performs operations with the database with the help of CustomerRepository.

I am trying to retrieve data from MYSQL database, i have total of 7 entities, almost having same logics with different functionalities.

答案1

得分: 1

请检查这一行:

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

而应该是:

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

你必须在一个具体的类中实现CustomerService,而不是在一个抽象类中。因为抽象类无法被实例化。

英文:

Check this line:

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

while it should be:

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

You have to implement CustomerService in a concrete class, not an abstract. Since, abstract classes can't be instantiated.

huangapple
  • 本文由 发表于 2020年10月24日 03:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64506263.html
匿名

发表评论

匿名网友

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

确定