当前位置: 首页 > 知识库问答 >
问题:

创建名为“Reports Controller”的bean时出错:注入autowired依赖项失败;

巫马星雨
2023-03-14
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
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;
     }  

}
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();
}
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();
     }
}
<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();
     }
}

共有1个答案

闻人博
2023-03-14

按以下方式修复VendorServiceImpl

@Service
public class VendorServiceImpl implements  VendorService{

     @Autowired
     private VendorDao vendorDao; 

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

然后在控制器使用中

@Autowired
  VendorService vendorService;

并将方法调用为

vendorService.listVendors();
<context:component-scan base-package="main.java.com.shc.mso" />
 类似资料: