Consider defining a bean of type 'com.fsse2207.project_backend.service.ProductService' in your configuration

Viewed 31

Basically, I have created all of the pojo and layers(including the repository layer) necessary for Spring Boot to automatically implement MySql commands. When I trying to run the programme, I get the following command: Description:

Parameter 0 of constructor in com.fsse2207.project_backend.api.ProductApi required a bean of type 'com.fsse2207.project_backend.service.ProductService' that could not be found.

Action:

Consider defining a bean of type 'com.fsse2207.project_backend.service.ProductService' in your configuration.

It turns out there's sth wrong about the bean in my ProductApi. It says " Could not autowire. No beans of 'ProductService' type found." How do I fix it?

The following is the interface under the service layer:

package com.fsse2207.project_backend.service;

import com.fsse2207.project_backend.data.ProductCreateData;
import com.fsse2207.project_backend.data.ProductDetailData;
import com.fsse2207.project_backend.exception.ProductFoundByIdException;
import org.springframework.stereotype.Service;

public interface ProductService {
    ProductDetailData createProductData (ProductCreateData productCreateData) throws ProductFoundByIdException;
}

The following is the service class:

package com.fsse2207.project_backend.service.impl;

import com.fsse2207.project_backend.data.ProductCreateData;
import com.fsse2207.project_backend.data.ProductDetailData;
import com.fsse2207.project_backend.data.entity.ProductEntity;
import com.fsse2207.project_backend.exception.ProductFoundByIdException;
import com.fsse2207.project_backend.repository.ProductRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl {
    private ProductRepository productRepository;
    @Autowired
    public ProductServiceImpl(ProductRepository productRepository){
        this.productRepository=productRepository;
    }
    public ProductDetailData createProductData (ProductCreateData productCreateData) throws ProductFoundByIdException {
        ProductEntity productEntity=new ProductEntity(productCreateData);
        if(productRepository.existsById(productEntity.getpId())){
            throw new ProductFoundByIdException();
        }
        return new ProductDetailData(productRepository.save(productEntity));
    }
}

The following is the Api:

package com.fsse2207.project_backend.api;

import com.fsse2207.project_backend.data.ProductCreateData;
import com.fsse2207.project_backend.data.ProductDetailData;
import com.fsse2207.project_backend.data.dto.CreateRequestDto;
import com.fsse2207.project_backend.data.dto.CreateResponseDto;
import com.fsse2207.project_backend.exception.ProductFoundByIdException;
import com.fsse2207.project_backend.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductApi {
    private ProductService productService;
    @Autowired
    public ProductApi(ProductService productService){
        this.productService=productService;
    }
    @PostMapping
    public CreateResponseDto createResponseDto(@RequestBody CreateRequestDto createRequestDto) throws ProductFoundByIdException {
        ProductCreateData productCreateData=new ProductCreateData(createRequestDto);
        ProductDetailData productDetailData =productService.createProductData(productCreateData);
        return new CreateResponseDto(productDetailData);
    }
}
2 Answers

I found the problem:

I didn't add the implements keyword in the class definition of ProductServiceImpl so it was not connected to the bean, aka the interface, aka the service layer.

First of all you should not add annotation @Service for ProductService interface. Moreover this can happen when you have your Class Application in "another package". You can solve the problem using annotation @ComponentScan (basePackages = {"your.company.domain.package"})

Related