【问题标题】:Dependency Injection in constructors构造函数中的依赖注入
【发布时间】: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


【解决方案1】:

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

要么从配置中删除&lt;constructor-arg type="java.lang.String" value="hello"/&gt;&lt;constructor-arg type="int" value="1"/&gt;,要么将相应的缺失参数添加到类Meal 的构造函数中。

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

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 2018-01-17
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多