I am just dabbling with Spring framework. Here I was trying out the “parent” attribute in the bean declaration,
This is my code below for CommonCar.java:
package com.justPractise.ex01;
public class CommonCar {
private String modelName;
private String engine;
public CommonCar(String modelName){
this.modelName = modelName;
System.out.println(" PARAMETERISED "+this.getClass().getName()+" INITIALISED..... ");
}
public CommonCar(){
System.out.println(this.getClass().getName()+" INITIALISED..... ");
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
@Override
public String toString(){
StringBuffer strb = new StringBuffer();
strb.append("\nDEFAULT CAR ");
strb.append(this.modelName);
strb.append("\nENGINE NAME ");
strb.append(this.engine);
return strb.toString();
}
}
This the code below for CustomCar.java:
package com.justPractise.ex01;
public class CustomCar {
private String modelName;
private String engine;
public CustomCar(){
System.out.println(this.getClass().getName()+" INITIALISED..... ");
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
@Override
public String toString(){
StringBuffer strb = new StringBuffer();
strb.append("\nDEFAULT CAR ");
strb.append(this.modelName);
strb.append("\nENGINE NAME ");
strb.append(this.engine);
return strb.toString();
}
}
This is the bean-jojo.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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" default-lazy-init="true">
<bean class="com.justPractise.ex01.CommonCar" id="commonCAR">
<constructor-arg value="TATA-SAFARI V30" />
<property name="engine" value="2340 CC FOUR CYLINDER 1700 BHP ENGINE" />
</bean>
<bean class="com.justPractise.ex01.CustomCar" id="customCAR" parent="commonCAR">
<property name="modelName" value="TOYOTA-INNOVA" />
</bean>
</beans>
This is the class with the main method, which I run from commandline:
package com.justPractise.ex01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainPractise01 {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = null;
CustomCar obj = null;
try{
ctx = new ClassPathXmlApplicationContext("bean-jojo.xml");
obj = (CustomCar) ctx.getBean("customCAR");
System.out.println(obj);
}catch(Exception e){
e.printStackTrace();
}
}
}
Now if I run the above programme, I get this error in the command prompt:
[java] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customCAR' defined in class path resource
[bean-jojo.xml]: 1 constructor arguments specified but no matching constructor found in bean 'customCAR' (hint: specify index/type/name
arguments for simple parameters to avoid type ambiguities)
But if I make the following changes to the bean-jojo.xml, my programme runs fine:
<bean class="com.justPractise.ex01.CommonCar" id="commonCAR">
<property name="modelName" value="TATA-SAFARI V30" />
<property name="engine" value="2340 CC FOUR CYLINDER 1700 BHP ENGINE" />
</bean>
This is the expected output I get by making the above change in xml:
[java] com.justPractise.ex01.CustomCar INITIALISED.....
[java]
[java] DEFAULT CAR TOYOTA-INNOVA
[java] ENGINE NAME 2340 CC FOUR CYLINDER 1700 BHP ENGINE
[echo] Java running completed
So, can you tell me why constructor args in the CommonCar declaration in bean-jojo.xml was not working?
Waiting for the comments
The exception could not be more readable. Create a constructor in your customBean car that accepts a String (Spring will pass it
TATA-SAFARI V30)Your second example works because you no longer reference the
commonClasssuperclass, so it is not defined a constructor with a parameter