Bean定義繼承
bean定義可以包含很多的配置信息,包括構造函數的參數,屬性值,容器的具體信息例如初始化方法,靜態工廠方法名,等等。子bean的定義繼承副定義的配置數據。子定義可以根據需要重寫一些值,或者添加其他值。
Spring Bean定義的繼承與Java類的繼承無關,但是繼承的概念是一樣的。你可以定義一個父bean的定義作為模板和其他子bean就可以從父bean中繼承所需的配置。
當你使用基於XML的配置元數據時,通過使用父屬性,指定父bean作為該屬性的值來表明自bean的定義。
Bean.xml:在該配置文件中我們定義有兩個屬性message1,message2的“helloworld“bean,然后,使用parent屬性把”helloIndia“bean定義為”helloworld“的孩子。這個字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.tuorialsponit.HelloWorld"> <property name="message1" value="Hello world"/> <property name="message2" value="Hello Second World"></property> </bean> <bean id="helloIndia" class="com.tuorialsponit.HelloIndia" parent="helloworld"> <property name="message1" value="hello India"></property> <property name="message3" value="Namaste India"></property> </bean> </beans>
HelloWorld.java的內容:
package com.tuorialsponit; public class HelloWorld { private String message1; private String message2; public String getMessage1() { return message1; } public void setMessage1(String message1) { this.message1 = message1; } public String getMessage2() { return message2; } public void setMessage2(String message2) { this.message2 = message2; } }
HelloIndia.java:
package com.tuorialsponit; public class HelloIndia { private String message1; private String message2; private String message3; public String getMessage1() { return message1; } public void setMessage1(String message1) { this.message1 = message1; } public String getMessage2() { return message2; } public void setMessage2(String message2) { this.message2 = message2; } public String getMessage3() { return message3; } public void setMessage3(String message3) { this.message3 = message3; } }
MainApp.java內容:
package com.tuorialsponit; 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 obj1 = (HelloWorld) context.getBean("helloworld"); System.out.println(obj1.getMessage1()); System.out.println(obj1.getMessage2()); System.out.println("-----------------------"); HelloIndia obj2 = (HelloIndia) context.getBean("helloIndia"); System.out.println(obj2.getMessage1()); System.out.println(obj2.getMessage2()); System.out.println(obj2.getMessage3()); // String message = obj.getMessage(); // System.out.println(message); } }
執行結果:

在這里你可以觀察到,我們創建”helloIndia“bean的同時並沒有傳遞message2,但是由於Bean定義的繼承,所以它傳遞了message2.
Bean定義模板
<?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="beanTemplate" abstract="true"> <property name="message1" value="Hello world"/> <property name="message2" value="Hello dddddddddSecond World"></property> <property name="message3" value="Hello world1"/> </bean> <bean id="helloworld" class="com.tuorialsponit.HelloWorld"> <property name="message1" value="Hello world"/> <property name="message2" value="Hello Second World"></property> </bean> <bean id="helloIndia" class="com.tuorialsponit.HelloIndia" parent="beanTemplate"> <property name="message1" value="hello India"></property> <property name="message3" value="Namaste India"></property> </bean> </beans>
父bean(abstract)自身不能被實例化,因為它是不完整的,而且它也被明確地標記為抽象的。當一個定義是抽象的,它僅僅作為一個純粹的模板bean定義來使用,充當子定義的父定義使用。
