---
title: "Spring中ref local与ref bean区别"
author: "一杯白开水w"
date: "2013-10-16T15:00:25Z"
tags: []
categories: ["编程语言"]
slug: "spring-ref-local-ref-bean"
draft: false
lastmod: "2013-10-16T15:00:25Z"
id: 1958876
url: "https://wlt2008-com.iteye.com/blog/1958876"
---

&lt; bean id = "userDAOProxy"

        class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" &gt;

        &lt; property name = "transactionManager" &gt;

            *&lt; ref *bean* = "transactionManager" /&gt;*

        &lt;/ property &gt;

        &lt; property name = "target" &gt;

            *&lt; ref *local* = "UserDAO" /&gt;*

        &lt;/ property &gt;

    &lt;/ bean &gt;

1 、用 local 属性指定目标 bean 可以利用 xml 解析器的能力在同一个文件中验证 xml id 引用 . 如果在同一个文件中没有匹 配的元素 ,xml 解析器就会产生一个 error, 所以如果目标 bean 在同一个 xml 文件中 , 那么用 local 形式是最好的选择 .

2 、可以这么说， &lt;ref bean 是寻找全局中的 bean; &lt;ref local 是寻找本 xml 文件中的 bean 

3 、 &lt;ref&gt; 提供了如下几方面的属性 :  
1)bean: 在当前 Spring XML 配置文件中，或者在同一 BeanFactory(ApplicationContext) 中的其他 JavaBean中 .  
2)local: 在当前 Spring XML 配置文件中 . 其依赖的 JavaBean 必须存在于当前 Spring XML 培植文件中 . 如果借助 于 Spring IDE, 则在编译期可以对其依赖的 JavaBean 进行验证。基于 local 方式，开发者能够使用到XML 本身提供 的优势，而进行验证。   
3)parent: 用于指定其依赖的父 JavaBean 定义。