[spring] @Autowired - No qualifying bean of type found for dependency at least 1 bean

Currently I'm facing an issue in Autowire configuration between controller and the service layer.

I'm unable to trace my mistakes.

Simple Log Info

    SEVERE:   Exception while loading the app
    SEVERE:   Undeployment failed for context /OTT
    SEVERE:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.ott.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

Below I have also given the Controller and Service Layer code and also the dispatcher-servlet.xml

Controller

package com.ott.controller;

import com.ott.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

    /**
     *
     * @author SPAR
     */
    @Controller
    public class AdminController {

        private EmployeeService employeeService;

        @RequestMapping("/employee")
        public String employee(){
            this.employeeService.fetchAll();
            return "employee";
        }

        @Autowired(required = true)
        @Qualifier(value="employeeService")
        public void setEmployeeService(EmployeeService empService) {
            this.employeeService = empService;
        }

    }

Service Interface

package com.ott.service;

import com.ott.hibernate.Employee;
import java.util.List;

/**
 *
 * @author SPAR
 */
public interface EmployeeService {

     List<Employee> fetchAll();


}

Service Interface Impl

package com.ott.service;

import com.ott.dao.EmployeeDAO;
import com.ott.hibernate.Employee;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author SPAR
 */
@Service
public class EmployeeServiceImpl implements EmployeeService{

    private EmployeeDAO employeeDAO;

    @Override
    @Transactional(readOnly = true)
    public List<Employee> fetchAll() {

        List<Employee> employees = employeeDAO.fetchAll();
        for (Employee employee : employees) {

            System.out.println("Name : "+employee.getFirst_Name() +" "+ employee.getLast_Name());

            System.out.println("Email Id : "+employee.getEmail_Id());
        }

        return employees;
    }

    @Autowired(required = true)
    @Qualifier(value="employeeDAO")
    public void setEmployeeDAO(EmployeeDAO empDAO) {
        this.employeeDAO = empDAO;
    }
}

Dispatcher-servlet.xml

 <?xml version='1.0' encoding='UTF-8' ?>
    <!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"       
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

        <context:component-scan base-package="com.ott.controller"/>
        <context:component-scan base-package="com.ott.hibernate"/>
        <context:component-scan base-package="com.ott.service"/>
        <context:component-scan base-package="com.ott.dao"/>

        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

        <mvc:resources mapping="/resources/**" location="/resources/" />

         <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/tiles-def/general-layout.xml</value>
                </list>
            </property>
        </bean>

        <bean id="viewResolverTiles" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
        </bean> 

        <mvc:annotation-driven />

        <bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix">
            <value>/WEB-INF/pages/</value>
          </property>
          <property name="suffix">
            <value>.jsp</value>
          </property>
        </bean>
    </beans>

This question is related to spring spring-mvc spring-annotations

The answer is


If you only have one bean of type EmployeeService, and the interface EmployeeService does not have other implementations, you can simply put "@Service" before the EmployeeServiceImpl and "@Autowire" before the setter method. Otherwise, you should name the special bean like @Service("myspecial") and put "@autowire @Qualifier("myspecial") before the setter method.


In your controller class, just add @ComponentScan("package") annotation. In my case the package name is com.shoppingcart.So i wrote the code as @ComponentScan("com.shoppingcart") and it worked for me.


You forgot @Service annotation in your service class.


I believe for @Service you have to add qualifier name like below :

@Service("employeeService") should solve your issue

or after @Service you should add @Qualifier annontion like below :

@Service
@Qualifier("employeeService")

Missing the 'implements' keyword in the impl classes might also be the issue


Guys I found the issue

I just tried by adding the qualifier name in employee service finally it solved my issue.

@Service("employeeService")

public class EmployeeServiceImpl implements EmployeeService{

}

@Service: It tells that particular class is a Service to the client. Service class contains mainly business Logic. If you have more Service classes in a package than provide @Qualifier otherwise it should not require @Qualifier.

Case 1:

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
}

Case2:

@Service
public class EmployeeServiceImpl implements EmployeeService{
}

both cases are working...


Just add below annotation with qualifier name of service in service Implementation class:

@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService{
}

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to spring-mvc

Two Page Login with Spring Security 3.2.x ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized The type WebMvcConfigurerAdapter is deprecated RestClientException: Could not extract response. no suitable HttpMessageConverter found Spring boot: Unable to start embedded Tomcat servlet container UnsatisfiedDependencyException: Error creating bean with name 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)

Examples related to spring-annotations

Spring Boot @Value Properties How to inject a Map using the @Value Spring Annotation? @Autowired - No qualifying bean of type found for dependency at least 1 bean Populating Spring @Value during Unit Test Spring MVC @PathVariable with dot (.) is getting truncated Is there a way to @Autowire a bean that requires constructor arguments? What does the @Valid annotation indicate in Spring?