在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用。

1. Bean在不同的XML文件

如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用它。
<ref bean="someBean"/>
“CsvOutputGenerator”或“JsonOutputGenerator”通过属性标签使用'ref'属性, - 在这个例子中,Bean “OutputHelper” 在 'Spring-Common.xml' 声明可以访问其他 Bean在'Spring-Output.xml'。

File : Spring-Common.xml

<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-2.5.xsd"> <bean id="OutputHelper" class="com.yiibai.output.OutputHelper">
<property name="outputGenerator" >
<ref bean="CsvOutputGenerator"/>
</property>
</bean> </beans>

File : Spring-Output.xml

<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-2.5.xsd"> <bean id="CsvOutputGenerator" class="com.yiibai.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.yiibai.output.impl.JsonOutputGenerator" /> </beans>

2. 在同一个XML文件中的Bean

如果引用在同一个XML文件中的bean,你可以用 'ref' 标签,“local”属性引用它。

<ref local="someBean"/>
在这个例子中,Bean “OutputHelper” 在 'Spring-Common.xml' 声明可以相互访问“CsvOutputGenerator”或“JsonOutputGenerator”。

File : Spring-Common.xml

<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-2.5.xsd"> <bean id="OutputHelper" class="com.yiibai.output.OutputHelper">
<property name="outputGenerator" >
<ref local="CsvOutputGenerator"/>
</property>
</bean> <bean id="CsvOutputGenerator" class="com.yiibai.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.yiibai.output.impl.JsonOutputGenerator" /> </beans>
总结
其实,无论是在相同或不同的XML文件,“ref” 标签可以访问一个bean,但是,对于该项目的可读性,如果引用了相同的 XML文件中声明 bean,您应该使用“local”属性。

Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子的更多相关文章

  1. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  2. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  3. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  4. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  5. Spring学习四----------Bean的配置之Bean的配置项及作用域

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的作用域(每个作用域都是在同一个Bean容器中) 1.singleton:单例,指一个Bean容器中只存在一份(默认) 2.prototype ...

  6. Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean

    1,bean 的配置 <bean id="helloWorld" class="com.yfy.HelloWorld"> <property ...

  7. spring学习(四) ———— 整合web项目(SSH)

    清楚了spring的IOC 和 AOP,最后一篇就来整合SSH框架把,记录下来,以后应该会用的到. --WH 一.web项目中如何使用spring? 当tomcat启动时,就应该加载spring的配置 ...

  8. spring学习 四 对象的创建

    spring中,有三种创建对象的方式 (1)构造创建 (2)实例工厂构造 (3)静态工厂构造 一  构造器创建 在构造器创建对象时,有无参构造和有参构造 两种 (1)在spring中,默认的是无参构造 ...

  9. Spring学习(四)

    Spring Ioc容器 1.具有依赖注入功能的容器.负责实例化,定位,配置应用程序中的对象及建立这些对象间的依赖.在Spring中BeanFactory是Ioc容器的实际代表者.BeanFactor ...

随机推荐

  1. Apollo2.5 CANBUS调试笔记(测试版)

    前言:CANBUS是Apollo需要根据你的底盘写代码的地方,感觉也是Apollo最难调试的部分.这部分首先要选好CAN卡,因为不是Apollo推荐的CAN卡,驱动程序和对应接口,可能都需要自己调整, ...

  2. Epoll为我们带来了什么

    libevent中用到的,epoll是Linux下多路复用IO接口select/poll的增强版本.网上找到的介绍资料,无法标明来源. Q:网络服务器的瓶颈在哪? A:IO效率. 在大家苦苦的为在线人 ...

  3. NSOJ 4621 posters (离散化+线段树)

    posters 时间限制: 1000ms 内存限制: 128000KB 64位整型:      Java 类名: 上一题 提交 运行结果 统计 讨论版 下一题 题目描述 The citizens of ...

  4. 关于iPad上模态显示视图中的UITextField,UITextView在输入完成后无法回收键盘的问题解决。

    在iPad开发过程中遇到一个问题,UITextField 存在由UIModalPresentationFormSheet 弹出的带导航条的视图控制器中时,调用 resignFirstResponder ...

  5. STM32 以太网学习

    STM32进行以太网通信,需要  了解一下内容: 硬件层:MAC控制器  和  PHY 和 变压器 . 软件层:网络协议栈,例如:lwip协议栈,RL-TCPnet协议栈,FreeRTOS-TCP协议 ...

  6. uboot中ftd命令

    可以从u-boot官网源码下载一个比较新的u-boot, 查看它的cmd/fdt.cftp://ftp.denx.de/pub/u-boot/ fdt命令使用示例nand read.jffs2 320 ...

  7. vue+element 点击按钮后 导致 刷新页面 致url中拼接 ? 或者拼接参数

    https://blog.csdn.net/sinat_37255207/article/details/88917162 element 自己的<el-form></el-form ...

  8. Tarjan算法初探(3):求割点与桥以及双连通分量

    接上一节Tarjan算法初探(2):缩点 在此首先提出几个概念: 割点集合:一个无向连通图G 若删除它的一个点集 以及点集中所有点相连的边(任意一端在点集中)后 G中有点之间不再连通则称这个点集是它的 ...

  9. #leetcode刷题之路21-合并两个有序链表

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例:输入:1->2->4, 1->3->4输出:1->1->2-&g ...

  10. Vue使用moment.js处理前端时间格式问题

    使用moment.js处理格式显示问题: 1.首先引入moment.js插件 npm install moment --save 2.入口文件main.js中导入并使用 3.在项目中使用即可:data ...