spring中的ref标签
来源:互联网 发布:python linix开发工具 编辑:程序博客网 时间:2024/06/10 00:22
快速上手Spring--7. ref的用法
发布时间:2006.03.17 09:51 来源:csdn 作者:javamxj
这篇文章来谈谈《Spring Framework 开发参考手册》的3.3.2小节中的ref的用法。
ref元素是用在property中,来设置需要引用的容器管理的其它Bean。
它的用法:,这里主要分析一下这三个参数的作用。
这次先看实例,再进行讲解。
· 先建立一个包: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;
}
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());
}
}
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"/>
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"/>
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>
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;
importorg.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) {
BeanFactory parent = new XmlBeanFactory(newClassPathResource(
"javamxj/spring/basic/ref/parent.xml"));
BeanFactory child = new XmlBeanFactory(newClassPathResource(
"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();
}
}
import org.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) {
BeanFactory parent = new XmlBeanFactory(newClassPathResource(
"javamxj/spring/basic/ref/parent.xml"));
BeanFactory child = new XmlBeanFactory(newClassPathResource(
"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中,使用可以从其它bean配置文件中寻找需要加载的目标bean。bean属性的值可以同目标bean的id属性相同,也可以同它的name属性中任意的一个值相同。
·hd2:
property标签中的“helloBean”如果不存在于beans.xml中,则XML解析器会提示错误。只能这个bean配置文件中寻找需要加载的目标bean,而且local属性值必须同目标bean的id属性相同。
·hd3:
property标签中的“helloBean”同时存在于beans.xml和parent.xml中,使用则会优先从beans.xml中寻找需要加载的目标bean,如果需要从parent.xml中加载目标bean,则需使用。在设置date时,使用的是内联bean,这时可以不要任何id或name定义。
好了,就说这么多了。
- spring中的ref标签
- Spring中的ref标签的使用
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
- Spring property标签ref属性和ref标签区别
- Spring中引用标签ref和属性ref
- Spring中的idref和ref
- spring配置文件中ref 和idref标签
- Spring idref标签和ref的区别
- spring 4.0 不再支持ref local标签
- spring中的idref和ref的区别
- Spring中的p标签
- Spring 中的p标签
- Spring 中的p标签
- Spring 中的p标签
- Spring中的p标签
- Spring中的p标签
- spring配置中的p标签
- FSO组件之文件夹操作
- ET199加密方案——文件MD5校验
- SQL 读取循环读取文件夹里面所有指定内容
- 搜索引擎爬虫的基本需求和考核标准
- 哈佛有一个著名的理论
- spring中的ref标签
- TCP/IP 详解 卷1:协议
- 标准C函数的一些字符串与整型、浮点型的转换
- Wince 系统内存
- 使用ASP更改 NTFS 分区中 文件夹的 读写等权限
- UNIX 高手的重大秘密
- SSH2+Crystal Report,重复行
- 非常有用的网站-职场
- 关于const char *的使用方法