利用 Spring 中的 Resource 读取文件和网络资源
package com.isoftstone.spring.beans;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class MyResource implements ApplicationContextAware, InitializingBean,
BeanFactoryAware, ResourceLoaderAware
{
private ApplicationContext ctx;
private BeanFactory beanFactory;
private ResourceLoader resourceLoader;
@Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException
{
this.ctx = ac;
}
@Override
public void afterPropertiesSet() throws Exception
{
MyResource bean = (MyResource) ctx.getBean("myResource", MyResource.class);
bean.sayHello();
System.out.println("------------------------------");
bean = (MyResource) beanFactory.getBean("myResource", MyResource.class);
bean.sayHello();
System.out.println("------------------------------");
ClassPathResource resource = (ClassPathResource) resourceLoader.getResource("classpath:com/isoftstone/spring/beans/MyResource.class");
BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
String line = reader.readLine();
for (; line != null; line = reader.readLine())
{
System.out.println(line);
}
System.out.println("--------------xxx----------------");
resource = (ClassPathResource) resourceLoader.getResource("classpath:com/isoftstone/spring/beans/MyResource.class");
InputStream is = resource.getInputStream();
byte[] buffer = new byte[1024];
int len = is.read(buffer);
for (; len > 0; len = is.read(buffer))
{
System.out.println(Arrays.toString(buffer));
}
System.out.println("---------------xxx---------------");
Resource urlr = resourceLoader.getResource("http://10.40.33.185/ci-master/home.do#");
BufferedReader reader1 = new BufferedReader(new InputStreamReader(urlr.getInputStream()));
String line1 = reader1.readLine();
for (; line1 != null; line1 = reader1.readLine())
{
System.out.println(new String(line1.getBytes(), "UTF-8"));
}
}
private final void sayHello()
{
System.out.println("Hello, World!");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException
{
this.beanFactory = beanFactory;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader)
{
this.resourceLoader = resourceLoader;
}
}
分享到:
相关推荐
利用spring resource读取配置文件。
主要给大家介绍了Spring Batch读取txt文件并写入数据库的方法,SpringBatch 是一个轻量级、全面的批处理框架。这里我们用它来实现文件的读取并将读取的结果作处理,处理之后再写入数据库中的功能。需要的朋友可以...
spring-resource-handling, Spring Framework 4.1资源处理示例 spring-资源处理 这里应用程序演示 Spring Framework 4.1中的新资源处理功能。 它最初是为在 4.1中讨论资源而开发的,在 SpringOne2GX 2014中讨论。...
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
Spring Boot中配置文件介绍及其使用教程所用到的Controller代码 Spring Boot中配置文件介绍及其使用教程所用到的Controller代码 Spring Boot中配置文件介绍及其使用教程所用到的Controller代码 Spring Boot中配置...
文件中进行了详细介绍说明这个配置文件是典型XML格式但是它既冗长又不实用对于需要定义大量 Springbeans大工程来说我们难以阅读和管理它 在这篇文章里对于SpringXML配置我将向你展示12种比较好实战其中些实战不仅...
本文档是很专业的 Spring动态加载配置文件,实现热部署方案。
spring框架的@Resource和@Componentspring框架的@Resource和@Component
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
spring使用resource注解的demo,依赖的库是spring core,建议通过myeclise添加。
NULL 博文链接:https://cdxs2.iteye.com/blog/1941621
NULL 博文链接:https://konghaibo.iteye.com/blog/430146
Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
spring配置文件实例
利用spring的依赖注入技术,实现打印机的功能,很适合初级学习spring
利用spring 生成Excel和PDF文件(内含项目所需要的jar包)
在src文件夹下创建xml文件,bean.xml或者applicationContext.xml都可以,在下载的Spring包中H:\spring-framework-4.1.0.RELEASE-dist\spring-framework-4.1.0.RELEASE\docs\spring-framework-reference\...
我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。
Spring3.0 配置文件中加载Properties文件的小例子