Spring bean configuration inheritance
2015-08-21 16:13
459 查看
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.
Bean configuration file
Above is a ‘
Run it
output
The ‘
If you want to make this base bean as a template and not allow others to instantiate it, you can add an ‘
Now, the ‘
In this case, the ‘
The ‘
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
countryproperty, 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
countryproperty 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 defineclassattribute, 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’)
countryproperty, 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.相关文章推荐
- “错误: 找不到或无法加载主类”解决办法
- 那些猥琐的Java笔试题集锦(持续更新)
- java获取文件md5值
- [leetcode-166]Fraction to Recurring Decimal(java)
- Java数组的基本操作方法整理
- java继承
- java变量类型
- Error: opening registry key 'Software\JavaSoft\Java Runtime Environment' could not find java.dll
- SpringMVC 表单提交参数不匹配报错
- springmvc学习笔记(二)路径配置与数据传递
- Java并发编程:同步容器
- selenium-java常用方法封装:
- java 之 注册表操作篇
- JAXB与java对象的转换
- java基础之集合框架Collection详解
- SpringMvc与Struts2的对比啦
- Java中的内部类
- Java动态库调用
- 一:HTML文本编译器 kindeditor-4.1.10 的使用 SpringMVC+jsp的实现
- 大写数字金额