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

  

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="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 >

  现在当你运行:

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="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 >

  也可以重写值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< 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 >

  




==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835130.html,如需转载请自行联系原作者
相关文章
|
14天前
|
缓存 安全 Java
Spring框架中Bean是如何加载的?从底层源码入手,详细解读Bean的创建流程
从底层源码入手,通过代码示例,追踪AnnotationConfigApplicationContext加载配置类、启动Spring容器的整个流程,并对IOC、BeanDefinition、PostProcesser等相关概念进行解释
Spring框架中Bean是如何加载的?从底层源码入手,详细解读Bean的创建流程
|
1天前
|
XML Java 数据格式
spring复习02,xml配置管理bean
详细讲解了Spring框架中基于XML配置文件管理bean的各种方式,包括获取bean、依赖注入、特殊值处理、属性赋值、集合类型处理、p命名空间、bean作用域及生命周期和自动装配。
spring复习02,xml配置管理bean
|
13天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
88 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
1天前
|
XML Java 数据格式
spring复习03,注解配置管理bean
Spring框架中使用注解配置管理bean的方法,包括常用注解的标识组件、扫描组件、基于注解的自动装配以及使用注解后的注意事项,并提供了一个基于注解自动装配的完整示例。
spring复习03,注解配置管理bean
|
2天前
|
前端开发 Java Spring
关于spring mvc 的 addPathPatterns 拦截配置常见问题
关于spring mvc 的 addPathPatterns 拦截配置常见问题
|
15天前
|
Java 数据库连接 Maven
Spring基础1——Spring(配置开发版),IOC和DI
spring介绍、入门案例、控制反转IOC、IOC容器、Bean、依赖注入DI
Spring基础1——Spring(配置开发版),IOC和DI
|
26天前
|
IDE Java 开发工具
还在为繁琐的配置头疼吗?一文教你如何用 Spring Boot 快速启动,让开发效率飙升,从此告别加班——打造你的首个轻量级应用!
【9月更文挑战第2天】Spring Boot 是一款基于 Spring 框架的简化开发工具包,采用“约定优于配置”的原则,帮助开发者快速创建独立的生产级应用程序。本文将指导您完成首个 Spring Boot 项目的搭建过程,包括环境配置、项目初始化、添加依赖、编写控制器及运行应用。首先需确保 JDK 版本不低于 8,并安装支持 Spring Boot 的现代 IDE,如 IntelliJ IDEA 或 Eclipse。
81 5
|
30天前
|
Java 微服务 Spring
Spring Cloud全解析:配置中心之解决configserver单点问题
但是如果该configserver挂掉了,那就无法获取最新的配置了,微服务就出现了configserver的单点问题,那么如何避免configserver单点呢?
|
27天前
|
Java Spring 开发者
解锁 Spring Boot 自动化配置的黑科技:带你走进一键配置的高效开发新时代,再也不怕繁琐设置!
【8月更文挑战第31天】Spring Boot 的自动化配置机制极大简化了开发流程,使开发者能专注业务逻辑。通过 `@SpringBootApplication` 注解组合,特别是 `@EnableAutoConfiguration`,Spring Boot 可自动激活所需配置。例如,添加 JPA 依赖后,只需在 `application.properties` 配置数据库信息,即可自动完成 JPA 和数据源设置。这一机制基于多种条件注解(如 `@ConditionalOnClass`)实现智能配置。深入理解该机制有助于提升开发效率并更好地解决问题。
45 0
|
27天前
|
Java Spring 开发者
Spring 框架配置属性绑定大比拼:@Value 与 @ConfigurationProperties,谁才是真正的王者?
【8月更文挑战第31天】Spring 框架提供 `@Value` 和 `@ConfigurationProperties` 两种配置属性绑定方式。`@Value` 简单直接,适用于简单场景,但处理复杂配置时略显不足。`@ConfigurationProperties` 则以类级别绑定配置,简化代码并更好组织配置信息。本文通过示例对比两者特点,帮助开发者根据具体需求选择合适的绑定方式,实现高效且易维护的配置管理。
34 0