Reputation: 65
I have problem with my code using dependency injection in constructors. I have three constructors in my class:
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;
}
}
In bean configuration file it looks like this:
<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>
After compiling I've got such an error:
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)
and I don't know how to fix it. Can someone help me?
Upvotes: 0
Views: 201
Reputation: 1511
Your configuration of bean myMeal
has three constructor arguments while the actual constructor has only one.
Either remove <constructor-arg type="java.lang.String" value="hello"/>
and <constructor-arg type="int" value="1"/>
from the configuration or add corresponding missing arguments to the constructor of the class Meal
.
In short, you need to have all the arguments in a single constructor and not in separate constructors.
Upvotes: 2