java spring 构造器注入报错
packagecn.csdn.hr.service;publicclassGeetingServiceBeanimplementsGreetingService{priv...
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) 展开
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) 展开
4个回答
展开全部
估计是使用到了不带参数的构造函数了,其他不是很清楚
追问
你有没spring构造器注入小例子源码?
追答
没找到
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
删除无参的构造方法试试
追问
试过了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般用属性注入的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询