您的位置:首页 > 编程语言 > Java开发

spring学习前奏001

2014-04-02 19:49 302 查看
再次学习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。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 读取配置文件