Spring bean配置继承
2016-05-27 10:37
323 查看
在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。
一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。
请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。
Bean配置文件
以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父('BaseCustomerMalaysia')这个值。
执行它
输出结果
CustomerBean Bean 只从它的父(“BaseCustomerMalaysia”)继承 country 属性。
继承抽象
在上面的例子中,'BaseCustomerMalaysia' 仍然能够实例化,例如,
如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个<bean>元素中添加一个“abstract”的属性。 例如
现在,“BaseCustomerMalaysia' Bean是一个纯粹的模板,因为Bean只能继承它,如果试图实例化它,你会遇到以下错误消息。
纯继承模板
其实,父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。这里的是一个例子
在这种情况下,“BaseCustomerMalaysia' Bean 是一个纯粹的模板,只分享其 ”country“属性。
覆盖它
但是,仍然可以指定的子bean的新值覆盖继承的值。让我们来看看这个例子
在“CustomerBean” Bean只是覆盖父(“BaseCustomerMalaysia”)country 属性,从 ‘Malaysia’ 修改为 ‘Japan’.
Spring bean配置继承是为了避免多个Bean有重复共同的值或配置是非常有用的。
一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。
请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。
package com.yiibai.common; public class Customer { private int type; private String action; private String Country; //... }
Bean配置文件
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer"> <property name="country" value="Malaysia" /> </bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父('BaseCustomerMalaysia')这个值。
执行它
package com.yiibai.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
输出结果
Customer [type=1, action=buy, Country=Malaysia]
CustomerBean Bean 只从它的父(“BaseCustomerMalaysia”)继承 country 属性。
在上面的例子中,'BaseCustomerMalaysia' 仍然能够实例化,例如,
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个<bean>元素中添加一个“abstract”的属性。 例如
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true"> <property name="country" value="Malaysia" /> </bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
现在,“BaseCustomerMalaysia' Bean是一个纯粹的模板,因为Bean只能继承它,如果试图实例化它,你会遇到以下错误消息。
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'BaseCustomerMalaysia': Bean definition is abstract
其实,父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。这里的是一个例子
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" abstract="true"> <property name="country" value="Malaysia" /> </bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia" class="com.yiibai.common.Customer"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
在这种情况下,“BaseCustomerMalaysia' Bean 是一个纯粹的模板,只分享其 ”country“属性。
但是,仍然可以指定的子bean的新值覆盖继承的值。让我们来看看这个例子
<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-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true"> <property name="country" value="Malaysia" /> </bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"> <property name="country" value="Japan" /> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
在“CustomerBean” Bean只是覆盖父(“BaseCustomerMalaysia”)country 属性,从 ‘Malaysia’ 修改为 ‘Japan’.
Customer [Country=Japan, action=buy, type=1]
总结
Spring bean配置继承是为了避免多个Bean有重复共同的值或配置是非常有用的。
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 交换机升级排障实例
- sql2008启动代理未将对象应用到实例解决方案
- Ajax教程实例详解
- 初识JQuery 实例一(first)
- JQuery入门基础小实例(1)
- Jquery具体实例介绍AJAX何时用,AJAX应该在什么地方用
- 一个小助手批处理实例代码
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- XStream使用方法总结附实例代码
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解