子 Bean 的定义继承父定义的配置数据。Spring Bean 定义的继承与 Java 类的继承无关,但是继承的概念是一样的。你可以定义一个父 bean 的定义作为模板和其他子 bean 就可以从父 bean 中继承所需的配置。通过使用父属性,指定父 bean 作为该属性的值来表明子 bean 的定义
    案例代码:
    Person.java

    1. package com.baklib.custom;
    2. public class Person {
    3. private String name;
    4. private String action;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public String getAction() {
    12. return action;
    13. }
    14. public void setAction(String action) {
    15. this.action = action;
    16. }
    17. public void doAction(){
    18. System.out.println(this.getName()+"的动作是"+this.getAction());
    19. }
    20. }

    Buyer.java

    1. package com.baklib.custom;
    2. public class Buyer {
    3. private String name;
    4. private String action;
    5. private String carType;
    6. public String getName() {
    7. return name;
    8. }
    9. public void setName(String name) {
    10. this.name = name;
    11. }
    12. public String getAction() {
    13. return action;
    14. }
    15. public void setAction(String action) {
    16. this.action = action;
    17. }
    18. public String getCarType() {
    19. return carType;
    20. }
    21. public void setCarType(String carType) {
    22. this.carType = carType;
    23. }
    24. public void doAction(){
    25. System.out.println(this.getName()+"的动作是"+this.getAction());
    26. }
    27. public void buyCar(){
    28. System.out.println(this.getName()+this.getAction()+this.getCarType());
    29. }
    30. }

    MainApp.java

    1. package com.baklib.custom;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    7. Person person = (Person) context.getBean("person");
    8. person.doAction();
    9. Buyer buyer = (Buyer) context.getBean("buyer");
    10. buyer.doAction();
    11. buyer.buyCar();
    12. }
    13. }

    配置文件 Beans.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <bean id="person" class="com.baklib.custom.Person">
    6. <property name="name" value="张三" />
    7. <property name="action" value="什么都不做" />
    8. </bean>
    9. <bean id="buyer" class="com.baklib.custom.Buyer" parent="person">
    10. <property name="action" value="购买" />
    11. <property name="carType" value="小轿车" />
    12. </bean>
    13. </beans>

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. 张三的动作是什么都不做
    2. 张三的动作是购买
    3. 张三购买小轿车

    Bean 定义模板
    定义一个 Bean 定义模板时,需要指定带 true 值的抽象属性
    案例代码:
    只需修改MainApp.java和Beans.xml
    MainApp.java

    1. package com.baklib.custom;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. public class MainApp {
    5. public static void main(String[] args) {
    6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    7. Buyer buyer = (Buyer) context.getBean("buyer");
    8. buyer.doAction();
    9. buyer.buyCar();
    10. }
    11. }

    配置文件 Beans.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <!-- 定义抽象 Bean 模板 -->
    6. <bean id="beanTemplate" abstract="true">
    7. <property name="name" value="张三" />
    8. <property name="action" value="什么都不做" />
    9. <property name="carType" value="小轿车" />
    10. </bean>
    11. <bean id="buyer" class="com.baklib.custom.Buyer" parent="beanTemplate">
    12. <property name="action" value="购买" />
    13. <property name="carType" value="小轿车" />
    14. </bean>
    15. </beans>

    编写完以上代码,就可以运行这个程序,运行结束后,可以在控制台看到下面的信息

    1. 张三的动作是购买
    2. 张三购买小轿车