在基于XML配置元数据中,bean标签可以包含很多配置信息,可以包含构造函数的参数,属性值以及其他一些初始化方法。子bean的定义可以继承父bean定义元数据,子bean定义可以根据需要重写父bean属性值或者添加一些其他属性。
Spring bean中的继承和Java中继承无关,只是继承的思想一致。可以把父bean作为一个定义模板,供其他子bean使用。
示例:
spring-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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="parent" class="com.test.spring.Parent"> <property name="message1" value="This is parent message1"></property> <property name="message2" value="This is parent message2"></property> </bean>
<!-- 使用parent属性来引用父bean --> <bean id="children" class="com.test.spring.Children" parent="parent">
<!-- 直接引用父bean中message1属性--> <property name="message1" value="This is parent message1"></property>
<!-- 重写父bean中message2属性--> <property name="message2" value="This is children message2"></property>
<!-- 子bean中添加了message3属性--> <property name="message3" value="This is children message3"></property> </bean> </beans>
Java 类:
package com.test.spring; public class Parent { 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; } @Override public String toString() { return "Parent [message1=" + message1 + ", message2=" + message2 + "]"; } }
----------------------------------------------------------------------------------------------------------------------------------------------------- package com.test.spring; public class Children { 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; } @Override public String toString() { return "Children [message1=" + message1 + ", message2=" + message2 + ", message3=" + message3 + "]"; } }
测试:
package com.test.spring; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class T { AbstractApplicationContext applicationcontext=null; @Before public void before() { System.out.println("》》》Spring ApplicationContext容器开始初始化了......"); applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"}); System.out.println("》》》Spring ApplicationContext容器初始化完毕了......"); } @Test public void test() { //BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class); //applicationcontext.close(); //applicationcontext.registerShutdownHook(); Parent parent=applicationcontext.getBean(Parent.class); System.out.println(parent); Children children=applicationcontext.getBean("children", Children.class); System.out.println(children); } }
测试结果:
》》》Spring ApplicationContext容器开始初始化了...... 2017-03-19 12:54:55 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5facc36f: startup date [Sun Mar 19 12:54:55 CST 2017]; root of context hierarchy 2017-03-19 12:54:55 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml] 》》》Spring ApplicationContext容器初始化完毕了...... Parent [message1=This is parent message1, message2=This is parent message2] Children [message1=This is parent message1, message2=This is children message2, message3=This is children message3