spring classpath路径和Resource类
2016-06-22 16:00
501 查看
spring classpath路径问题和Resource类的使用。
类文件:
package com.yb.t;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
public class ClassA {
// 持有一个Resource属性
private Resource resource;
public ClassA() {
init();
}
private void init() {
try {
Resource r = new FileSystemResource("conf/test.txt");
print(r.getInputStream(), "--------FileSystemResource");
r = new ClassPathResource("conf/applicationContext-conf-test.xml");
print(r.getInputStream(), "--------ClassPathResource");
r = new PathResource("conf/test.txt");
print(r.getInputStream(), "--------PathResource");
r = new DefaultResourceLoader().getResource("classpath:conf/applicationContext-conf-test.xml");
print(r.getInputStream(), "--------DefaultResourceLoader");
} catch (IOException e) {
}
}
public void printContent() {
if (resource != null && resource.exists()) {
try {
print(resource.getInputStream(), "--------通过spring的xml配置文件读取resource");
} catch (IOException e) {
}
}
}
public void print(InputStream inputStream, String desc) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
System.out.println(desc);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
if (inputStream != null) {
inputStream.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void setResource(Resource resource) {
this.resource = resource;
}
}
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="classA" class="com.yb.t.ClassA">
<property name="resource">
<value>file:conf/test.txt</value>
</property>
</bean>
</beans>
附件:http://down.51cto.com/data/2367836
类文件:
package com.yb.t;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
public class ClassA {
// 持有一个Resource属性
private Resource resource;
public ClassA() {
init();
}
private void init() {
try {
Resource r = new FileSystemResource("conf/test.txt");
print(r.getInputStream(), "--------FileSystemResource");
r = new ClassPathResource("conf/applicationContext-conf-test.xml");
print(r.getInputStream(), "--------ClassPathResource");
r = new PathResource("conf/test.txt");
print(r.getInputStream(), "--------PathResource");
r = new DefaultResourceLoader().getResource("classpath:conf/applicationContext-conf-test.xml");
print(r.getInputStream(), "--------DefaultResourceLoader");
} catch (IOException e) {
}
}
public void printContent() {
if (resource != null && resource.exists()) {
try {
print(resource.getInputStream(), "--------通过spring的xml配置文件读取resource");
} catch (IOException e) {
}
}
}
public void print(InputStream inputStream, String desc) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
System.out.println(desc);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
if (inputStream != null) {
inputStream.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void setResource(Resource resource) {
this.resource = resource;
}
}
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="classA" class="com.yb.t.ClassA">
<property name="resource">
<value>file:conf/test.txt</value>
</property>
</bean>
</beans>
附件:http://down.51cto.com/data/2367836
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- 详解Java的MyBatis框架和Spring框架的整合运用
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao