再次学习spring,万事开头难,那就找个pdf吧,下载了一个《精通spring》的电子书,能不能精通先不管,每次写简单的代码,也能有新的收获,开始贴程序。
根据文档,写了三个类,helloworld 总是永恒传奇的开始,呵呵。
publicclass HelloWorld {
/**
* @Title: getContent
* @Description: TODO(读取配置文件并且返回读取的属性值)
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getContent(){
HelloWorldStr hello = new HelloWorldStr("helloworld.properties");
String helloContent = hello.getContent();
return helloContent;
}
}
——————————————————————————————————————————————————
/**
* @ClassName: HelloWorldStr
* @Description: TODO(这里用一句话描述这个类的作用)
* @author huangbin 876301469@qq.com
* @date 2014-4-2 下午7:04:26
*
*/
public class HelloWorldStr {
privateString propertyFileName ;
publicHelloWorldStr(String propertyFileName) {
this.propertyFileName= propertyFileName;
}
/**
* @Title: getContent
* @Description: TODO(从配置文件中读取内容)
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getContent() {
String helloWorld = "";
Properties property = new Properties();
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream(propertyFileName);
try {
property.load(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
helloWorld = property.getProperty("helloworld");
return helloWorld;
}
}
——————————————————————————————————————————————————
/**
* @ClassName: HelloWorldClient
* @Description: TODO(客户端调用)
* @author huangbin 876301469@qq.com
* @date 2014-4-2 下午7:28:02
*
*/
public class HelloWorldClient {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
System.out.println(hello.getContent());
}
}
——————————————————————————————————————————————————
功能很简单,但是学会了从配置文件中读取配置属性,不必自诩几年编程经验,细节总是决定成败的,每次都有收获,下一篇将重构该功能会引出spring。