Constructor overloading issue with spring constructor based injection
In the previous example we have
seen Dependency injection via constructor and it is very simple. Lets take a case, what if a class has two constructors with same number of input
parameters with differen types of arguments? How does spring dependency works? In this case spring gets into ambiguous situation. To resolve this,
we must specify constructor input parameter type. Here is the example to specify input parameter type:
package com.java2novice.bean;
public class Article {
private String title;
private int number;
private String body;
public Article(String title, int number){
this.title = title;
this.number = number;
}
public Article(String title, String body){
this.title = title;
this.body = body;
}
}
|
Here is the sample spring bean configuration file:
<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-3.0.xsd">
<bean id="articleBean" class="com.java2novice.bean.Article">
<constructor-arg type="java.lang.String">
<value>Java2novice article</value>
</constructor-arg>
<constructor-arg type="int">
<value>120</value>
</constructor-arg>
</bean>
</beans>
|
|