siddheshghule
siddheshghule

Reputation: 25

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

I have already referred Why is bean not found during Spring Boot? and '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 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.

Upvotes: 1

Views: 862

Answers (1)

Aman
Aman

Reputation: 1744

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. Since, abstract classes can't be instantiated.

Upvotes: 1

Related Questions