在基于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. php+mysql 数据库分表分段备份程序--宋正河

    <?php //宋正河 转载请注明出处 set_time_limit(0); header('content-type:text/html;charset=utf-8'); mysql_conn ...

  2. JUC——线程同步锁(LockSupport阻塞原语)

    java.util.concurrent.locks.LockSupport这个是一个独立的类,这个类的主要功能是用来解决Thread里面提供的suspend()(挂起线程).resume()(恢复运 ...

  3. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第6节: 异线程回收对象

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第六节: 异线程回收对象 异线程回收对象, 就是创建对象和回收对象不在同一条线程的情况下, 对象回收的逻辑 我 ...

  4. [转载]java面试中经常会被问到的一些算法的问题

    Java面试中经常会被问到的一些算法的问题,而大部分算法的理论及思想,我们曾经都能倒背如流,并且也能用开发语言来实现过, 可是很多由于可能在项目开发中应用的比较少,久而久之就很容易被忘记了,在此我分享 ...

  5. 笨办法学Python - 习题11-12: Asking Questions & Prompting People

    目录 1.习题 11: 提问 2.习题 12: 提示别人 3.总结 1.习题 11: 提问 学习目标:了解人机交互场景,熟悉raw_input 的用法. 1.在 Python2.x 中 raw_inp ...

  6. Mysql数据库的隔离级别

    Mysql数据库的隔离级别有四种 1.read umcommitted   读未提交(当前事务可以读取其他事务没提交的数据,会读取到脏数据) 2.read committed 读已提交(当前事务不能读 ...

  7. pycharm连接服务器

    python其他知识目录 1. pycharm当做xshell等远程工具,远程连接服务器步骤: 2.pycharm结合Linux服务器进行代码学习: 2.2使用pycharm远程在服务器上修改和执行代 ...

  8. “吃神么,买神么”的第一个Sprint计划(第五天)

    “吃神么,买神么”项目Sprint计划 ——5.25  星期一(第五天)立会内容与进度 摘要:logo2出来了,修改过不一样的风格,组内总体评价可以,但是颜色要改,色调没注意,统一决定改成与背景色一致 ...

  9. wcf服务查看工具

    文章:接口测试工具soapUI(一) 文章:VS自带WCF测试客户端简单介绍

  10. Scala入门系列(二):条件控制与循环

    条件控制与循环   if表达式 定义:if表达式是有值的,就是if或者else中最后一行语句返回的值. 例如:val isAdult = if (age > 18) 1 else 0 类型推断: ...