在春季尝试 Bean 定义继承,但没有按预期工作



我只是在涉足Spring框架。在这里,我正在尝试 bean 声明中的"parent"属性,

这是我下面为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();     
    }
}

这是以下用于定制车的代码.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 声明中的构造函数参数不起作用吗?等待评论

异常的

可读性再高不过了。在你的自定义Bean汽车中创建一个接受字符串的构造函数(春天会把它传递给TATA-SAFARI V30

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

最新更新