Spring 4 Injection of autowired dependencies failed

Java

29/01/2015

Pessoal,

estou iniciando os estudos no Spring 4, e estou criando serviços e DAO, porém esta dando o erro abaixo :

    GRAVE: Context initialization failed  
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private xyz.sample.baremvc.service.CategoriaService xyz.sample.baremvc.HomeController.categoriaService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [xyz.sample.baremvc.service.CategoriaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)  
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)  
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)  
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)  
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)  
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)  
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)  
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)  
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)  
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)  
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)  
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)  
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)  
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)  
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)  
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)  
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)  
        at javax.servlet.GenericServlet.init(GenericServlet.java:158)  
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)  
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)  
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)  
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)  
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)  
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)  
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)  
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)  
        at java.util.concurrent.FutureTask.run(Unknown Source)  
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  
        at java.lang.Thread.run(Unknown Source)  
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private xyz.sample.baremvc.service.CategoriaService xyz.sample.baremvc.HomeController.categoriaService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [xyz.sample.baremvc.service.CategoriaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)  
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)  
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)  
        ... 29 more  
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [xyz.sample.baremvc.service.CategoriaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)  
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)  
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)  
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)  
        ... 31 more  
      
    jan 28, 2015 8:43:52 PM org.apache.catalina.core.ApplicationContext log  
    GRAVE: StandardWrapper.Throwable  
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private xyz.sample.baremvc.service.CategoriaService xyz.sample.baremvc.HomeController.categoriaService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [xyz.sample.baremvc.service.CategoriaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}  
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)  
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)  
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)  
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)  
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)  
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)  
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)  
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)  
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)  
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)  
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)  


Segue abaixo meu código :
servlet-context.xml
    <?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:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
      
        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->  
      
        <!-- Scans within the base package of the application for @Components to configure as beans -->  
        <!-- @Controller, @Service, @Configuration, etc. -->  
        <context:component-scan base-package="xyz.sample.baremvc" />  
      
        <!-- Enables the Spring MVC @Controller programming model -->  
        <mvc:annotation-driven />  
      
    </beans>  


root-context.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        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-3.0.xsd">  
          
        <!-- Root Context: defines shared resources visible to all other web components -->  
      
        <!--  
            CSRF protection. Here we only include the CsrfFilter instead of all of Spring Security.  
            See http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf for more information on  
            Spring Security's CSRF protection  
        -->  
        <bean id="csrfFilter" class="org.springframework.security.web.csrf.CsrfFilter">  
            <constructor-arg>  
                <bean class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"/>  
            </constructor-arg>  
        </bean>  
        <!--  
            Provides automatic CSRF token inclusion when using Spring MVC Form tags or Thymeleaf. See  
            http://localhost:8080/#forms and form.jsp for examples  
        -->  
        <bean id="requestDataValueProcessor" class="org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor"/>  
    </beans>  


    @Controller  
    public class HomeController {  
          
        @Autowired  
        private CategoriaService categoriaService;  
          
      
        @RequestMapping(value = "/")  
        public String home() {  
            System.out.println("HomeController: Passing through...");  
            //return "WEB-INF/views/home.jsp";  
            return "index.jsp";  
        }  
      
        @RequestMapping(value = "/evandro")  
        public String carrega() {  
            System.out.println("HomeController: Passing through...");  
            return "WEB-INF/views/evandro.jsp";  
        }  
      
        @RequestMapping(value = "/compare", method = RequestMethod.GET)  
        public String compare(@RequestParam("input1") String input1,  
                @RequestParam("input2") String input2, Model model) {  
            String output = "According to our Comparator, '" + input1 + "' is "  
                    +  "'" + input2 + "'";  
            model.addAttribute("output", output);  
            return "WEB-INF/views/compareResult.jsp";  
        }  
          
        @RequestMapping(value = "/add", method = RequestMethod.GET)  
        public ModelAndView addEmployee(@ModelAttribute("command") CategoriaBean categoriaBean,   
                BindingResult result) {  
            Map<String, Object> model = new HashMap<String, Object>();  
            model.put("employees",  prepareListofBean(categoriaService.listCategoria()));  
            return new ModelAndView("addEmployee", model);  
        }  
          
        @RequestMapping(value = "/openStudentPage", method = RequestMethod.GET)  
        public String listStudents(Model model) {  
            System.out.println("HomeController: Passing through...");  
            return "teste.jsp";  
        }     
          
          
        private List<CategoriaBean> prepareListofBean(List<Categoria> employees){  
            List<CategoriaBean> beans = null;  
            if(employees != null && !employees.isEmpty()){  
                beans = new ArrayList<CategoriaBean>();  
                CategoriaBean bean = null;  
                for(Categoria employee : employees){  
                    bean = new CategoriaBean();  
                    bean.setDescricao(employee.getDescricao());  
                    bean.setId(employee.getId());  
                    beans.add(bean);  
                }  
            }  
            return beans;  
        }           
    }  


Alguem pode me ajudar ???
Evandro Abreu

Evandro Abreu

Curtidas 0

Respostas

Ronaldo Lanhellas

Ronaldo Lanhellas

29/01/2015

Ele não esta conseguindo injetar o categoriaService, como está sua classe CategoriaService ? Tem anotação de @Service nela ?
GOSTEI 0
Ronaldo Lanhellas

Ronaldo Lanhellas

29/01/2015

Ele não esta conseguindo injetar o categoriaService, como está sua classe CategoriaService ? Tem anotação de @Service nela ?
GOSTEI 0
Evandro Abreu

Evandro Abreu

29/01/2015

Minha classe service estava sem "@Service" e coloquei a anotação e mesmo assim o problema persiste.
Obrigado

@Service
public class CategoriaServiceImpl implements CategoriaService {
	
	@Autowired
	private CategoriaDAO categoriaDAO;
	

	@Override
	public List<Categoria> listCategoria() {
		
		return categoriaDAO.listCategoria();
	}

}


GOSTEI 0
Evandro Abreu

Evandro Abreu

29/01/2015

deu certo, inclui a anotação @Service no meu serviço e no meu dao.
GOSTEI 0
Ronaldo Lanhellas

Ronaldo Lanhellas

29/01/2015

Certo, lembre-se que todas as injeções do spring devem ser anotadas.
GOSTEI 0
Ronaldo Lanhellas

Ronaldo Lanhellas

29/01/2015

Certo, lembre-se que todas as injeções do spring devem ser anotadas.
GOSTEI 0
POSTAR