Spring Bean 定义继承

本例子源于:W3CSchool,在此作记录

bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等。

子 bean 的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。

Spring Bean 定义的继承与 Java 类的继承无关,但是继承的概念是一样的。你可以定义一个父 bean 的定义作为模板和其他子 bean 就可以从父 bean 中继承所需的配置。

当你使用基于 XML 的配置元数据时,通过使用父属性,指定父 bean 作为该属性的值来表明子 bean 的定义。

其实这个继承和java继承是差不多一样的理解,你可以在子类中重写父类的属性,如果没有重写就默认继承父类的,自己照着例子做一般,顺便做一下自己的笔记,总比看一遍下来好!

bu duo bibi,zhi jie kan dai ma

HelloIndia.java

package com.how2java.w3cschool.inherit;

public class HelloIndia {
    private String message1;
    private String message2;
    private String message3;

    public void setMessage1(String message1) {
        this.message1 = message1;
    }

    public void setMessage2(String message2) {
        this.message2 = message2;
    }

    public void setMessage3(String message3) {
        this.message3 = message3;
    }

    public void getMessage1() {
        System.out.println("India message is:" + message1);
    }

    public void getMessage2() {
        System.out.println("India message is :" + message2);
    }

    public void getMessage3() {
        System.out.println("India message is:" + message3);
    }

}

HelloWorld.java

package com.how2java.w3cschool.inherit;

public class HelloWorld {
    private String message1;
    private String message2;

    public void getMessage1() {
        System.out.println("World message is:" + message1);
    }

    public void setMessage1(String message1) {
        this.message1 = message1;
    }

    public void getMessage2() {
        System.out.println("World message is:" + message2);
    }

    public void setMessage2(String message2) {
        this.message2 = message2;
    }

}

MainApp.java

package com.how2java.w3cschool.inherit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        ApplicationContext context =  new ClassPathXmlApplicationContext("BeanInherit.xml");
        
        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
        
        objA.getMessage1();
        objA.getMessage2();
        
        HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
        objB.getMessage1();
        objB.getMessage2();
        objB.getMessage3();
    }

}

BeanInherit.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   
   <bean id ="helloWorld" class ="com.how2java.w3cschool.inherit.HelloWorld">
       <property name="message1" value="Hello World!"/>
       <property name="message2" value="Hello Second World!"/>
   </bean>
   
   <bean id ="helloIndia" class ="com.how2java.w3cschool.inherit.HelloIndia" parent="helloWorld">
       <property name ="message1" value ="Hello India"/>
       <property name ="message3" value ="Namaste India"/>
   </bean>
   
 </beans>

在这个例子中

 <bean id ="helloIndia" class ="com.how2java.w3cschool.inherit.HelloIndia" parent="helloWorld">

指定了1 helloIndia这个bean的父bean是HelloWorld,并且接下来的这两句指定了子bean“重写”了父bean的getmessage1和getmessage3这个两个方法,(value的值其实是传给setmessage1和setmessage3这两个方法的)

<property name ="message1" value ="Hello India"/>
<property name ="message3" value ="Namaste India"/>

因此结果可以猜到getmessage1和getmessage3的结果以子bean的为准,而getmessage2的结果继承了父bean的

输出结果如下:

原文地址:https://www.cnblogs.com/Guhongying/p/10595661.html