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类,检查结果,可以看到已经正常运行:

[译]11-spring bean定义的继承的更多相关文章

  1. Spring Bean 定义继承

    本例子源于:W3CSchool,在此作记录 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置 ...

  2. spring学习五:Spring Bean 定义继承

    Bean 定义继承 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置数据.子定义可以根据需要 ...

  3. Spring IOC 之Bean定义的继承

    一个Bean的定义可以包含大量的配置信息,包括构造器参数.属性值以及容器规范信息,比如初始化方法.静态工厂方法名字等等.一子bean的定义可以从父bean的定义中继承配置数据信息.子bean定义可以覆 ...

  4. Spring Bean 定义

    Bean 定义 被称作 bean 的对象是构成应用程序的支柱.也是由 Spring IoC 容器管理的. bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象. 这些 bea ...

  5. Spring学习二:Spring Bean 定义

    Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean 是 ...

  6. 一个 Spring Bean 定义 包含什么?

    一个Spring Bean 的定义包含容器必知的所有配置元数据,包括如何创建一个bean,它的生命周期详情及它的依赖.

  7. Spring Bean定义配置

    1-定义bean 1.1 如果显示的指定了名称,IOC容器就是用这个名称 1.2 若没有显示指定名称,spring自带的BeanNameGenerator会使用自己的规则创建bean的名称(eg: 类 ...

  8. Spring Bean定义的三种方式

    <!--Spring容器启动配置(web.xml文件)--> <context-param> <param-name>contextConfigLocation&l ...

  9. Spring bean继承

    Bean 定义继承 bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等. 子 bean 的定义继承父定义的配置数据.子定义可以根据需要 ...

随机推荐

  1. 使用selenium grid遇到的坑,解决不了冲突,只有避免

    背景:使用到grid做分发,已经有两周,运行较稳定,分发也健壮,不知道是不是要因为运行量小,服务器也没出问题,稳定到两周后,发现分发到A服务器(10.40.2.113)和B服务器(10.40.2.11 ...

  2. ios相关配置

    Deployment Target,它控制着运行应用需要的最低操作系统版本.

  3. 【转】使用webmagic搭建一个简单的爬虫

    [转]使用webmagic搭建一个简单的爬虫 刚刚接触爬虫,听说webmagic很不错,于是就了解了一下. webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代 ...

  4. git优点缺点(简单介绍)

    什么是Git Git是目前世界上最先进的分布式版本控制系统. Git是免费.开源的 最初Git是为辅助 Linux 内核开发的,来替代 BitKeeper 作者 Linux和Git之父李纳斯·托沃兹( ...

  5. treap数组版

    然而就是将指针的地方换成int引用 就是存个代码 #include<cstdio> #include<iostream> #include<cstdlib> #in ...

  6. 11-UITableView

    UITableView 掌握 设置UITableView的dataSource.delegate UITableView多组数据和单组数据的展示 UITableViewCell的常见属性 UITabl ...

  7. BZOJ3288: Mato矩阵(欧拉函数 高斯消元)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 386  Solved: 296[Submit][Status][Discuss] Descriptio ...

  8. 【A* 网络流】codechef Chef and Cut

    高嘉煊讲的杂题:A*和网络流的练手题 题目大意 https://s3.amazonaws.com/codechef_shared/download/translated/SEPT16/mandarin ...

  9. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  10. js、jquery初始化加载顺序

    // ready 这个方法只是在页面所有的DOM加载完毕后就会触发 // 方式1 $(function(){ // do something }); // 方式2 $(document).ready( ...