spring中bean的定义包含很多信息,如,构造器参数、property指定的依赖项、初始化方法、工厂类和工厂方法等.

如果spring容器的中每个bean都重复声明这些属性,是非常烦人也是十分低效易出错的.好在spring的bean定义可

以继承.

一个子的bean定义可以从一个父bean定义中继承得到所有的属性,并且子bean的定义可以覆盖其通过继承得来的

父bean定义的属性.

可以在一个bean的定义中使用parent属性指定其需要继承的bean定义.来看个例子:

1.新建包com.tutorialspoint.bean_inherit,并在包中新建HelloWorld和HelloIndia类.内容分别如下:

HelloWorld.java如下:

package com.tutorialspoint.bean_inherit;

public class HelloWorld {

    private String message1;
private String message2; public void setMessage1(String message) {
this.message1 = message;
} public void setMessage2(String message) {
this.message2 = message;
} public void getMessage1() {
System.out.println("World Message1 : " + message1);
} public void getMessage2() {
System.out.println("World Message2 : " + message2);
}
}

HelloIndia.java如下:

package com.tutorialspoint.bean_inherit;

public class HelloIndia {

    private String message1;
private String message2;
private String message3; public void setMessage1(String message) {
this.message1 = message;
} public void setMessage2(String message) {
this.message2 = message;
} public void setMessage3(String message) {
this.message3 = message;
} public void getMessage1() {
System.out.println("India Message1 : " + message1);
} public void getMessage2() {
System.out.println("India Message2 : " + message2);
} public void getMessage3() {
System.out.println("India Message3 : " + message3);
}
}

2.在src目录下新建bean_inherits.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-3.0.xsd"> <bean id="helloWorld" class="com.tutorialspoint.bean_inherit.HelloWorld">
<property name="message1" value="Hello World!"/>
<property name="message2" value="Hello Second World!"/>
</bean> <!-- 通过parent属性指定需要继承的bean的定义 -->
<!-- 覆盖了parent的message1属性 -->
<!-- 继承了parent的message2属性 -->
<!-- 新加了helloIndia的message3属性 -->
<bean id="helloIndia" class="com.tutorialspoint.bean_inherit.HelloIndia" parent="helloWorld">
<property name="message1" value="Hello India!"/>
<property name="message3" value="Namaste India!"/>
</bean> </beans>

3.在包com.tutorialspoint.bean_inherit中新建MainApp.java类,内容如下:

package com.tutorialspoint.bean_inherit;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean_inherits.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.getMessage1();
objA.getMessage2(); HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
objB.getMessage1();
objB.getMessage2();
objB.getMessage3(); context.registerShutdownHook();
}
}

4.运行代码,检查结果:

bean定义的模版

上述如果我们仅需要使用HelloIndia类,单纯为了继承而定义HelloWorld类是比较繁琐的.好在spring支持bean定义的模板

.bean定义的模板就是把<bean/>元素中添加abstract="true"属性,而不必指定class属性.其他的bean直接从bean模板继

承。我们把上述代码的配置文件bean_inherits.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-3.0.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> <!-- 通过parent属性指定需要继承的bean定义的模板-->
<!-- 覆盖了parent的message1属性 -->
<!-- 继承了parent的message2属性 -->
<!-- 继承了parent的message3属性 -->
<bean id="helloIndia" class="com.tutorialspoint.bean_inherit.HelloIndia" parent="beanTeamplate">
<property name="message1" value="Hello India!"/>
</bean> </beans>

并把MainApp.java修改成如下内容:

package com.tutorialspoint.bean_inherit;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("bean_inherits.xml"); HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
objB.getMessage1();
objB.getMessage2();
objB.getMessage3(); context.registerShutdownHook();
}
}

继续运行MainApp.java类,检查结果,可以看到已经正常运行:

最新文章

  1. WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
  2. c++减法高精度算法
  3. AMap编辑折线、多边形、圆
  4. mysql python image
  5. 转载~vim配置文件
  6. 如何在MyEclipse中部署struts2的环境
  7. Redis数据结构底层知识总结
  8. Vue浅谈
  9. vs2013和.net 4.5.1调用.net core中的Kestrel(基于libuv)的http服务器代码 两种方式
  10. rails将类常量重构到数据库对应的表中之一
  11. java代码的编译、执行过程
  12. Spring MVC 使用介绍(十)—— 编码
  13. vue css背景图片打包后路径问题
  14. 数据库及ORM
  15. Netty学习记录
  16. 735. Asteroid Collision彗星相撞后的消失数组
  17. Java图片比对
  18. C语言基础语法
  19. swift - UISlider 的用法
  20. Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)

热门文章

  1. ASP .NET CORE 读取配置文件的方法
  2. 轻量级HTTP服务器Nginx(入门与安装篇)
  3. 2.NBU管理NetBackup
  4. 2017.9.22 HTML学习总结--JavaScript脚本语言
  5. matlab 大块注释和取消注释的快捷键
  6. npm包发布过程
  7. Command &quot;python setup.py egg_info&quot; failed with error code 1 in /tmp/pip-build-9enuqi/MySQL-python/
  8. H5新增的Web Storage本地存储
  9. html5 canvas中CanvasGradient对象用法
  10. opendaylight安装