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>
 
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
 
 
   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定义。

Spring----. ref的用法的更多相关文章

  1. Spring中@Async用法详解及简单实例

    Spring中@Async用法 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类 ...

  2. SpringMVC +mybatis+spring 结合easyui用法及常见问题总结

    SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...

  3. vue里ref ($refs)用法

    ref 有三种用法: 1.ref 加在普通的元素上,用this.ref.name 获取到的是dom元素 2.ref 加在子组件上,用this.ref.name 获取到的是组件实例,可以使用组件的所有方 ...

  4. (转)Spring中@Async用法总结

     原文:http://blog.csdn.net/blueheart20/article/details/44648667 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的: ...

  5. Spring中@Async用法总结

    引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3. ...

  6. spring AOP的用法

    AOP,面向切面编程,它能把与核心业务逻辑无关的散落在各处并且重复的代码给封装起来,降低了模块之间的耦合度,便于维护.具体的应用场景有:日志,权限和事务管理这些方面.可以通过一张图来理解下: Spri ...

  7. Spring中@Value用法收集

    一.配置方式 @Value需要参数,这里参数可以是两种形式: @Value("#{configProperties['t1.msgname']}") 或者 @Value(" ...

  8. Spring.Net简单用法

    Spring.Net其实就是抽象工厂,只不过更加灵活强大,性能上并没有明显的区别. 它帮我们实现了控制反转. 其有两种依赖注入方式. 第一:属性注入 第二:构造函数注入 首先,我们去  Spring. ...

  9. 7 -- Spring的基本用法 -- 5...

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  10. 7 -- Spring的基本用法 -- 3...

    7.3 Spring 的核心机制 : 依赖注入 Spring 框架的核心功能有两个. Spring容器作为超级大工厂,负责创建.管理所有的Java对象,这些Java对象被称为Bean. Spring容 ...

随机推荐

  1. Shell编程进阶 1.8 for循环

    产生序列的命令 seq 1 2 3 4 5 6 7 8 9 10 seq 1 3 5 7 9  (从1开始增加2显示这个数字,到10结束) seq - 10 8 6 4 2 seq - 10 9 8 ...

  2. MyBatis总结四:配置文件xml详解

    XML 映射配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 ...

  3. str_place()替换函数

    str_replace() 函数使用一个字符串替换字符串中的另一些字符. 注释:该函数对大小写敏感.请使用 str_ireplace() 执行对大小写不敏感的搜索. echo str_replace( ...

  4. JS jquery ajax 已看1 有用

    4.form中的input可以设置为readonly和disable,请问2者有什么区别? readonly不可编辑,但可以选择和复制:值可以传递到后台 disabled不能编辑,不能复制,不能选择: ...

  5. Inheritance with EF Code First: Part 2 – Table per Type (TPT)

    In the previous blog post you saw that there are three different approaches to representing an inher ...

  6. leetCode编程题

    已知链表1->2->3->4,先需要删除3这个节点,请完成函数.注意,这里只给定要删除的节点3,并不知道3之前的节点是哪个,以及整个链表是什么节点. void delete(List ...

  7. 前端学习01-06URL

    URL(Uniform Resource Locator) 统一资源定位 URL的基本组成:协议,主机名,端口号,资源名 例如: http://www.sina.com:80/index.html 相 ...

  8. ConcurrentHashMap的putIfAbsent

    这个方法在key不存在的时候加入一个值,如果key存在就不放入,等价: if (!map.containsKey(key)) return map.put(key, value); else retu ...

  9. POJ 1795 DNA Laboratory (贪心+状压DP)

    题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...

  10. 自定义MVC的Helper扩展方法 转 Insus.NET

    记得在开发ASP.NET时候,也经常性使用C#可以写自己义的扩展方法,如: http://www.cnblogs.com/insus/p/3154363.html 或http://www.cnblog ...