java - 在 Spring 中尝试 Bean 定义继承,但没有达到预期效果

标签 java spring

我刚刚涉足 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();     
    }

}

这是 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 声明中的构造函数参数不起作用吗? 等待评论

最佳答案

异常的可读性再好不过了。在您的 customBean 汽车中创建一个接受字符串的构造函数(Spring 将传递它 TATA-SAFARI V30)

您的第二个示例之所以有效,是因为您不再引用 commonClass 父类(super class),因此它没有定义带有参数的构造函数

关于java - 在 Spring 中尝试 Bean 定义继承,但没有达到预期效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831945/

相关文章:

java - Spring可以处理POJO吗?

spring - 如果授权 header 不存在,则无法在 Webfilter 中发送自定义正文

java - Spring 安全: Page does not redirect after login

java - 如何按 boolean 字段对 ArrayList 对象进行排序

java - 如何使用 Java 和 MySQL 确定插入或更新是否成功?

java - 生成列表中的大量数据并生成文件

Java 用文件中的分隔符替换字段(字符串/整数)

java - 无法执行目标 org.springframework.boot :spring-boot-maven-plugin:2. 2.5.RELEASE:run (default-cli)

java - org.hibernate.exception.SQLGrammarException : could not prepare statement; nested exception is javax. 持久性.PersistenceException

Spring Hibernate JPA 规范