Spring中Bean的定义继承
以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition-inheritance.html:
Bean定义继承
bean定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等。
子bean的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。
Spring Bean定义的继承与Java类的继承无关,但是继承的概念是一样的。你可以定义一个父bean作为模板和其他子bean就可以从父bean中继承所需的配置。
当你使用基于XML的配置元数据时,通过使用父属性,指定父bean作为该属性的值来表明子bean的定义。
继承例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testbeandefinition</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testbeandefinition</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies>
</project>
HelloWorld.java:
package com.jsoft.testspring.testbeandefinition;
public class HelloWorld {
private String messageString1;
public void setMessage1(String message){
this.messageString1 = message;
}
public void getMessage1(){
System.out.println(this.messageString1);
}
private String messageString2;
public void getMessage2() {
System.out.println(this.messageString2);
}
public void setMessage2(String messageString2) {
this.messageString2 = messageString2;
}
}
HelloIndia.java:
package com.jsoft.testspring.testbeandefinition;
public class HelloIndia {
private String messageString1;
public void setMessage1(String message){
this.messageString1 = message;
}
public void getMessage1(){
System.out.println(this.messageString1);
}
private String messageString2;
public void getMessage2() {
System.out.println(this.messageString2);
}
public void setMessage2(String messageString2) {
this.messageString2 = messageString2;
}
private String messageString3;
public void getMessage3() {
System.out.println(this.messageString3);
}
public void setMessage3(String messageString3) {
this.messageString3 = messageString3;
}
}
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="helloWorld" class="com.jsoft.testspring.testbeandefinition.HelloWorld">
<property name="Message1" value="Hello World Message1!"></property>
<property name="Message2" value="Hello World Message2!"></property>
</bean> <bean id="helloIndia" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="helloWorld">
<property name="Message1" value="Hello India Message1!"></property>
<property name="Message3" value="Hello India Message3!"></property>
</bean> </beans>
在该配置文件中我们定义有两个属性message1和message2的“helloWorld”bean。然后,使用parent属性把“helloIndia”bean定义为“helloWorld”bean的孩子。这个子bean继承message2的属性,重写message1的属性,并且引入一个属性message3。
App.java:
package com.jsoft.testspring.testbeandefinition; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
helloWorld.getMessage1();
helloWorld.getMessage2(); HelloIndia helloIndia = (HelloIndia)applicationContext.getBean("helloIndia");
helloIndia.getMessage1();
helloIndia.getMessage2();
helloIndia.getMessage3();
}
}
运行结果:

可以观察到,我们创建“helloIndia”bean的同时并没有传递message2,但是由于Bean定义的继承,所以它传递了message2。
模板例子:
代码逻辑不变,只需要修改beans.xml,不用指定class属性,指定带true值的abstract属性即可。
<?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="beanTeamplate" abstract="true">
<property name="message1" value="Hello World!"/>
<property name="message2" value="Hello Second World!"/>
<property name="message3" value="Namaste India!"/>
</bean> <bean id="helloIndia2" class="com.jsoft.testspring.testbeandefinition.HelloIndia" parent="beanTeamplate">
<property name="Message1" value="Hello India2 Message1!"></property>
<property name="Message3" value="Hello India2 Message3!"></property>
</bean> </beans>
父bean自身不能被实例化,因为它是不完整的,而且它也被明确地标记为抽象(abstract)的。当一个定义是抽象的,它仅仅作为一个纯粹的模板bean定义来使用的,充当子定义的父定义使用。
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test7/testbeandefinition
Spring中Bean的定义继承的更多相关文章
- spring中bean的配置详解--定义parent
在工作中碰到了好多的配置文件,具体来说是spring 中bean配置的parent的配置,搞的我一头雾水,仔细看一下spring中有关bean的配置,剖析一下,具体什么含义! 一.Spring IoC ...
- Spring 中bean的作用、定义
Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...
- Spring中bean的注入方式
首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入.依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring ...
- 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章
前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
- 一次性讲清楚spring中bean的生命周期之三:bean是如何实例化的
在前面的两篇博文<一次性讲清楚spring中bean的生命周期之一:getSingleton方法>和<一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今 ...
- Spring中Bean的作用域、生命周期
Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...
- Spring中Bean的实例化
Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
随机推荐
- Ukulele 原来你也在这里
- FTP文传协议的应用
我开发的项目中一直用到都是AFNetworking上传图片的方法,最近老大说要用FTP上传,网上的资料很少,毕竟这种上传方式现在用的不多了,于是花了一天时间学习了FTP文件传输协议.下面是我的个人理解 ...
- CSS 文本下划线 text-decoration
定义和用法 text-decoration 属性规定添加到文本的修饰. 可能的值 值 描述 none 默认.定义标准的文本. underline 定义文本下的一条线. overline 定义文本上 ...
- Lex与Yacc学习(十)之Yacc库
Yacc库 每个实现都需要有用的例程库,在UNIX系统中,可以通过cc命令行尾端给出-ly标志(或通过其他系统下的等价物)来包含库. 库的内容在不同的实现之间是不同的,但总是包括main()和yyer ...
- Cplex: MIP Callback Interface
*本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 这篇文章主要记录一些Cplex的Callback的使用方法,采用Java语言. https://www.ibm.com/support/ ...
- JQuery中xxx is not a function或者can not find $
在项目中,遇到以上两个错误,反复折腾了好久,js代码写得没有问题,jquery的文件也引入了,就是反复的报告错误,xxx is not a function.如图: 就是这样的错误,shake is ...
- iOS-MVC设计模式不足
View 的最大的任务就是向 Controller 传递用户动作事件. ViewController 不再承担一切代理和数据源的职责,通常只负责一些分发和取消网络请求以及一些其他的任务. 1.1 苹果 ...
- NYOJ 104 最大和
最大和 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个 ...
- [android开发篇]elicpse安装教程
http://www.runoob.com/eclipse/eclipse-install.html Eclipse 安装(Neon 版本) Eclipse 最新版本 Eclipse Neon,这个首 ...
- .NET重构(五):存储过程、触发器和函数的区别
导读:在触发器的学习过程中,师傅讲了它的耦合性高,建议我能用存储过程,那到底什么是存储过程呢,自己也不是特别了解,还有就是,触发器也算是一种特殊的存储过程,为什么就不建议多用呢?接下来,就谈谈触发器. ...