java spring 构造器注入报错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java spring 构造器注入报错相关的知识,希望对你有一定的参考价值。
package cn.csdn.hr.service;
public class GeetingServiceBean implements GreetingService
private int greeting;
public void setGreeting(int greeting)
this.greeting = greeting;
public GeetingServiceBean(int greeting)
this.greeting = greeting;
public GeetingServiceBean()
public void sayGeeting()
System.out.println(greeting);
<?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-2.5.xsd">
<bean id="geetingServiceBean" class="cn.csdn.hr.service.GeetingServiceBean">
<constructor-arg type="int" value="456">
</constructor-arg>
</bean>
<!--
<bean id="geetingServiceBean" class="cn.csdn.hr.service.GeetingServiceBean">
<property name="greeting">
<value>111</value>
</property>
</bean>
-->
</beans>
用set方式不会报错,但是用构造器缺报错了:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geetingServiceBean' defined in class path resource [applicationContext.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
<value>456</value>
</constructor-arg>
不要引号追问
一样不行啊。。。
追答(1)这样试试:
456
(2)删除无参构造函数定义试试;
你有没spring构造器注入小例子源码?
追答没找到 参考技术B 删除无参的构造方法试试追问试过了
参考技术C 一般用属性注入的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
以上是关于java spring 构造器注入报错的主要内容,如果未能解决你的问题,请参考以下文章
从头认识Spring-2.4 基于java的标准注解装配-@Inject-通过构造器方法注入