构造函数中的依赖注入

时间:2018-01-08 13:40:23

标签: java spring dependency-injection constructor frameworks

我在构造函数中使用依赖注入我的代码有问题。我班上有三个构造函数:

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)

我不知道如何解决它。有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

bean myMeal的配置有三个构造函数参数,而实际构造函数只有一个。

从配置中删除<constructor-arg type="java.lang.String" value="hello"/><constructor-arg type="int" value="1"/>,或者将相应的缺失参数添加到类Meal的构造函数中。

简而言之,您需要在单个构造函数中包含所有参数,而不是在单独的构造函数中。