鍍金池/ 教程/ Java/ Bean 定義繼承
Spring MVC Hello World 例子
事務(wù)管理
JDBC 框架概述
MVC 框架教程
Spring 自動裝配 ‘byName’
Spring 中的自定義事件
Spring 編程式事務(wù)管理
環(huán)境設(shè)置
概述
Spring 中 SQL 的存儲過程
體系結(jié)構(gòu)
Spring 中的事件處理
Spring 靜態(tài)頁面例子
基于注解的配置
依賴注入
Spring 自動裝配 ‘byType’
Spring 由構(gòu)造函數(shù)自動裝配
Spring @Qualifier 注釋
Spring ApplicationContext 容器
Spring 中基于 AOP 的 XML架構(gòu)
Bean 的生命周期
IoC 容器
注入內(nèi)部 Beans
Spring JDBC 示例
Spring 基于構(gòu)造函數(shù)的依賴注入
Spring @Required 注釋
Hello World 實例
Bean 定義
Sping 的 BeanFactory 容器
Spring 頁面重定向例子
Bean 定義繼承
Spring 中基于 AOP 的 @AspectJ
注入集合
Beans 自動裝配
Spring 異常處理例子
Spring 聲明式事務(wù)管理
Spring @Autowired 注釋
基于 Java 的配置
Spring MVC 表單處理例子
Spring——Bean 后置處理器
使用 Log4J 記錄日志
Spring 基于設(shè)值函數(shù)的依賴注入
Spring JSR-250 注釋
Bean 的作用域
Spring 框架的 AOP

Bean 定義繼承

bean 定義可以包含很多的配置信息,包括構(gòu)造函數(shù)的參數(shù),屬性值,容器的具體信息例如初始化方法,靜態(tài)工廠方法名,等等。

子 bean 的定義繼承父定義的配置數(shù)據(jù)。子定義可以根據(jù)需要重寫一些值,或者添加其他值。

Spring Bean 定義的繼承與 Java 類的繼承無關(guān),但是繼承的概念是一樣的。你可以定義一個父 bean 的定義作為模板和其他子 bean 就可以從父 bean 中繼承所需的配置。

當(dāng)你使用基于 XML 的配置元數(shù)據(jù)時,通過使用父屬性,指定父 bean 作為該屬性的值來表明子 bean 的定義。

例子

我們在適當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個 Spring 應(yīng)用程序:

步驟 描述
1 創(chuàng)建一個名稱為 SpringExample 的項目,并且在創(chuàng)建項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint。
2 使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節(jié)。
3 com.tutorialspoint 包中創(chuàng)建 Java 類 HelloWorld、HelloIndiaMainApp。
4 src 文件夾中創(chuàng)建 Beans 配置文件 Beans.xml。
5 最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容,并運行應(yīng)用程序,解釋如下所示。

下面是配置文件 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 文件的內(nèi)容:

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 文件的內(nèi)容:

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 文件的內(nèi)容:

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

一旦你創(chuàng)建源代碼和 bean 配置文件完成后,我們就可以運行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下信息:

World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!

在這里你可以觀察到,我們創(chuàng)建 “helloIndia” bean 的同時并沒有傳遞 message2,但是由于 Bean 定義的繼承,所以它傳遞了 message2。

Bean 定義模板

你可以創(chuàng)建一個 Bean 定義模板,不需要花太多功夫它就可以被其他子 bean 定義使用。在定義一個 Bean 定義模板時,你不應(yīng)該指定的屬性,而應(yīng)該指定帶 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 自身不能被實例化,因為它是不完整的,而且它也被明確地標(biāo)記為抽象的。當(dāng)一個定義是抽象的,它僅僅作為一個純粹的模板 bean 定義來使用的,充當(dāng)子定義的父定義使用。