1

I am getting the below error no such bean definition exception while trying to run my spring boot application. Please let me know what am i missing. Its a simple spring framework but i am stuck with this error. Any help is appreciated.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cnanational.servicecommon.validation.Validator<com.cnanational.productservice.model.DealerSeriesAttributesRequest>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)

Given below is the service class which is calling the bean

@Service
public class ProductServiceImpl implements ProductService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    private final Validator<SeriesSurchargesRequest> seriesSurchargesRequestValidator;

    private final Validator<DealerSeriesAttributesRequest> dealerSeriesAttributesRequestValidator;

    private final ProductServiceDao productDao;

    @Value("${spring.application.name}")
    private String serviceName;

    @Autowired
    public ProductServiceImpl(
            final ProductServiceDao productDao, final Validator<SeriesSurchargesRequest> seriesSurchargesRequestValidator,
            final Validator<DealerSeriesAttributesRequest> dealerSeriesAttributesRequestValidator )
    {
        this.productDao = productDao;
        this.seriesSurchargesRequestValidator = seriesSurchargesRequestValidator;
        this.dealerSeriesAttributesRequestValidator = dealerSeriesAttributesRequestValidator;
    }

    @Override
    public SeriesSurcharges getSurcharges(SeriesSurchargesRequest stdOptions) {

    this.seriesSurchargesRequestValidator.validateAndThrowIfInvalid(
                stdOptions,
                "SeriesSurchargesRequest");

        SeriesSurcharges charges = productDao.findSurcharges(stdOptions);

        return charges;
    }

    @Override
    public List<DealerSeriesAttribute> getDealerSeriesAttributes(DealerSeriesAttributesRequest request) throws Exception {         
           this.dealerSeriesAttributesRequestValidator.validateAndThrowIfInvalid(
                request,
                "dealerSeriesAttributesRequest");
           
           List <DealerSeriesAttribute> result = new ArrayList<DealerSeriesAttribute>();
           
           for (ProductSeriesFilter psfilter : request.getProductSeriesFilters()) {
                log.debug("getDealerSeriesAttributes-1: ProductSeriesFilter: " + psfilter.toString());
                List<DealerSeriesAttribute> dsa = productDao.getDealerSeriesAttributes( request);
                result.addAll(dsa);
                log.debug("Product service Impl : The size of dsa, sizes={}", dsa.size());
           }           
           return new ArrayList<DealerSeriesAttribute>(result);         
    }
}

Given below is the Validator class.

public class CreateDealerSeriesAttributesRequestValidator implements Validator<DealerSeriesAttributesRequest>{

    private final SmartValidator smartValidator;

    public CreateDealerSeriesAttributesRequestValidator(final SmartValidator smartValidator)
    {
        this.smartValidator = smartValidator;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return DealerSeriesAttributesRequest.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {}

    @Override
    public SmartValidator smartValidator() {
        return smartValidator;
    }
}
Poonkodi Sivapragasam
  • 1,127
  • 4
  • 18
  • 27

2 Answers2

2

I guess your package com.cnanational.productservice.model is not under the package where your entry point ( the class annotated with @SpringBootApplication)

Spring needs to know where to scan for the beans, in spring boot by default spring boot will scan the package where the class annotated with @SpringBootApplication is and the packages under that package, also you can use @componantscan to specify the packages you need to scan, so you can solve your issue like this

@SpringBootApplication
@ComponentScan("com.cnanational.productservice.model")

or place your bean implementation under a package which will be scanned by default.

Also you need to make sure that your class is registered as a bean so if your Validator interface is not registered as bean, you can either annotate Validator or CreateDealerSeriesAttributesRequestValidator @Component or use @Bean to create an instance of CreateDealerSeriesAttributesRequestValidator under one of your configuration classes.

Note since you are using the interface to autowired you need to be careful when you have more than one implementation, you will need to help spring decide which implementation to pick using @Qualifier

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
0

It is either your SpringBootApplication class is not in the root package or the Validator class is not registered as a Spring bean.

Ensure your SpringBootApplication class is located in the root package, i.e. com.cnanational and the class com.cnanational.servicecommon.validation.Validator is annotated with @Component, which will register it as a spring bean.

Olantobi
  • 869
  • 1
  • 8
  • 16