在Spring中尝试Bean定义继承但没有按预期方式解决

时间:2012-05-31 10:37:42

标签: java spring

我只是在涉及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>

这是我从命令行运行的主要方法的类

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无法正常工作, 等待评论

1 个答案:

答案 0 :(得分:2)

异常无法读取。在customBean car中创建一个接受String的构造函数(Spring将传递它TATA-SAFARI V30

您的第二个示例有效,因为您不再引用commonClass超类,因此未定义带参数的构造函数