Spring实战之ResourceLoaderAware加载资源用法示例
更新时间:2020年01月02日 09:07:49 作者:cakincqm
这篇文章主要介绍了Spring实战之ResourceLoaderAware加载资源用法,结合实例形式分析了spring使用ResourceLoaderAware加载资源相关配置与操作技巧,需要的朋友可以参考下
本文实例讲述了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程序设计有所帮助。
相关文章
springboot使用Thymeleaf报错常见的几种解决方案
这篇文章主要介绍了springboot使用Thymeleaf报错常见的几种解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-11-11
详解springmvc之json数据交互controller方法返回值为简单类型
这篇文章主要介绍了springmvc之json数据交互controller方法返回值为简单类型,非常具有实用价值,需要的朋友可以参考下2017-05-05
解决springboot application.properties server.port配置问题
这篇文章主要介绍了解决springboot application.properties server.port配置问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-08-08
java实现服务器文件打包zip并下载的示例(边打包边下载)
这篇文章主要介绍了java实现服务器文件打包zip并下载的示例,使用该方法,可以即时打包文件,一边打包一边传输,不使用任何的缓存,让用户零等待,需要的朋友可以参考下2014-04-04


最新评论