【发布时间】:2018-06-17 12:04:43
【问题描述】:
我的代码在构造函数中使用依赖注入有问题。我的班级中有三个构造函数:
public class Meal {
private Ingredients ingredient;
private String text;
private int;
public Meal(Ingredients ingredient) {
this.ingredient = ingredient;
}
public Meal(String text) {
this.text = text;
}
public Meal(int number) {
this.number = number;
}
}
在 bean 配置文件中是这样的:
<bean id="ingredient"
class="mytest.Sugar"
></bean>
<bean id="myMeal"
class="mytest.Meal">
<constructor-arg ref="ingredient"/>
<constructor-arg type="java.lang.String" value="hello"/>
<constructor-arg type="int" value="1"/>
</bean>
编译后出现这样的错误:
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myMeal' defined in class path resource [app.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myMeal' defined in class path resource [app.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at mytest.MainApp.main(MainApp.java:9)
我不知道如何解决它。有人可以帮我吗?
【问题讨论】:
-
好吧,如果你遵循堆栈跟踪,请查看 MainApp.java 中的第 9 行
标签: java spring dependency-injection constructor frameworks