Let’s say we have a parameterized constructor in student class. Bean creation will be failed. See example below.
Merchant.java
package com.javabykiran;
public class Merchant {
int merchantId;
String legalBusinessName;
public Merchant(int merchantId, String legalBusinessName) {
super();
this.merchantId = merchantId;
this.legalBusinessName = legalBusinessName;
}
}
Config file – remove autowiring tag from this xml
<bean id="merch" class="com.javabykiran.Merchant" />
Client.java
Merchant merchant = (Merchant) ctx.getBean("merch");
Error on console
Error creating bean with name 'merch' defined in class path resource
[spconfigConstructor.xml]: Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.javabykiran.Merchant]: No default constructor found;
nested exception is java.lang.NoSuchMethodException: com.javabykiran.Merchant.<init>()
To solve this we will change implementation as below.
Config file – remove autowiring tag from this xml and add below lines.
<bean id="merch" class="com.javabykiran.Merchant">
<constructor-arg index="0" value="234"></constructor-arg>
<constructor-arg index="1" value="kiran"></constructor-arg>
</bean>