
- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境設定
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定義
- Spring - Bean 作用域
- Spring - Bean 生命週期
- Spring - Bean 後置處理器
- Spring - Bean 定義繼承
- Spring - 依賴注入
- Spring - 注入內部 Bean
- Spring - 注入集合
- Spring - Bean 自動裝配
- 基於註解的配置
- Spring - 基於 Java 的配置
- Spring - Spring 中的事件處理
- Spring - Spring 中的自定義事件
- Spring - 使用 Spring 框架進行 AOP
- Spring - JDBC 框架
- Spring - 事務管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 進行日誌記錄
- Spring 問題及解答
- Spring - 問題及解答
- Spring 有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring - Bean 定義繼承
Bean 定義可以包含許多配置資訊,包括建構函式引數、屬性值以及容器特定的資訊,例如初始化方法、靜態工廠方法名稱等等。
子 Bean 定義繼承父定義的配置資料。子定義可以根據需要覆蓋某些值或新增其他值。
Spring Bean 定義繼承與 Java 類繼承無關,但繼承的概念相同。您可以將父 Bean 定義定義為模板,其他子 Bean 可以從父 Bean 繼承所需的配置。
當您使用基於 XML 的配置元資料時,您可以使用parent屬性指示子 Bean 定義,並將父 Bean 指定為此屬性的值。
示例
讓我們準備一個可用的 Eclipse IDE,並採取以下步驟建立 Spring 應用程式:
步驟 | 描述 |
---|---|
1 | 建立一個名為SpringExample的專案,並在建立的專案中src資料夾下建立一個com.tutorialspoint包。 |
2 | 使用新增外部 JAR選項新增所需的 Spring 庫,如Spring Hello World 示例章節中所述。 |
3 | 在com.tutorialspoint包下建立 Java 類HelloWorld、HelloIndia和MainApp。 |
4 | 在src資料夾下建立 Bean 配置檔案Beans.xml。 |
5 | 最後一步是建立所有 Java 檔案和 Bean 配置檔案的內容,並按以下說明執行應用程式。 |
以下是配置檔案Beans.xml,其中我們定義了“helloWorld” Bean,它具有兩個屬性message1和message2。接下來,“helloIndia” Bean 被定義為“helloWorld” Bean 的子 Bean,使用parent屬性。子 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 定義繼承,它被傳遞了。
Bean 定義模板
您可以建立一個 Bean 定義模板,其他子 Bean 定義可以使用它,而無需付出太多努力。在定義 Bean 定義模板時,您不應指定class屬性,而應指定abstract屬性,並應將 abstract 屬性的值指定為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 本身無法例項化,因為它不完整,並且還明確標記為abstract。當定義像這樣是抽象的時,它只能用作純模板 Bean 定義,作為子定義的父定義。