Spring----. ref的用法
ref元素是用在property中,来设置需要引用的容器管理的其它Bean。
它的用法:<ref bean|local|parent="someBean">,这里主要分析一下这三个参数的作用。
这次先看实例,再进行讲解。
· 先建立一个包:javamxj.spring.basic.ref ,然后把以下5个文件放在这个包下。
HelloBean.java
|
package javamxj.spring.basic.ref;
public class HelloBean { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } }
|
HelloDate.java
|
package javamxj.spring.basic.ref;
import java.util.Date; public class HelloDate { private Date date; private HelloBean hb; public void setDate(Date date) { this.date = date; } public void setHb(HelloBean hb) { this.hb = hb; } public void sayHello() { System.out.println(hb.getHello() + " " + date.toLocaleString()); } } |
beans.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">
<property name="hello" value="Hello! Child Bean." />
</bean>
<bean id="dateBean" name="#date" class="java.util.Date"/>
<bean id="hd1" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref bean="helloBeanParent"/>
</property>
<property name="date">
<ref bean="#date"/>
<!--<ref bean="dateBean"/>-->
</property>
</bean>
<bean id="hd2" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref local="helloBean"/>
</property>
<property name="date">
<ref local="dateBean"/>
</property>
</bean>
<bean id="hd3" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref parent="helloBean"/>
</property>
<property name="date">
<bean class="java.util.Date"/>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">
<property name="hello" value="Hello! Child Bean." />
</bean>
<bean id="dateBean" name="#date" class="java.util.Date"/>
<bean id="hd1" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref bean="helloBeanParent"/>
</property>
<property name="date">
<ref bean="#date"/>
<!--<ref bean="dateBean"/>-->
</property>
</bean>
<bean id="hd2" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref local="helloBean"/>
</property>
<property name="date">
<ref local="dateBean"/>
</property>
</bean>
<bean id="hd3" class="javamxj.spring.basic.ref.HelloDate">
<property name="hb">
<ref parent="helloBean"/>
</property>
<property name="date">
<bean class="java.util.Date"/>
</property>
</bean>
</beans>
parent.xml
|
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean"> <property name="hello" value="Hello! Javamxj." /> </bean> <bean id="helloBeanParent" class="javamxj.spring.basic.ref.HelloBean"> <property name="hello" value="Hello! Parent Bean." /> </bean> </beans> |
Main.java
|
package javamxj.spring.basic.ref;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class Main { public static void main(String[] args) { BeanFactory parent = new XmlBeanFactory(new ClassPathResource( "javamxj/spring/basic/ref/parent.xml")); BeanFactory child = new XmlBeanFactory(new ClassPathResource( "javamxj/spring/basic/ref/beans.xml"), parent); HelloDate hd1 = (HelloDate) child.getBean("hd1"); HelloDate hd2 = (HelloDate) child.getBean("hd2"); HelloDate hd3 = (HelloDate) child.getBean("hd3"); hd1.sayHello(); hd2.sayHello(); hd3.sayHello(); } } |
·运行Main.java,输出结果如下:
Hello! Parent Bean. 2005-8-10 22:25:56
Hello! Child Bean. 2005-8-10 22:25:56
Hello! Javamxj. 2005-8-10 22:25:56
Hello! Child Bean. 2005-8-10 22:25:56
Hello! Javamxj. 2005-8-10 22:25:56
OK!这里主要分析beans.xml、Main.java这两个文件。对于Main.java要注意的是如何加载“parent”的,重点看看beans.xml中ref元素的用法。
首先定义两个bean:helloBean、dateBean,分别指向HelloBean类和Date类。然后定义了hd1、hd2、hd3等三个bean,都指向HelloDate类。
·hd1:
property标签中的“helloBeanParent”并不存在于beans.xml中,而是在parent.xml中,使用<ref bean="someBean">可以从其它bean配置文件中寻找需要加载的目标bean。bean属性的值可以同目标bean的id属性相同,也可以同它的name属性中任意的一个值相同。
·hd2:
property标签中的“helloBean”如果不存在于beans.xml中,则XML解析器会提示错误。<ref local="someBean">只能这个bean配置文件中寻找需要加载的目标bean,而且local属性值必须同目标bean的id属性相同。
·hd3:
property标签中的“helloBean”同时存在于beans.xml和parent.xml中,使用<ref bean="someBean">则会优先从beans.xml中寻找需要加载的目标bean,如果需要从parent.xml中加载目标bean,则需使用<ref parent="someBean">。在设置date时,使用的是内联bean,这时可以不要任何id或name定义。
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了