日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring中Bean的定义继承

發(fā)布時(shí)間:2023/12/10 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring中Bean的定义继承 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

Bean定義繼承

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

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

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

當(dāng)你使用基于XML的配置元數(shù)據(jù)時(shí),通過使用父屬性,指定父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>

在該配置文件中我們定義有兩個(gè)屬性message1和message2的“helloWorld”bean。然后,使用parent屬性把“helloIndia”bean定義為“helloWorld”bean的孩子。這個(gè)子bean繼承message2的屬性,重寫message1的屬性,并且引入一個(gè)屬性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();} }

運(yùn)行結(jié)果:

可以觀察到,我們創(chuàng)建“helloIndia”bean的同時(shí)并沒有傳遞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自身不能被實(shí)例化,因?yàn)樗遣煌暾?#xff0c;而且它也被明確地標(biāo)記為抽象(abstract)的。當(dāng)一個(gè)定義是抽象的,它僅僅作為一個(gè)純粹的模板bean定義來使用的,充當(dāng)子定義的父定義使用。

?

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

總結(jié)

以上是生活随笔為你收集整理的Spring中Bean的定义继承的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。