Spring中Bean的定義繼承

以下內容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition-inheritance.html:

Bean定義繼承

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

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

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

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

繼承例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jsoft.testspring</groupId><artifactId>testbeandefinition</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>testbeandefinition</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- Spring Core --><!-- http://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.4.RELEASE</version></dependency><!-- Spring Context --><!-- http://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.4.RELEASE</version></dependency></dependencies>
</project>

HelloWorld.java:

package com.jsoft.testspring.testbeandefinition;public class HelloWorld {private String messageString1;public void setMessage1(String message){this.messageString1 = message;}public void getMessage1(){System.out.println(this.messageString1);}private String messageString2;public void getMessage2() {System.out.println(this.messageString2);}public void setMessage2(String messageString2) {this.messageString2 = messageString2;}
}

HelloIndia.java:

package com.jsoft.testspring.testbeandefinition;public class HelloIndia {private String messageString1;public void setMessage1(String message){this.messageString1 = message;}public void getMessage1(){System.out.println(this.messageString1);}private String messageString2;public void getMessage2() {System.out.println(this.messageString2);}public void setMessage2(String messageString2) {this.messageString2 = messageString2;}private String messageString3;public void getMessage3() {System.out.println(this.messageString3);}public void setMessage3(String messageString3) {this.messageString3 = messageString3;}
}

beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloWorld" class="com.jsoft.testspring.testbeandefinition.HelloWorld"><property name="Message1" value="Hello World Message1!"></property><property name="Message2" value="Hello World Message2!"></property></bean>    <bean id="helloIndia" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="helloWorld"><property name="Message1" value="Hello India Message1!"></property><property name="Message3" value="Hello India Message3!"></property></bean></beans>

在該配置文件中我們定義有兩個屬性message1和message2的“helloWorld”bean。然后,使用parent屬性把“helloIndia”bean定義為“helloWorld”bean的孩子。這個子bean繼承message2的屬性,重寫message1的屬性,并且引入一個屬性message3。

App.java:

