[转载]Spring Bean Configuration Inheritance
转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/
In Spring, the inheritance is supported in bean configuration for a bean to share common values, properties or configurations.
A child bean or inherited bean can inherit its parent bean configurations, properties and some attributes. In additional, the child beans are allow to override the inherited value.
See following full example to show you how bean configuration inheritance works in Spring.
package com.mkyong.common;
public class Customer {
private int type;
private String action;
private String Country;
//...
}
Bean configuration file
<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.mkyong.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>
Above is a ‘BaseCustomerMalaysia’ bean contains a ‘Malaysia’ value for country property, and the ‘CustomerBean’ bean inherited this value from its parent (‘BaseCustomerMalaysia’).
Run it
package com.mkyong.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("SpringBeans.xml");
Customer cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}
output
Customer [type=1, action=buy, Country=Malaysia]
The ‘CustomerBean’ bean just inherited the country property from its parent (‘BaseCustomerMalaysia’).
Inheritance with abstract
In above example, the ‘BaseCustomerMalaysia’ is still able to instantiate, for example,
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
If you want to make this base bean as a template and not allow others to instantiate it, you can add an ‘abstract‘ attribute in the <bean> element. For example
<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.mkyong.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>
Now, the ‘BaseCustomerMalaysia’ bean is a pure template, for bean to inherit it only, if you try to instantiate it, you will encounter the following error message.
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
org.springframework.beans.factory.BeanIsAbstractException:
Error creating bean with name 'BaseCustomerMalaysia':
Bean definition is abstract
Pure Inheritance Template
Actually, parent bean is not necessary to define class attribute, often times, you may just need a common property for sharing. Here’s is an example
<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.mkyong.common.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
</beans>
In this case, the ‘BaseCustomerMalaysia’ bean is a pure template, to share its ‘country’ property only.
Overrride it
However, you are still allow to override the inherited value by specify the new value in the child bean. Let’s see this example
<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.mkyong.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>
The ‘CustomerBean’ bean is just override the parent (‘BaseCustomerMalaysia’) country property, from ‘Malaysia’ to ‘Japan’.
Customer [Country=Japan, action=buy, type=1]
Conclusion
The Spring bean configuration inheritance is very useful to avoid the repeated common value or configurations for multiple beans.
[转载]Spring Bean Configuration Inheritance的更多相关文章
- Spring bean configuration inheritance
In Spring, the inheritance is supported in bean configuration for a bean to share common values, pro ...
- [转载]Spring Bean Definition Inheritance
Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...
- Spring Tools 4 STS没有创建Dynamic Web Project的选项 以及 Spring Tools 4 STS New 菜单没有Spring Bean Configuration File选项
Spring Tools 4 STS没有创建Dynamic Web Project的选项 STS4默认不带Dynamic Web Project插件. 解决方法:1.打开:Help 选择 Instal ...
- STS4 add spring bean configuration file
转自:https://blog.csdn.net/asc_123456/article/details/83216577
- Spring IoC Container and Spring Bean Example Tutorial
Spring Framework is built on the Inversion of Control (IOC) principle. Dependency injection is the t ...
- Spring Bean Life Cycle Methods – InitializingBean, DisposableBean, @PostConstruct, @PreDestroy and *Aware interfaces
Spring Beans are the most important part of any Spring application. Spring ApplicationContext is res ...
- [转载]Spring Annotation Based Configuration
Annotation injection is performed before XML injection, thus the latter configuration will override ...
- [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
- Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
随机推荐
- Ajax 技术二
一.Ajax与XML案例 例:使用Ajax+XML读取数据表中的分类信息并放入下拉选框中 demo01.php 运行结果: 二.Ajax中的JSON 在Javascript中,可以通过两种方式(XML ...
- unity 开发总结
1.慎用线程,unity对线程的支持不是特别完善. 在一些网络通信,资源下载,解压文件,不得已要开线程操作的地方,需要进行线程回收. 2.不压缩的ab,比经过压缩的ab大了将近4倍. 在打包ngui的 ...
- C++二维数组动态内存分配
对于二维数组和二维指针的内存的分配 这里首选说一下一维指针和一维数组的内存分配情况. 一维: 数组:形如int a[5];这里定义了一个一维数组a,并且数组的元素个数是5,这里的a是这五个元素的整体 ...
- 打造自己的3D全景漫游
three.js 示例: 打造H5里的"3D全景漫游"秘籍 - 腾讯ISUX QQ物联星球计划 通过pano2vr直接将鱼眼全景图生成立体空间的六个面:也可通过Photos ...
- jquery插件开发模板
(function($){ $.fn.extend({ //将可选择的变量传递给方法 pluginname: function(options) { //设置默认值并用逗号隔开 var default ...
- scp实现mac与linux服务器之间文件传输
1.mac上传文件到linux服务器 scp 文件名 用户名@服务器ip:目标路径如:scp /Users/test/testFile test@xxx.xxx.xxx.xxx:/test/ 2.ma ...
- RTC搭建android下三层应用程序访问服务器MsSql-客户端
android下stringgrid已知问题: 通过点击时获取对应行的值有问题,在win下调试正常,在android下出现定位不准 二.客户端开发 1,新建工程 2,添加相关客户端控件TRtcHttp ...
- 婚庆手机APP
这是一个信息化的时代,即将步入婚姻殿堂的新人们,你们是否希望有这样一款手机软件能伴随你从结婚到婚后一路的历程呢?比如说请帖通过手机客户端来将结婚的时间地点流程共享给您的亲朋好友,将您的婚纱照.随拍.写 ...
- AJAX异步同步
为了更好的用户体验,AJAX的异步同步技术给了我们一个很好的用户体验下面是我做的一个例子. 1.客户端处理 UserId.HTML <!DOCTYPE html PUBLIC "-// ...
- MySQL 5.7.9多源复制报错修复
版本:5.7.9 用5.7.9的版本搭建MySQL多源复制测试环境 开发说复制出现问题,上去看了一下: mysql> show slave status\G******************* ...