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. springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能

    转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包     数据库表 数据库表就不用教大家了,一张表,很简 ...

  2. DAY11-MYSQL多表查询

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name ) ); create table employ ...

  3. Mediaplayer

    Mediaplayer报错 prepareAsync called in state 1     是因为在setDataSource之前调用了prepare.因为setDataSource放到了线程里 ...

  4. Tomcat服务器简介

  5. close、flush、read、readline、seek、tell、truncate、write的使用

    1.close关闭文件 f1= open("ha.log","r+",encoding="utf-8") data = f1.read() ...

  6. SpringMVC接收对象数组参数进行封装

    前台代码:注意.contentType : "application/json; charset=utf-8",必须要设置,只有这样SpringMVC才认识这个json数组参数 f ...

  7. cocos2d-js 骨骼动画 3.10

    近期使用了cocos动画中的骨骼动画,这里记录使用的两种方式(3.10版): 一.cocos自带的动画编辑器导出的动画 ccs.armatureDataManager.addArmatureFileI ...

  8. 205. Isomorphic Strings两个数组变形记,是否符合规则

    [抄题]: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  9. Windows系统 安装 CMake

    Windows系统 安装 CMake 我们的电脑系统:Windows 10 64位 安装的CMake 版本:cmake-3.6.1-win64-x64(目前最新) 下载 在CMake官网下载:cmak ...

  10. netty源码阅读之UnpooledByteBufAllocator

    使用IDEA阅读源码Navigate下面的工具是个好东西 .可以帮助分析类的结构等 ByteBufAllocator主要用来生成三种ByteBuf :HeadBuffer,DirectBuffer,C ...