Spring实战中ResourceLoaderAware如何实现资源加载示例?

2026-05-28 07:381阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计406个文字,预计阅读时间需要2分钟。

Spring实战中ResourceLoaderAware如何实现资源加载示例?

原文示例:本文实例讲述了Spring实战中ResourceLoaderAware加载资源用法。分享给广大读者,仅供参考。具体如下:

一、配置文件.xmlbeans xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns=http://www.springframework.org/schema/beans>

改写后:本文以实例说明Spring中ResourceLoaderAware如何加载资源。供大家参考,内容如下:

一、XML配置

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

一 配置文件

<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="www.springframework.org/schema/beans" xsi:schemaLocation="www.springframework.org/schema/beans 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

Spring实战中ResourceLoaderAware如何实现资源加载示例?

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

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

本文共计406个文字,预计阅读时间需要2分钟。

Spring实战中ResourceLoaderAware如何实现资源加载示例?

原文示例:本文实例讲述了Spring实战中ResourceLoaderAware加载资源用法。分享给广大读者,仅供参考。具体如下:

一、配置文件.xmlbeans xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns=http://www.springframework.org/schema/beans>

改写后:本文以实例说明Spring中ResourceLoaderAware如何加载资源。供大家参考,内容如下:

一、XML配置

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

一 配置文件

<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="www.springframework.org/schema/beans" xsi:schemaLocation="www.springframework.org/schema/beans 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

Spring实战中ResourceLoaderAware如何实现资源加载示例?

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

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