Spring实战之ResourceLoaderAware加载资源用法示例

发布时间:2020-09-04 20:21:01 作者:cakincqm
来源:脚本之家 阅读:364

本文实例讲述了Spring实战之ResourceLoaderAware加载资源用法。分享给大家供大家参考,具体如下:

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
   <bean id="test" class="org.crazyit.app.service.TestBean"/>
</beans>

二 Bean

package org.crazyit.app.service;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
public class TestBean implements ResourceLoaderAware
{
  private ResourceLoader rd;
  // 实现ResourceLoaderAware接口必须实现的方法
  // 如果把该Bean部署在Spring容器中,该方法将会由Spring容器负责调用
  // Spring容器调用该方法时,Spring会将自身作为参数传给该方法
  public void setResourceLoader(ResourceLoader resourceLoader)
  {
    this.rd = resourceLoader;
  }
  // 返回ResourceLoader对象的引用
  public ResourceLoader getResourceLoader()
  {
    return rd;
  }
}

三 测试类

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.springframework.core.io.*;
import org.dom4j.io.XPPReader;
import org.dom4j.Document;
import org.dom4j.Element;
import java.util.*;
import org.crazyit.app.service.*;
public class SpringTest
{
  public static void main(String[] args)
  {
    // 创建ApplicationContext容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 获取容器中名为test的Bean实例
    TestBean tb = ctx.getBean("test" , TestBean.class);
    // 通过tb实例来获取ResourceLoader对象
    ResourceLoader rl = tb.getResourceLoader();
    // 判断程序获得ResourceLoader和容器是否相同
    System.out.println(rl == ctx);
  }
}

四 测试结果

true

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

推荐阅读:
  1. Spring实战之缓存使用condition操作示例
  2. Spring实战之缓存使用key操作示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring resourceloaderaware 加载资源

上一篇:C/C++读写文本文件、二进制文件的方法

下一篇:Python通过属性手段实现只允许调用一次的示例讲解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》