读书人

spring2.0 bean承袭

发布时间: 2012-09-15 19:09:28 作者: rapoo

spring2.0 bean继承
1.spirng中bean 继承不同于java中继承,springbean继承类似于将父bean当作模版使用2.子bean中有许多相同的property都可以提取出来放到父类bean中,父类bean因为是类似抽象的,是虚拟的,所以可以不指定class属性。3.在父类用abstract="true"来注入一个父类(类模板),在子类中使用parent = "父类id"来继承。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="beanTemplant" abstract="true"><property name="id" value="1000"/><property name="name" value="zhangsan"/></bean><bean id="bean2" parent="beanTemplant"><property name="passwd" value="123456"/></bean><bean id="bean3" parent="beanTemplant"><!-- bean3中如果不想继承父类bean2给的name属性,只需要重新注入一下那么属性就OK了 --><property name="name" value="Tom"/></bean></beans>
??这样bean2、bean3的id和name属性通过“继承”就被注入了值。注意:如果子类中不想继承父类的某个属性,那么只需要在子类中用property标签重新注入一下就好了。

读书人网 >软件架构设计

热点推荐