博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中bean配置的继承
阅读量:6987 次
发布时间:2019-06-27

本文共 2794 字,大约阅读时间需要 9 分钟。

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
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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
<
beans 
xmlns=""
    
xmlns:xsi=""
    
xsi:schemaLocation="
    
">
  
    
<
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
>

  

转载地址:http://znhpl.baihongyu.com/

你可能感兴趣的文章
基于socket.io的实时消息推送
查看>>
查询进程并杀死
查看>>
VMXNET3 vs E1000E and E1000
查看>>
7200的GRE(隧道)+ipsec(传输模式+pre-share)配置
查看>>
四、编译安装php-5.5.34
查看>>
Thinkpad X240修改bios引导,U盘安装系统
查看>>
Slave SQL: Relay log read failure: Could not parse relay log event entry.
查看>>
抽取Zabbix的图形整合到自己后台
查看>>
Linux输入子系统
查看>>
大数据_JAVA_第二天_进制转化和补码存储方式
查看>>
linux下oracle 11g dg环境搭建
查看>>
laravel安装intervention/image图像处理扩展 报错fileinfo is missing
查看>>
Jenkins(2)
查看>>
满血回归
查看>>
利用ARP欺骗另一台电脑并偷窥
查看>>
第一周作业
查看>>
Web应用的工作原理
查看>>
Python和Java就业前景对比
查看>>
Python学习笔记__9章 IO编程
查看>>
Python学习笔记__20.1章 协程
查看>>