Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring Bean 定义继承
Spring Bean 定义继承
作者:--    发布时间:2019-11-20

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

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

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

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

例子

我们在适当的位置使用 eclipse ide,然后按照下面的步骤来创建一个 spring 应用程序:

步骤 描述
1 创建一个名称为 springexample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint
2 使用 add external jars 选项,添加所需的 spring 库,解释见 spring hello world example 章节。
3 com.tutorialspoint 包中创建 java 类 helloworldhelloindiamainapp
4 src 文件夹中创建 beans 配置文件 beans.xml
5 最后一步是创建的所有 java 文件和 bean 配置文件的内容,并运行应用程序,解释如下所示。

下面是配置文件 beans.xml,在该配置文件中我们定义有两个属性 message1message2 的 “helloworld” bean。然后,使用 parent 属性把 “helloindia” bean 定义为 “helloworld” bean 的孩子。这个子 bean 继承 message2 的属性,重写 message1 的属性,并且引入一个属性 message3

<?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.helloworld">
      <property name="message1" value="hello world!"/>
      <property name="message2" value="hello second world!"/>
   </bean>

   <bean id="helloindia" class="com.tutorialspoint.helloindia" parent="helloworld">
      <property name="message1" value="hello india!"/>
      <property name="message3" value="namaste india!"/>
   </bean>

</beans>

这里是 helloworld.java 文件的内容:

package com.tutorialspoint;
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;

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);
   }
}

下面是 mainapp.java 文件的内容:

package com.tutorialspoint;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class mainapp {
   public static void main(string[] args) {
      applicationcontext context = new classpathxmlapplicationcontext("beans.xml");

      helloworld obja = (helloworld) context.getbean("helloworld");

      obja.getmessage1();
      obja.getmessage2();

      helloindia objb = (helloindia) context.getbean("helloindia");
      objb.getmessage1();
      objb.getmessage2();
      objb.getmessage3();
   }
}

一旦你创建源代码和 bean 配置文件完成后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:

world message1 : hello world!
world message2 : hello second world!
india message1 : hello india!
india message2 : hello second world!
india message3 : namaste india!

在这里你可以观察到,我们创建 “helloindia” bean 的同时并没有传递 message2,但是由于 bean 定义的继承,所以它传递了 message2。

bean 定义模板

你可以创建一个 bean 定义模板,不需要花太多功夫它就可以被其他子 bean 定义使用。在定义一个 bean 定义模板时,你不应该指定的属性,而应该指定带 true 值的抽象属性,如下所示:

<?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>

   <bean id="helloindia" class="com.tutorialspoint.helloindia" parent="beanteamplate">
      <property name="message1" value="hello india!"/>
      <property name="message3" value="namaste india!"/>
   </bean>

</beans>

父 bean 自身不能被实例化,因为它是不完整的,而且它也被明确地标记为抽象的。当一个定义是抽象的,它仅仅作为一个纯粹的模板 bean 定义来使用的,充当子定义的父定义使用。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册