Spring中bean配置的继承

简介:

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.

例子如下:

1
2
3
4
5
6
7
8
9
public  class  Customer {
  
     private  int  type;
     private  String action;
     private  String Country;
  
     //...
  
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  

1
2
3
4
5
6
7
8
9
10
11
12
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);
  
     }
}

  运行结果为:Customer [type=1, action=buy, Country=Malaysia]

In above example, the ‘BaseCustomerMalaysia’ is still able to instantiate, for example,

 

1
Customer cust = (Customer)context.getBean( "BaseCustomerMalaysia" );

  类似于java的抽象类,我们可以有:(注意abstract="true")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  现在当你运行:

1
Customer cust = (Customer)context.getBean( "BaseCustomerMalaysia" );

  将会出现:

org.springframework.beans.factory.BeanIsAbstractException: 
	Error creating bean with name 'BaseCustomerMalaysia': 
	Bean definition is abstract

Pure Inheritance Template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  也可以重写值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
     xsi:schemaLocation="http://www.springframework.org/schema/beans
  
     < 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 >

  

目录
相关文章
|
5月前
|
XML Java 数据格式
Spring-实例化bean的四种方式
Spring-实例化bean的四种方式
43 0
|
8月前
|
XML Java 数据格式
spring是如何实例化bean的?spring实例化bean有哪些方式
spring是如何实例化bean的?spring实例化bean有哪些方式
|
8月前
|
设计模式 缓存 Java
Spring ioC源码深入剖析Bean的实例化 2
Spring ioC源码深入剖析Bean的实例化
29 1
|
8月前
|
XML Java 数据格式
Spring ioC源码深入剖析Bean的实例化 1
Spring ioC源码深入剖析Bean的实例化
36 0
|
存储 缓存 Java
Spring-Bean的实例化
Spring-Bean的实例化
Spring-Bean的实例化
|
Java Spring 容器
【关于Spring那些事】——Bean的实例化的三种方式
【关于Spring那些事】——Bean的实例化的三种方式
132 0
【关于Spring那些事】——Bean的实例化的三种方式
|
XML Java 数据格式
Spring基于注解方式实现对象创建(超详细)
Spring基于注解方式实现对象创建(超详细)
Spring基于注解方式实现对象创建(超详细)
|
XML Java 数据格式
Spring(五)之Bean定义继承和依赖注入
一、Bean定义继承 bean定义可以包含许多配置信息,包括构造函数参数,属性值和特定于容器的信息,例如初始化方法,静态工厂方法名称等。 子bean定义从父定义继承配置数据。子定义可以根据需要覆盖某些值或添加其他值。
1930 0
|
XML Java 数据格式
Spring Bean的继承
Spring Bean的继承
67 0
Spring Bean的继承