博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring学习笔记-5】Spring中的抽象bean以及bean继承
阅读量:6967 次
发布时间:2019-06-27

本文共 1704 字,大约阅读时间需要 5 分钟。

概要:

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>

转载地址:http://qpfsl.baihongyu.com/

你可能感兴趣的文章
java inputStream ,outputStream
查看>>
Citrix桌面虚拟化解决方案介绍
查看>>
Apache - AH00451
查看>>
ImportError: No module named extern;
查看>>
asyncio 学习
查看>>
vs2010的一个opencv插件
查看>>
Layer中自定义属性的动画
查看>>
网络传输基础
查看>>
read和readFully的区别! .
查看>>
[LeetCode]: 121: Best Time to Buy and Sell Stock
查看>>
演示一个使用db vault进行安全控制的示例
查看>>
快速入门系列--Log4net日志组件
查看>>
数据结构与算法面试题80道(33)
查看>>
SaaS客服的难点和坑点全解析
查看>>
Yenista光学发布新型无源器件测试平台CTP10
查看>>
在MAC平台下使用Eclipse出现了中文乱码
查看>>
redis字典结构
查看>>
第一记: JS变量类型判断(VUE源码解读)
查看>>
es6之扩展运算符...
查看>>
find 命令详解
查看>>