在基于XML配置元数据中,bean标签可以包含很多配置信息,可以包含构造函数的参数,属性值以及其他一些初始化方法。子bean的定义可以继承父bean定义元数据,子bean定义可以根据需要重写父bean属性值或者添加一些其他属性。

Spring bean中的继承和Java中继承无关,只是继承的思想一致。可以把父bean作为一个定义模板,供其他子bean使用。

示例:

spring-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="parent" class="com.test.spring.Parent">
<property name="message1" value="This is parent message1"></property>
<property name="message2" value="This is parent message2"></property>
</bean>
<!-- 使用parent属性来引用父bean -->
<bean id="children" class="com.test.spring.Children" parent="parent">
<!-- 直接引用父bean中message1属性-->
<property name="message1" value="This is parent message1"></property>
<!-- 重写父bean中message2属性-->
<property name="message2" value="This is children message2"></property>
<!-- 子bean中添加了message3属性-->
<property name="message3" value="This is children message3"></property>
</bean>
</beans>

Java 类:

package com.test.spring;

public class Parent {
private String message1;
private String message2; public String getMessage1() {
return message1;
} public void setMessage1(String message1) {
this.message1 = message1;
} public String getMessage2() {
return message2;
} public void setMessage2(String message2) {
this.message2 = message2;
} @Override
public String toString() {
return "Parent [message1=" + message1 + ", message2=" + message2 + "]";
} }
----------------------------------------------------------------------------------------------------------------------------------------------------- package com.test.spring; public class Children {
private String message1;
private String message2;
private String message3; public String getMessage1() {
return message1;
} public void setMessage1(String message1) {
this.message1 = message1;
} public String getMessage2() {
return message2;
} public void setMessage2(String message2) {
this.message2 = message2;
} public String getMessage3() {
return message3;
} public void setMessage3(String message3) {
this.message3 = message3;
} @Override
public String toString() {
return "Children [message1=" + message1 + ", message2=" + message2
+ ", message3=" + message3 + "]";
}
}

测试:

package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
AbstractApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
//BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class);
//applicationcontext.close();
//applicationcontext.registerShutdownHook();
Parent parent=applicationcontext.getBean(Parent.class);
System.out.println(parent);
Children children=applicationcontext.getBean("children", Children.class);
System.out.println(children);
}
}

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 12:54:55 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5facc36f: startup date [Sun Mar 19 12:54:55 CST 2017]; root of context hierarchy
2017-03-19 12:54:55 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
Parent [message1=This is parent message1, message2=This is parent message2]
Children [message1=This is parent message1, message2=This is children message2, message3=This is children message3

Spring点滴六:Spring中定义bean的继承的更多相关文章

  1. 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  2. 【Spring Framework】12种spring中定义bean的方法

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  3. Spring入门2. IoC中装配Bean

    Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...

  4. spring在IoC容器中装配Bean详解

    1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...

  5. Spring 在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...

  6. Spring Spring boot 获取IOC中的bean,ApplicationContext

    https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...

  7. Spring:获取容器中的Bean

    某些情况下我们要获取 IOC 容器中指定注解.类型.名字的 Bean 要获取 IOC 容器中指定条件的 Bean 可以通过 ApplicationContext 相应的方法 @Autowired pr ...

  8. Spring系列(六) Spring Web MVC 应用构建分析

    DispatcherServlet DispatcherServlet 是Spring MVC的前端控制器名称, 用户的请求到达这里进行集中处理, 在Spring MVC中, 它的作用是为不同请求匹配 ...

  9. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

随机推荐

  1. numpy技巧

    矩阵旋转: mat_array = np.array([[1,2],[4,3]]) rotate_90 = np.zeros((2,2), np.uint8) rotate_180 = np.zero ...

  2. Unity3D画面渲染官方教程(一)对光照和渲染的介绍

    本系列是对官方教程的翻译加上自己的一些理解译著的,官方网址:https://unity3d.com/cn/learn/tutorials/s/graphics 翻译上尽量保证准确性,但不排除省略或者添 ...

  3. GlusterFS分布式存储集群-2. 使用

    参考文档: Quick Start Guide:http://gluster.readthedocs.io/en/latest/Quick-Start-Guide/Quickstart/ Instal ...

  4. PASSWORD MySQL 5.6.21-1ubuntu14.04_amd64

    /***************************************************************************** The main idea is that ...

  5. 笔试题——C++后序字符比较

    题目:从两个数组的最后一个元素开始比较,输出数组中不同元素的个数.当一个数组的所有元素比较完成后,结束比较.a数组长度5,b数组长度3,a[ 4 ]和b[ 2 ]比较. 例: 输入: 77 21 1 ...

  6. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)

    先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...

  7. python基础知识-11-函数装饰器

    python其他知识目录 1.装饰器学习前热身准备 1.1装饰器简介 1.2装饰器热身分析 ) def func(): pass v1 = v2 = func #将函数名赋予一个变量,就和变量赋值是同 ...

  8. (第十一周)约跑APP测试报告

    项目名称:约跑App 用户需求规格说明书URL:http://www.cnblogs.com/liquan/p/6071804.html 组长博客URL:http://www.cnblogs.com/ ...

  9. Data truncation: Truncated incorrect DOUBLE value:

    在写sql查询语句queryRunner.update(connection,"update account set balance=? where name=?",account ...

  10. JAVA第一次实验 ——凯撒密码

    课程:Java程序设计 班级:1352 姓名:黄伟业 学号:20135215 成绩:            指导教师:娄嘉鹏  实验日期:2015.4.15 实验密级: 预习程度:  实验时间:19: ...