*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:#99ccff;}*.hl_mark_KMSmartTagYellowImg{background-color:#ffff66;}*.hl_mark_KMSmartTagOrangeImg{background-color:#ffad5b;}*.hl_mark_KMSmartTagGreenImg{background-color:#84e384;}*.hl_mark_KMSmartTagPurpleImg{background-color:#d6acff;}*.hl_mark_KMSmartTagRedImg{background-color:#ff8888;}

.wiz-todo, .wiz-todo-img {width: 16px; height: 16px; cursor: default; padding: 0 10px 0 2px; vertical-align: -7%;-webkit-user-select: none;} .wiz-todo-label { line-height: 2.5;} .wiz-todo-label-checked { color: #666;} .wiz-todo-label-unchecked {text-decoration: initial;} .wiz-todo-completed-info {padding-left: 44px; display: inline-block; } .wiz-todo-avatar { width:20px; height: 20px; vertical-align: -20%; margin-right:10px; border-radius: 2px;} .wiz-todo-account, .wiz-todo-dt { color: #666; }

概要:

Spring配置文件中,当若干个bean的配置内容大部分都是相同的,只有少部分是不同的时候,如果按照普通的方式去配置这些bean,实际有太多的重复内容被配置。
可以通过抽象bean来实现简化。
抽象bean类似java中的父类,把公有的配置写在抽象bean中,可以实现简化。


具体操作:

通过指定abstract=“true”,来声明一个bean为抽象bean,可被继承;

  1. <?xml version="1.0" encoding="GBK"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!-- 定义Axe实例 -->
  7. <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
  8. <!-- 指定abstract="true"定义抽象Bean -->
  9. <bean id="personTemplate" abstract="true">
  10. <property name="name" value="crazyit"/>
  11. <property name="axe" ref="steelAxe"/>
  12. </bean>
  13. <!-- 通过指定parent属性指定下面Bean配置可从父Bean继承得到配置信息 -->
  14. <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
  15. parent="personTemplate"/>
  16. <bean id="american" class="org.crazyit.app.service.impl.American"
  17. parent="personTemplate"/>
  18. </beans>

上面的配置与下面的等同

  1. <?xml version="1.0" encoding="GBK"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!-- 定义Axe实例 -->
  7. <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
  8. <!-- 通过指定parent属性指定下面Bean配置可从父Bean继承得到配置信息 -->
  9. <bean id="chinese" class="org.crazyit.app.service.impl.Chinese">
  10. <property name="name" value="crazyit"/>
    <property name="axe" ref="steelAxe"/>
  11. <bean/>
  12. <bean id="american" class="org.crazyit.app.service.impl.American">
  13. <property name="name" value="crazyit"/>
    <property name="axe" ref="steelAxe"/>
    <bean/>
  14. </beans>



【Spring学习笔记-5】Spring中的抽象bean以及bean继承的更多相关文章

  1. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  2. Java架构师之路 Spring学习笔记(一) Spring介绍

    前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...

  3. 1.2(Spring学习笔记)Spring中的Bean

    一.<Bean>的属性及子元素 在1.1中我们对<Bean>有了初步的认识,了解了一些基本用法. 现在我们进一步理解<Bean>属性及子元素. 我们先来看下< ...

  4. Spring学习笔记之五----Spring MVC

    Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...

  5. Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入

    本节主要内容:       1.实例化Spring容器示例    2.利用Spring容器创建JavaBean对象    3.如何控制Bean实例化    4.利用Spring实现bean属性sett ...

  6. [Spring学习笔记 5 ] Spring AOP 详解1

    知识点回顾:一.IOC容器---DI依赖注入:setter注入(属性注入)/构造子注入/字段注入(注解 )/接口注入 out Spring IOC容器的使用: A.完全使用XML文件来配置容器所要管理 ...

  7. [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解

    .xml使用注解 xml 用来定义bean的信息,注解用来配置依赖信息 ) 在配置文件中配置bean )在javaBean中用注解来指定依赖注入 )在配置文件中开启注解扫描 @Resource标签 j ...

  8. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  9. Spring 学习笔记(2) Spring Bean

    一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...

  10. Spring学习笔记(一) Spring基础IOC、AOP

    1.       注入类型 a)       Spring_0300_IOC_Injection_Type b)       setter(重要) c)       构造方法(可以忘记) d)     ...

随机推荐

  1. spoj8406

    题解: 二分+树状数组 记录以下i在当前拍第几 代码: #include<bits/stdc++.h> using namespace std; ; int a[N],f1[N],f2[N ...

  2. os、os.path模块中关于文件、目录常用的函数使用方法

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法   getcwd()   返回当前工作目录   chdir(path)   改变工作目录   listdir(path='.')   列举 ...

  3. if...then

    我到现在明白一些道理 有些事情是徒劳无功的 有些却不是 世间事不过如此,只要你肯付出相应的代价,你就可以得到.

  4. 福大软工1816 - 第八次作业(课堂实战)- 项目UML设计

    团队 学号 姓名 本次作业博客链接 031602428 苏路明(组长) https://www.cnblogs.com/Sulumer/p/9822854.html 031602401 陈瀚霖 htt ...

  5. L224 词汇题

    Elaborate 精心的 preparations were being made for the Prime Minister’s official visit to the four forei ...

  6. mysql5.7高可用架构之MHA

    一.MHA简介 MHA(Master High Availability)目前在mysql高可用方面比较成熟.是一套优秀的作为 mysql高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障 ...

  7. [LeetCode&Python] Problem 559. Maximum Depth of N-ary Tree

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  8. Battle City 优先队列+bfs

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  9. SQLite数据库学习小结——Frameworks层实现

    3. SQLite的Frameworks层实现 3.1 Frameworks层架构 Android系统方便应用使用,在Frameworks层中封装了一套Content框架,之所以叫Content框架而 ...

  10. DMABUFF

    1.DMABUF框架提供了在多设备间共享缓存的通用方法,支持DMABUF的设备驱动可以将一个DMA缓存以文件句柄的方式输出到用户空间(输出者规则),以文件句柄的方式从用户空间获取一个DMA缓存,这个文 ...