spring依赖注入报错

Posted jxdr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring依赖注入报错相关的知识,希望对你有一定的参考价值。

在上一篇文章“spring的主要内容之依赖注入”中,我总结了通过XML配置文件注入的两种方式,分别是构造方法注入和set方法注入。在使用set方法注入时,遇到了一个报错,找了好长时间才找到问题。这个错误是在启动服务时报出来的。

现在分别贴出代码和报错信息。

package com.zaoren.controller;

import java.util.List;
import java.util.Set;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zaoren.bean.BookFriend;
import com.zaoren.service.BossService;
import com.zaoren.service.UserService;


@RequestMapping(value="user")
public class UserController {
    
    private UserService uService;
    private BossService bService;
    private String bookName;
    private List<String> bookNameList;
    private Set<String> bookNoSet;
    private List<BookFriend> bookFriends;
    
    @RequestMapping(value="trade",method=RequestMethod.GET)
    public void trade() {
        System.out.println("--------->>>buy a book.");
        System.out.println("The name of the book is"+ bookName);
        
        System.out.println("查看待购清单,发现还有如下书籍:");
        for(String bookName : bookNameList) {
            System.out.println(bookName);
        }
        
        System.out.println("查看待购清单,发现还有如下书籍编号:");
        for(String bookNo : bookNoSet) {
            System.out.println(bookNo);
        }
        
        System.out.println("有书友如下:");
        for(BookFriend bookFriend : bookFriends) {
            System.out.println(bookFriend);
        }
        
        uService.searchBook();
        bService.downBook();
    }
    
    
    public void setuService(UserService uService) {
        System.out.println("调用setuService");
        this.uService = uService;
    }

    public void setbService(BossService bService) {
        System.out.println("调用setbService");
        this.bService = bService;
    }

    public void setBookName(String bookName) {
        System.out.println("调用setBookName");
        this.bookName = bookName;
    }

    public void setBookNameList(List<String> bookNameList) {
        System.out.println("调用setBookNameList");
        this.bookNameList = bookNameList;
    }

    public void setBookNoSet(Set<String> bookNoSet) {
        System.out.println("调用setBookNoSet");
        this.bookNoSet = bookNoSet;
    }

    public void setBookFriends(List<BookFriend> bookFriends) {
        System.out.println("调用setBookFriends");
        this.bookFriends = bookFriends;
    }

    
    public UserController(UserService uService,BossService bService,String bookName, List<String> bookNameList, 
            Set<String> bookNoSet, List<BookFriend>bookFriends
                                  ){ 
        System.out.println("六参构造方法调用"); 
        this.uService= uService; 
        this.bService = bService; 
        this.bookName = bookName;
        this.bookNameList = bookNameList; this.bookNoSet = bookNoSet;
        this.bookFriends = bookFriends;
    }
     
}

spring配置文件的相关代码如下:

<bean id="uService" class="com.zaoren.service.UserService"/>
    <bean id="bService" class="com.zaoren.service.BossService"/>
    <bean id="bookFriend" class="com.zaoren.bean.BookFriend"/>
    
    <bean id="uController2" class="com.zaoren.controller.UserController">
        <property name="uService" ref="uService"/>
        <property name="bService" ref="bService"/>
        <property name="bookName" value="ss"/>
        <property name="bookNameList">
            <list>
                <value>茶花女</value>
                <value>三重门</value>
                <value>明朝那些事儿</value>
            </list>
        </property>
        <property name="bookNoSet">
            <set>
                <value>111222</value>
                <value>111223</value>
                <value>111224</value>
                <value>111224</value>
            </set>
        </property>
        <property name="bookFriends">
            <list>
                <ref bean="bookFriend"/>            
                <ref bean="bookFriend"/>        
                <ref bean="bookFriend"/>
            </list>
        </property>
    </bean>

 

启动服务时的报错信息如下:

信息: Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Wed Oct 16 10:42:24 CST 2019]; root of context hierarchy
十月 16, 2019 10:42:26 上午 org.springframework.web.context.support.XmlWebApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘uController2‘ defined in class path resource [WEB-INF/spring-servlet.xml]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [java.lang.String]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
十月 16, 2019 10:42:26 上午 org.springframework.web.context.ContextLoader initWebApplicationContext
严重: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘uController2‘ defined in class path resource [WEB-INF/spring-servlet.xml]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [java.lang.String]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1051)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5197)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5720)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1707)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1697)
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.NoSuchBeanDefinitionException: No qualifying bean found for dependency [java.lang.String]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 24 more

十月 16, 2019 10:42:26 上午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘uController2‘ defined in class path resource [WEB-INF/spring-servlet.xml]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [java.lang.String]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1051)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5197)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5720)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1707)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1697)
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.NoSuchBeanDefinitionException: No qualifying bean found for dependency [java.lang.String]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 24 more

十月 16, 2019 10:42:26 上午 org.apache.catalina.core.StandardContext startInternal

以上是关于spring依赖注入报错的主要内容,如果未能解决你的问题,请参考以下文章

关于Spring依赖注入的问题

Junit5 单元测试 使用Spring 依赖注入 Idea 提示报错, 但是实际运行是没问题的,怎么解决红字报错问题?

spring autowired怎么按照name注入

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

spring 抽象类注入问题