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

原文地址:https://www.cnblogs.com/jxdr/p/11684178.html

时间: 2024-10-03 02:38:38

spring依赖注入报错的相关文章

Spring依赖注入 --- 简单使用说明

Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣的同学可以参考我的日志:http://www.cnblogs.com/kodoyang/p/Frame_Imitate_Spring.html 我们会用spring提供的ClassPathXmlApplicationContext实现来代替这篇日志中相应的实现 为导入的jar文件配置api 对应的L

SpringBoot导入jsp依赖始终报错

先粘出我自己的pom代码: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apac

Spring依赖注入

Spring依赖注入基础 一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是可以专注业务逻辑.因此学习Spring Framework在架构和模式方面的结构和原理,对我们在架构和模块级别的理解帮助极大.Spring Framework(参考1)的宗旨是简化Java开发,主要的手段如下: (1)在架构上解耦:通过DI(依赖注入)管理类型依赖,通过AO

SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式

在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中.依赖注入的另一种说法是"控制反转",通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做. Spring依赖注入(

(转)Java Web系列:Spring依赖注入基础

一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是可以专注业务逻辑.因此学习Spring Framework在架构和模式方面的结构和原理,对我们在架构和模块级别的理解帮助极大.Spring Framework(参考1)的宗旨是简化Java开发,主要的手段如下: (1)在架构上解耦:通过DI(依赖注入)管理类型依赖,通过AOP分离关注点,减少重复代码

java框架篇---spring 依赖注入

spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class User { private String username; private String password; public User(){} public User(String username, String password) { super(); this.username = use

dubbo spring pom文件报错:提示no declaration can be found for element &#39;dubbo:service&#39;.

pom文件报错:The matching wildcard is strict, but no declaration can be found for  element 'dubbo:service'. 解决办法: 找到dubbo的jar包,解压后在META-INF文件夹下找到dubbo.xsd 在eclipse中点击windows-->preferrence-->xml-->xmlcatalog 选择add-->catalog entry  -->file system

Java Web系列:Spring依赖注入基础

一.Spring简介 1.Spring简化Java开发 Spring Framework是一个应用框架,框架一般是半成品,我们在框架的基础上可以不用每个项目自己实现架构.基础设施和常用功能性组件,而是可以专注业务逻辑.因此学习Spring Framework在架构和模式方面的结构和原理,对我们在架构和模块级别的理解帮助极大.Spring Framework(参考1)的宗旨是简化Java开发,主要的手段如下: (1)在架构上解耦:通过DI(依赖注入)管理类型依赖,通过AOP分离关注点,减少重复代码

Spring学习笔记——Spring依赖注入原理分析

我们知道Spring的依赖注入有四种方式,分别是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 下面我们先分析下这几种注入方式 1.get/set方法注入 public class SpringAction { //注入对象springDao private SpringDao springDao; //一定要写被注入对象的set方法 public void setSpringDao(SpringDao springDao) { this.springDao = spri