[转载]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 ...
随机推荐
- CSS3伪类
1.:last-child 比如:查找ul的最后一个li ul li:last-child { //样式 } 2.:first-child 比如:查找ul的第一个li ul li:first-chil ...
- Eclipse 快捷键 快捷输入
快捷键: 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如appli ...
- .NET中变量的类型问题
1.JAVA和C# Byte的不同. java里一个byte取值范围是-128~127, 而C#里一个byte是0~255. 首位不同. 但是底层I/O存储的数据是一样的, 比如, 十进制的100, ...
- oracle 备份与还原 及相关操作
drop user 用户名 cascade; ........删除用户 create user 用户名 identified by 密码 default tablespace 数据文件名 tempor ...
- CSS精粹之布局技巧
1.若有疑问立即检测 在出错时若能对原始代码做简单检测可以省去很多头痛问题.W3C对于XHTML与CSS都有检测工具可用,请见http://validator.w3.org 请注意,在文件开头的错误, ...
- 转 XMLHttpRequest().readyState的五种状态详解
转 http://javathinker.blog.ccidnet.com/blog-htm-itemid-1262479-do-showone-type-blog-uid-36384.html 在& ...
- jQuery对Select操作大集合
介绍了jQuery对Select的操作进行了详细的汇总. 1.jQuery添加/删除Select的Option项: 2.$("#select_id").append("& ...
- HTML5 input新增的几种类型(数字、日期、颜色选取、范围)
你可能已经听说过,HTML5里引入了几种新的input类型.在HTML5之前,大家熟知的input类型包括:text(输入框),hidden(隐藏域),submit(提交按钮)等.而HTML5到来之后 ...
- Translation perface: <<Professional JavaScript for Web Developers, 3rd Edition>>
It is a huge pitty to breaking translating this book. Sincerly speaking, I am striken by this great ...
- MySQL在ROW模式下通过binlog提取SQL语句
Linux基于row模式的binlog,生成DML(insert/update/delete)的rollback语句通过mysqlbinlog -v 解析binlog生成可读的sql文件提取需要处理的 ...