Search code examples
springspring-mvcspring-ioc

Why isn't @Autowired working in my Spring MVC @Controller?


I have a Spring MVC project going.

I have a WebController that has an @Autowired service.

When I run the web app on the server, the WebController is created but it doesn't autowire the service.

Been really banging my head against a wall trying to figure this out ... Any idea what's wrong?

Here's the error I get ...

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'webController': 
Unsatisfied dependency expressed through field 'myAppService'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.mycompany.myapp.controller.MyAppService'     
available: expected at least 1 bean which qualifies as 
autowire candidate. Dependency annotations:     
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

Here's my web.xml ...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Here's my dispatcher-servlet.xml ...

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

    <context:component-scan base-package="com.mycompany.myapp" />

    <mvc:annotation-driven />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

I have a WebController like so ...

package com.mycompany.myapp.controller;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mycompany.myapp.model.TableRecord;

@Controller
public class WebController {

    @Autowired
    MyAppService myAppService;

    @RequestMapping(value = "/showprojects", method = RequestMethod.GET)
    public ModelAndView showProjects(Model model) {
        ModelAndView mav = new ModelAndView();

        try {
            Set<TableRecord> recProjects = myAppService.getProjects();
            model.addAttribute("projects", recProjects);
        } catch (Exception x) {
            x.printStackTrace();
        }

        mav.setViewName("projects");
        return mav;

    }

Here's my Service interface ...

package com.mycompany.myapp.controller;

import java.util.Set;
import org.springframework.stereotype.Component;
import com.mycompany.myapp.model.TableRecord;

@Component
public interface MyAppService {

    public Set<TableRecord> getProjects() throws Exception;

}

And here's my Service implementation ...

package com.mycompany.myapp.controller;

import java.util.Set;
import org.springframework.stereotype.Component;
/* ... other import statements ... */

@Component
public class MyAppServiceImpl {

    public MyAppServiceImpl() throws Exception {
        /* ... some initialization of member variables 
        and db connection ... */
    }

    public Set<TableRecord> getProjects() throws Exception {
        /* ... implementation that returns a Set... */
    }

}

Solution

  • Your MyAppServiceImpl class does not implement the MyAppService interface.