[英]Required a bean of type that could not be found REST API
I have already referred Why is bean not found during Spring Boot?我已经提到了为什么在 Spring Boot 期间找不到 bean? and 'Field required a bean of type that could not be found.'和“字段需要一个无法找到的类型的 bean。” error spring restful API using mongodb 使用mongodb的错误spring restful API
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 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. CustomerService 只是一个接口,充当 CustomerController 和 CustomerServiceImplementation 之间的中间人,实际上是在 CustomerRepository 的帮助下对数据库进行操作。
I am trying to retrieve data from MYSQL database, i have total of 7 entities, almost having same logics with different functionalities.我正在尝试从 MYSQL 数据库中检索数据,我总共有 7 个实体,几乎具有相同的逻辑和不同的功能。
Check this line:检查这一行:
@Service
public abstract class CustomerServiceImplementation implements CustomerService {...}
while it should be:虽然它应该是:
@Service
public class CustomerServiceImplementation implements CustomerService {...}
You have to implement CustomerService
in a concrete class, not an abstract .您必须在具体类中implement CustomerService
,而不是抽象类。 Since, abstract classes can't be instantiated.因为,抽象类不能被实例化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.