spring中ref的用法
时间: 2023-09-13 18:10:23 浏览: 52
在Spring中,ref元素是用在property中,用于设置需要引用的容器管理的其他Bean。它的用法是通过<ref bean|local|parent="someBean">来指定引用的Bean。其中,bean表示引用同一个容器中的Bean,local表示引用同一个容器中的局部Bean,parent表示引用父容器中的Bean。\[1\]
举个例子来说明,假设我们有两个Bean,HelloBean和HelloDate。HelloDate中有一个HelloBean类型的属性hb,我们可以通过ref来设置hb属性引用HelloBean。具体的配置如下:
```xml
<bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">
<property name="hello" value="Hello World"/>
</bean>
<bean id="helloDate" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref bean="helloBean"/>
</property>
</bean>
```
在上面的配置中,我们通过<ref bean="helloBean"/>来设置helloDate的hb属性引用helloBean。这样,在使用helloDate的时候,就可以通过hb属性来访问helloBean的方法和属性了。\[1\]
#### 引用[.reference_title]
- *1* [快速上手Spring--7. ref的用法](https://blog.csdn.net/javamxj/article/details/451455)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]