[译]11-spring bean定义的继承

spring中bean的定义包含很多信息,如,构造器参数、property指定的依赖项、初始化方法、工厂类和工厂方法等.

如果spring容器的中每个bean都重复声明这些属性,是非常烦人也是十分低效易出错的.好在spring的bean定义可

以继承.

一个子的bean定义可以从一个父bean定义中继承得到所有的属性,并且子bean的定义可以覆盖其通过继承得来的

父bean定义的属性.

可以在一个bean的定义中使用parent属性指定其需要继承的bean定义.来看个例子:

1.新建包com.tutorialspoint.bean_inherit,并在包中新建HelloWorld和HelloIndia类.内容分别如下:

HelloWorld.java如下:

package com.tutorialspoint.bean_inherit;

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

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

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

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

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

 HelloIndia.java如下:

package com.tutorialspoint.bean_inherit;

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

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

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

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

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

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

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

2.在src目录下新建bean_inherits.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">

   <bean id="helloWorld" class="com.tutorialspoint.bean_inherit.HelloWorld">
      <property name="message1" value="Hello World!"/>
      <property name="message2" value="Hello Second World!"/>
   </bean>

   <!-- 通过parent属性指定需要继承的bean的定义 -->
   <!-- 覆盖了parent的message1属性 -->
   <!-- 继承了parent的message2属性 -->
   <!-- 新加了helloIndia的message3属性 -->
   <bean id="helloIndia" class="com.tutorialspoint.bean_inherit.HelloIndia" parent="helloWorld">
      <property name="message1" value="Hello India!"/>
      <property name="message3" value="Namaste India!"/>
   </bean>

</beans>

3.在包com.tutorialspoint.bean_inherit中新建MainApp.java类,内容如下:

package com.tutorialspoint.bean_inherit;

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

public class MainApp {
    public static void main(String[] args) {
        
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean_inherits.xml");

        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

        objA.getMessage1();
        objA.getMessage2();

        HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
        objB.getMessage1();
        objB.getMessage2();
        objB.getMessage3();
        
        context.registerShutdownHook();
    }
}

4.运行代码,检查结果:

bean定义的模版

上述如果我们仅需要使用HelloIndia类,单纯为了继承而定义HelloWorld类是比较繁琐的.好在spring支持bean定义的模板

.bean定义的模板就是把<bean/>元素中添加abstract="true"属性,而不必指定class属性.其他的bean直接从bean模板继

承。我们把上述代码的配置文件bean_inherits.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">

    <bean id="beanTeamplate" abstract="true">
      <property name="message1" value="Hello World!"/>
      <property name="message2" value="Hello Second World!"/>
      <property name="message3" value="Namaste India!"/>
    </bean>

   <!-- 通过parent属性指定需要继承的bean定义的模板-->
   <!-- 覆盖了parent的message1属性 -->
   <!-- 继承了parent的message2属性 -->
   <!-- 继承了parent的message3属性 -->
   <bean id="helloIndia" class="com.tutorialspoint.bean_inherit.HelloIndia" parent="beanTeamplate">
      <property name="message1" value="Hello India!"/>
   </bean>

</beans>

 并把MainApp.java修改成如下内容:

package com.tutorialspoint.bean_inherit;

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

public class MainApp {
    public static void main(String[] args) {
        
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean_inherits.xml");

        HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
        objB.getMessage1();
        objB.getMessage2();
        objB.getMessage3();
        
        context.registerShutdownHook();
    }
}

继续运行MainApp.java类,检查结果,可以看到已经正常运行:

原文地址:https://www.cnblogs.com/sysman/p/4479711.html