package com.jsoft.testspring.testbeandefinition;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Hello world!**/
public class App 
{public static void main( String[] args ){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");helloWorld.getMessage1();helloWorld.getMessage2();HelloIndia helloIndia = (HelloIndia)applicationContext.getBean("helloIndia");helloIndia.getMessage1();helloIndia.getMessage2();helloIndia.getMessage3();}
}

運行結果:

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

模板例子:

代碼邏輯不變,只需要修改beans.xml,不用指定class屬性,指定帶true值的abstract屬性即可。

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.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="helloIndia2" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="beanTeamplate"><property name="Message1" value="Hello India2 Message1!"></property><property name="Message3" value="Hello India2 Message3!"></property></bean></beans>

父bean自身不能被實例化,因為它是不完整的,而且它也被明確地標記為抽象(abstract)的。當一個定義是抽象的,它僅僅作為一個純粹的模板bean定義來使用的,充當子定義的父定義使用。

?

測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test7/testbeandefinition

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258690.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258690.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258690.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python實時連接oracle_Python連接Oracle

Python連接Oracle當前環境&#xff1a;Linux Centos 71. 下載安裝包cx_Oracle由于我本地Python版本是2.7,所以選擇是2.7版本wget https://pypi.python.org/packages/e1/18/00987c6a9af9568ee87d1fcba877407684a3f1b87515e5eb82d5d5acb9ff/cx_Oracle-6.0rc1-py27-1.x86_64.rpm#m…

C語言字符串函數大全

轉載自http://www.360doc.com/content/08/0723/22/26860_1462024.shtml# C語言字符串函數大全 函數名: stpcpy 功能: 拷貝一個字符串到另一個 用法: char *stpcpy(char *destin, char *source); 程序例: #include<stdio.h> #include<string.h> int main(void) { ch…

Makefile中 -I -L -l區別

轉載自&#xff1a;http://blog.csdn.net/davion_zhang/article/details/41805641 我們用gcc編譯程序時&#xff0c;可能會用到“-I”&#xff08;大寫i&#xff09;&#xff0c;“-L”&#xff08;大寫l&#xff09;&#xff0c;“-l”&#xff08;小寫l&#xff09;等參數&am…

PLT redirection through shared object injection into a running process

PLT redirection through shared object injection into a running process

python電腦版軟件下載_Python for windows

Python是一門跨平臺的腳本語言,Python規定了一個Python語法規則,實現了Python語法的解釋程序就成為了Python的解釋器,我們用的比較多的是C版本的Python,也就是使用C語言實現的Python解釋器,除此之外還有使用Java實現的Jython和使用.NET實現的IronPython,這些實現可以使Python用…

Struts優缺點

跟Tomcat、Turbine等諸多Apache項目一樣&#xff0c;是開源軟件&#xff0c;這是它的一大優點。使開發者能更深入的了解其內部實現機制。 Struts開放源碼框架的創建是為了使開發者在構建基于Java Servlet和JavaServer Pages&#xff08;JSP&#xff09;技術的Web應用時更加容易…

由Google Protocol Buffer的小例子引起的g++編譯問題

問題 學習 Google Protocol Buffer 的使用和原理時&#xff0c;提供了一個小例子&#xff0c;講述了protobuf的使用方法。 假如已經有了如下文件&#xff1a; 其中writer.cpp如下&#xff1a;#include "lm.helloworld.pb.h" #include<iostream> #include<…

用python編寫表達式求值_用Python3實現表達式求值

Problem Description yizhen has no girlfriend due to his stupid brain that he even can’t solve a simple arithmetic roblem. Can you help him If you solve it and tell him the result, then he can find his lovers! So beautiful! Input The input一、題目描述請用 …

the first day

開博第一天&#xff0c;從此記錄我生活學習的點滴&#xff0c;加油轉載于:https://www.cnblogs.com/fkissx/p/3702132.html

驅動-問題解決

今天在網上買了一個二手的電腦&#xff0c;拿回來以后&#xff0c;發現有點問題&#xff0c;一個問題就是 1.usb插上U盤以后沒有反應 解決方法&#xff1a; 嘗試一、直接在網上下載了一個360驅動大師&#xff0c;更新了一下驅動&#xff0c;沒有解決 嘗試二、在網上下載了一個驅…

Swift 學習- 02 -- 基礎部分2

class NamedShape{ var numberOfSides: Int 0 var name: String init(name: String) { self.name name } func simpleDecription() -> String { return "A shape with \(numberOfSides) \(name) sides" } } // 除了儲存簡單的屬性之外,屬性可以有 getter 和 set…

R-CNN detection 運行問題及辦法

運行caffe官方提供的jupyter 的rcnn detection&#xff0c;總是出現各種問題。先將問題及方法匯集在此&#xff1a; 1. Selective Search 的安裝問題 按照官網&#xff0c;我下載了selective_search_ijcv_with_python&#xff0c;但是在我的linux matlab2017a上總是出現問題&…

python怎么用lambda和map函數_Python之lambda匿名函數及map和filter的用法

現有兩個元組((a),(b)),((c),(d))&#xff0c;請使用python中匿名函數生成列表[{a:c},{b:d}]t1 ((a), (c))t2 ((b), (d))print(list(map(lambda t: {t[0]: t[1]}, zip(t1, t2))))l lambda t1, t2: [{i: j} for i, j in zip(t1, t2)]print(l(t1, t2))map內置函數使用&#xf…

UVALive 5903 Piece it together(二分圖匹配)

給你一個n*m的矩陣&#xff0c;每個點為B或W或.。然后你有一種碎片。碎片可以旋轉&#xff0c;問可否用這種碎片精確覆蓋矩陣。N,M<500 WB 《碎片 W 題目一看&#xff0c;感覺是精確覆蓋&#xff08;最近被覆蓋洗腦了&#xff09;&#xff0c;但是仔細分析可以知道&#xf…

將undefault和null的數據轉換成bool類型的數據 使用!!

<script> var o{}; var anull; console.info(!!o.name); </script> 輸出false 此方法是將undefault和null的數據轉換成bool類型的數據. var model avalon.define({ $id: model, defaultvalue {},});<span ms-if"!!defaultvalue .cost" >測試</…

springcloud(五):熔斷監控Hystrix Dashboard和Turbine

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具&#xff0c;通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。但是只使用Hystrix Dashboard的話, 你只能看到單個應用內的服務信息, 這明顯不夠. 我們需要一個工具能讓我們…

如何修改PKG_CONFIG_PATH環境變量

兩種情況&#xff0c;如果你只是想加上某庫的pkg&#xff0c;則選擇下面其一&#xff1a;export PKG_CONFIG_PATH/usr/lib/pkgconfig/ 或者 export PKG_CONFIG_LIBDIR/usr/lib/pkgconfig/ 如果你想覆蓋掉原來的pkg,選擇后者。因為&#xff1a;PKG_CONFIG_LIBDIR的優先級比 PKG_…

python跨包導入包_python引入跨模塊包

人生苦短&#xff0c;我學python。最近學習python&#xff0c;由于包的模塊分的比較多。所以要用到跨模塊引入 且調用中間的方法整體目錄結構如下。需求&#xff1a;在 API模塊 user.py 中 調用 plugin 模塊中 douyin_login 下的方法。貼一下最終解決方案&#xff1a;from plug…

jdk1.8版本已經不包含jdbc.odbc連接

連接access的時候發現報錯&#xff0c;無法加載jdbc.odbc類文件&#xff0c;到Java安裝目錄上jre/lib/rt.jar上找jdbcodbc類也沒有了。 找個jdk1.7安裝就ok啦。轉載于:https://www.cnblogs.com/dohn/p/3707254.html

位運算問題

位運算 位運算是把數字用二進制表示之后&#xff0c;對每一位上0或者1的運算。 理解位運算的第一步是理解二進制。二進制是指數字的每一位都是0或者1.比如十進制的2轉化為二進制之后就是10。在程序員的圈子里有一個流傳了很久的笑話&#xff0c;說世界上有10種人&#xff0c;一…