Spring点滴六:Spring中定义bean的继承
在基于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的继承的更多相关文章
- 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- 【Spring Framework】12种spring中定义bean的方法
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- Spring入门2. IoC中装配Bean
Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...
- spring在IoC容器中装配Bean详解
1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...
- Spring 在xml文件中配置Bean
Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...
- Spring Spring boot 获取IOC中的bean,ApplicationContext
https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...
- Spring:获取容器中的Bean
某些情况下我们要获取 IOC 容器中指定注解.类型.名字的 Bean 要获取 IOC 容器中指定条件的 Bean 可以通过 ApplicationContext 相应的方法 @Autowired pr ...
- Spring系列(六) Spring Web MVC 应用构建分析
DispatcherServlet DispatcherServlet 是Spring MVC的前端控制器名称, 用户的请求到达这里进行集中处理, 在Spring MVC中, 它的作用是为不同请求匹配 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
随机推荐
- jQuery中turn.js(翻页效果)学习笔记
Turn.js是一个内置的jQuery翻页插件1 html中引入<script type="text/javascript" src="js/turn.js&quo ...
- 用Unity的UGUI实现简单摇杆
1.在Canvas下新建一个空对象作为我们的摇杆,命名为Joystick. 摇杆由背景和杆两部分组成,所以在Joystick下新建一个Image作为摇杆的背景,命名为BG. 在BG下新建一个Image ...
- 关于UC浏览器兼容scroll事件问题
经过本人查阅无数资料,最终得出一个比较简单,具有一定兼容性的结果. $(window).scroll(function( ) { var scrollTop = document.documentEl ...
- Java 集合基础知识 List/Set/Map
一.List Set 区别 List 有序,可重复: Set 无序,不重复: 二.List Set 实现类间区别及原理 Arraylist 底层实现使用Object[],数组查询效率高 扩容机制 ...
- 3星|《实战复盘第四季·商业巨头们的变革之道》:GE、TCL、力拓集团、英美资源集团等企业总裁的变更经验
实战复盘第四季·商业巨头们的变革之道(<哈佛商业评论>增刊) 本期是<哈佛商业评论>“实战复盘”栏目的10篇文章,讲的是GE.TCL.力拓集团.英美资源集团等企业如何熬过变革期 ...
- 新手Python第一天(接触)
Python 变量 Python的变量由字母,数字,下划线组成不包含特殊字符,不能以数字开头 可以使用的名称 例如:name,name2,my_name 不可使用的名称 例如:if...(Python ...
- XSS跨站脚本
1.反射型 非持久化,需要用户自己点击才可以触发 通常出现在搜索框 <?php $id=$_GET['id']; echo $id; ?> http://127.0.0.1/test/sc ...
- Shiro异常1:java.lang.IllegalArgumentException: Line argument must contain a key and a value. Only one
按照开涛shiro的事例 ,老是出这个异常 原因在于ini文件格式错误,为什么我也不知道 我是这样改的:找到其他可以的(换行的时候能右移两下的)在换行处复制粘贴得到那个格式,新建一个ini文件,把刚刚 ...
- Enterprise Library 4.1 参考源码索引
http://www.projky.com/entlib/4.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...
- HDU 2061 Treasure the new start, freshmen!
http://acm.hdu.edu.cn/showproblem.php?pid=2061 Problem Description background:A new semester comes , ...