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的更多相关文章

  1. [转载]Spring Bean Configuration Inheritance

    转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...

  2. 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 ...

  3. [转载]Spring Bean Definition Inheritance

    Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...

  4. STS4 add spring bean configuration file

    转自:https://blog.csdn.net/asc_123456/article/details/83216577

  5. Spring IoC Container and Spring Bean Example Tutorial

    Spring Framework is built on the Inversion of Control (IOC) principle. Dependency injection is the t ...

  6. 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 ...

  7. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  8. Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  9. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

随机推荐

  1. NDK(13)JNIEnv和JavaVM

    转自:  http://www.cnblogs.com/canphp/archive/2012/11/13/2768937.html JNIEnv是一个与线程相关的变量,不同线程的JNIEnv彼此独立 ...

  2. C# 计时器的三种使用方法

    在.net中有三种计时器,一是System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet;二是System.Timers命名空间下的Timer类. Timer控件 ...

  3. ExtJs自学教程(2):从DOM看EXTJS

    <二> 从DOM看EXTJS 看标题可能有人会问,为啥好好的例子不看,得从DOM看起呢?答案是目标只为了一个:自运行结果把EXTJS看得更清楚点 首先,要看得靠点工具,带点放大镜什么吧?对 ...

  4. ASP.NET Web API上实现 Web Socket

    1. 什么是Web Socket Web Socket是Html5中引入的通信机制,它为浏览器与后台服务器之间提供了基于TCP的全双工的通信通道.用以替代以往的LongPooling等comet st ...

  5. Web内容管理系统 Magnolia 安装使用-挖掘优良的架构(2)

    在Windows上安装社区版  tomcat集成版 Magnolia CMS社区版本为免费发行,不需要任何GNU通用公共许可协议(第3版)条款下的授权(这个许可协议允许您在特定条款和条件下,重新分配和 ...

  6. Catalan数推导(转载)

    Raney引理: 设整数序列A = {Ai, i=1, 2, …, N},且部分和Sk=A1+…+Ak,序列中所有的数字的和SN=1,在A的N个循环表示中,有且仅有一个序列B,满足B的任意部分和Si均 ...

  7. SQL全文搜索

    ( select dd.*,t.RANK from crm_CustomerAnalyzeDetails dd ) as t on dd.ID = t.[key] ) union all ( sele ...

  8. .gitignore的使用:首次创建及事后添加无法生效.

    问题:使用sourceTree做版本管理后, Xcode工程每次打开, 都会有几个xcodeworkspace之类的文件提示改动了. 如果想ignore掉, 有两种情况: 1:整个工程没有纳入版本管理 ...

  9. zoj 2286 Sum of Divisors

    // f(n)表示 n的约数和 不包括自己// 给你一个m 求1 到 100万里面 f(n)<=m 的个数// 那么首先要用筛选求出所有出 f(n)// 然后就好办了 // 写好后 看见别人好快 ...

  10. Android的图片压缩并上传

    Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...