我只是涉足 Spring 框架。在这里,我尝试了 bean 声明中的“父”属性,
这是我下面的 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();
}
}
下面是 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();
}
}
这是 bean-jojo.xml 文件:
<?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>
这是具有 main 方法的类,我从命令行运行:
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();
}
}
}
现在,如果我运行上述程序,我会在命令提示符下收到此错误:
[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)
但是,如果我对 bean-jojo.xml 进行以下更改,我的程序运行良好:
<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>
这是通过在 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
那么,你能告诉我为什么 bean-jojo.xml 中的 CommonCar 声明中的构造函数 args 不起作用吗?等待评论