使用Spring中的Resource接口隔离对文件系统的依赖
2007-12-29 14:06
447 查看
在项目中,经常要用到读系统文件.在项目的遗留代码中,都是在系统启动是传入一个APP_HOME,然后根据相对路径去读文件.这样做的缺点是比较难测试,而且自动化的测试更难.
比如说有这样一个类Server,要根据server.properties来初始化,一开始的代码是这样的:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author sting
*/
public class Server {
private static final String FILE = "conf" + File.separator + "server.properties";
public void initial() throws IOException {
FileInputStream in = new FileInputStream(System.getProperty("APP_HOME") + File.separator + FILE);
Properties properties = new Properties();
properties.load(in);
// initial
}
}
文件路径和文件名都是hard code,很难测试. 我们首先把initial()重构一下,代码如下:
public void initial(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
// initial
}
至少,测试时,我们可以传进来自己的InputStream,也可以方便的时候测试用的server.properties,或者干脆使用内联的文件,代码如下:
class ServerTest extends TestCase {
private Server server;
public void setUp() throws Exception {
this.server = new Server();
}
public void testInitial() throws Exception {
String serverProperties = "port=8080/n" +
"run_mode=normal";
InputStream in = new ByteArrayInputStream(serverProperties.getBytes());
this.server.initial(in);
// assert
}
}
但是,在实际工作的代码中,文件名和路径依然要hard code进代码中.这时,我们可以使用spring中的Resource接口来进一步改进我们的代码.
public class Server {
private Resource resource;
public void setResource(Resource r) {
this.resource = r;
}
public void initial() throws IOException {
Properties properties = new Properties();
properties.load(this.resource.getInputStream());
// initial
}
}
再加一段spring的配置文件:
<beans>
<bean id="server" class="Server">
<property name="resource" value="classpath:server.properties"/>
</bean>
</beans>
这样,Server的代码完全与文件的具体路径和文件名无关,仅仅用配置文件就可以指定,表达更清楚,也更易于测试.
当然,仅限于已经使用spring的项目.
点击查看原文
比如说有这样一个类Server,要根据server.properties来初始化,一开始的代码是这样的:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author sting
*/
public class Server {
private static final String FILE = "conf" + File.separator + "server.properties";
public void initial() throws IOException {
FileInputStream in = new FileInputStream(System.getProperty("APP_HOME") + File.separator + FILE);
Properties properties = new Properties();
properties.load(in);
// initial
}
}
文件路径和文件名都是hard code,很难测试. 我们首先把initial()重构一下,代码如下:
public void initial(InputStream in) throws IOException {
Properties properties = new Properties();
properties.load(in);
// initial
}
至少,测试时,我们可以传进来自己的InputStream,也可以方便的时候测试用的server.properties,或者干脆使用内联的文件,代码如下:
class ServerTest extends TestCase {
private Server server;
public void setUp() throws Exception {
this.server = new Server();
}
public void testInitial() throws Exception {
String serverProperties = "port=8080/n" +
"run_mode=normal";
InputStream in = new ByteArrayInputStream(serverProperties.getBytes());
this.server.initial(in);
// assert
}
}
但是,在实际工作的代码中,文件名和路径依然要hard code进代码中.这时,我们可以使用spring中的Resource接口来进一步改进我们的代码.
public class Server {
private Resource resource;
public void setResource(Resource r) {
this.resource = r;
}
public void initial() throws IOException {
Properties properties = new Properties();
properties.load(this.resource.getInputStream());
// initial
}
}
再加一段spring的配置文件:
<beans>
<bean id="server" class="Server">
<property name="resource" value="classpath:server.properties"/>
</bean>
</beans>
这样,Server的代码完全与文件的具体路径和文件名无关,仅仅用配置文件就可以指定,表达更清楚,也更易于测试.
当然,仅限于已经使用spring的项目.
点击查看原文
相关文章推荐
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- Spring依赖注入:在spring配置文件中使用properties配置文件
- Spring.net 控制反转(IOC)依赖注入(DI)的使用 以及Config文件的配置
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- linux 使用系统接口实现复制文件
- Spring 从文件系统读取配置文件, 使用@PropertySource
- 8.2.3: Spring的零配置支持---使用@Resource配置依赖
- Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决
- 8 -- 深入使用Spring -- 2...3 使用@Resource配置依赖
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- 有关spring的依赖注入和@resource注解的使用
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- 8 -- 深入使用Spring -- 3...2 ResouceLoader 接口和 ResourceLoaderAware 接口
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
- 使用Java函数接口及lambda表达式隔离和模拟外部依赖更容易滴单测
- STM32学习笔记之fatfs文件系统接口函数使用