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

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

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

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个实体,几乎具有相同的逻辑但具有不同的功能。

英文:

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

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.

答案1

得分: 1

请检查这一行:

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

而应该是:

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

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

英文:

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.

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:

确定