简体   繁体   English

创建名称为'reportsController'的bean时出错:自动连接的依赖项注入失败;

[英]Error creating bean with name 'reportsController': Injection of autowired dependencies failed;

I've been getting this exception in the console.I tried a lot and could not resolve the issue 我一直在控制台中遇到此异常。我尝试了很多,但无法解决问题

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private main.java.com.shc.mso.service.VendorService main.java.com.shc.mso.controllers.ReportsController.vendorsservice; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [main.java.com.shc.mso.service.VendorService] 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:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
    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 main.java.com.shc.mso.service.VendorService main.java.com.shc.mso.controllers.ReportsController.vendorsservice; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [main.java.com.shc.mso.service.VendorService] 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)
    ... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [main.java.com.shc.mso.service.VendorService] 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:1100)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 24 more

I tried looking at all the answers but could not figure out the solution. 我尝试查看所有答案,但找不到解决方案。

ReportsController.class ReportsController.class

package main.java.com.shc.mso.controllers;

import java.util.ArrayList;
import java.util.List;




import main.java.com.shc.mso.model.PandLReport;
import main.java.com.shc.mso.model.Vendor;



import main.java.com.shc.mso.service.PandLService;
import main.java.com.shc.mso.service.VendorService;

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





import com.google.gson.Gson;

/*
 *
 * This is the Reports controller class. 
 * It invokes several methods present in Service layer; which in turn invokes methods present in DAO layer.
 * It contains methods which creates jqGrid data, models, validations etc.
 *
 * */
@Controller
public class ReportsController {

    //Logger myLog = LogManager.getLogger(ReportsController.class);


    @Autowired
    private VendorService vendorsservice; 


    @RequestMapping(value="/pandlreport", method = RequestMethod.GET)  
     public ModelAndView listEmployees() {  
           Gson gson = new Gson();
           List list=new ArrayList<String>();
           for (Vendor v:vendorsservice.listVendors()){
               String json = new String();
               json = gson.toJson(v);
               list.add(json);
           }

            System.out.println(list.toArray());
            ModelAndView modelAndView = new ModelAndView("employeesList");
            modelAndView.addObject("employeesList", list);
            System.out.println(modelAndView);
            return modelAndView;
     }  

}

VendorService.class VendorService.class

package main.java.com.shc.mso.service;
import java.util.ArrayList;
import main.java.com.shc.mso.model.Vendor;



public interface VendorService{


    public ArrayList<Vendor> listVendors();
}

VendorServiceImpl.class VendorServiceImpl.class

package main.java.com.shc.mso.service;

import java.util.List;
import main.java.com.shc.mso.dao.VendorDao;
import main.java.com.shc.mso.model.Vendor;


import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;  

@Service("vendorService")
@Transactional
public class VendorServiceImpl {

     @Autowired(required=true)  
     private VendorDao vendorDao; 

     @Transactional
     public List<Vendor> listVendors(){

         return vendorDao.listVendors();
     }
}

Spring.xml Spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee.xsd

        http://www.springframework.org/schema/lang 
        http://www.springframework.org/schema/lang/spring-lang.xsd

        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd

        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- Activates various annotations to be detected in bean classes -->
    <mvc:annotation-driven />

    <context:component-scan base-package="main.java.com.shc.mso.controllers" />
     <context:component-scan base-package="main.java.com.shc.mso.dao" />
      <context:component-scan base-package="main.java.com.shc.mso.model" />
       <context:component-scan base-package="main.java.com.shc.mso.service" />
        <context:component-scan base-package="main.java.com.shc.mso.util" />


    <!-- Forwards requests to the "/" resource to the "welcome" view -->
    <mvc:view-controller path="/" view-name="login"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean 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>

    <!-- View Resolver for Excel Documents -->
    <bean id="nonJSPViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="order" value="0" />
        <property name="location" value="/WEB-INF/spring/spring-excel-views.xml" />
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <!-- Use SpringException when it is invoked-->
    <!-- Use defaultErrorView when any other exception occurs -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="com.shc.mso.util.SpringException">
                    ExceptionPage
                </prop>
            </props>
        </property>
        <property name="defaultErrorView" value="error"/>
    </bean> 


</beans>





import main.java.com.shc.mso.dao.VendorDao;
import main.java.com.shc.mso.model.Vendor;


import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;  

@Service("vendorService")
@Transactional
public class VendorServiceImpl {

     @Autowired(required=true)  
     private VendorDao vendorDao; 

     @Transactional
     public List<Vendor> listVendors(){

         return vendorDao.listVendors();
     }
}

异常提示NoSuchBeanDefinitionException: No qualifying bean of type [main.java.com.shc.mso.service.VendorService] found ,您没有将VendorService类注释为可以在spring容器中注册的服务。

I guess this is just a simple mistake VendorServiceImpl should implement the VendorService interface. 我想这只是VendorServiceImpl应该实现VendorService接口的简单错误。 @Autowire is done by type and in the code above VendorServiceImpl is not of type VendorService See also Inject and Resource and Autowired annotations @Autowire按类型完成,并且在上面的代码中,VendorServiceImpl不是VendorService类型,另请参见注入和资源以及自动装配注释

fix VendorServiceImpl as following 如下修复VendorServiceImpl

@Service
public class VendorServiceImpl implements  VendorService{

     @Autowired
     private VendorDao vendorDao; 

     @Transactional
     public List<Vendor> listVendors(){
         return vendorDao.listVendors();
     }
}

then in controller use 然后在控制器中使用

@Autowired
  VendorService vendorService;

and call method as 和调用方法为

vendorService.listVendors();

and in spring xml keep only the following line. spring xml仅保留以下行。 you dont need all five 你不需要全部五个

<context:component-scan base-package="main.java.com.shc.mso" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BeanCreationException:创建名称为&#39;userController&#39;的bean时出错:自动连接依赖项的注入失败 - BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed 创建名称为&#39;empController&#39;的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'empController': Injection of autowired dependencies failed 创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败 - Error creating bean with name 'securityConfig': Injection of autowired dependencies failed 创建名称为&#39;initDbService&#39;的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'initDbService': Injection of autowired dependencies failed 创建名称为“ homeController”的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'homeController': Injection of autowired dependencies failed 创建名为“countriesDao”的 bean 时出错:注入自动装配的依赖项失败; - Error creating bean with name 'countriesDao': Injection of autowired dependencies failed; 创建名称为&#39;serviceController&#39;的bean时出错:自动连接的依赖项注入失败; - Error creating bean with name 'serviceController': Injection of autowired dependencies failed; 创建名称为&#39;employeeController&#39;的bean时出错:自动连接依赖项的注入失败 - Error creating bean with name 'employeeController': Injection of autowired dependencies failed 创建名称为&#39;studentController&#39;的bean时出错:自动连接依赖项的注入失败 - Error creating bean with name 'studentController': Injection of autowired dependencies failed 创建名称为“ clientsDaoImpl”的bean时出错:自动连接依赖项的注入失败; - Error creating bean with name 'clientsDaoImpl': Injection of autowired dependencies failed;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM