看着直接上传代码的挺好,还要申请什么公钥,先写完这4个spring demo 再换中方式。开整.
先定义一个接口,实现获得读取配置文件内容的方法。
___________________________________________________________________________________________
package com.michael.spring;
/**
* @ClassName: IHelloWorld
* @Description: TODO(接口中定义了获得内容的方法)
* @author huangbin 876301469@qq.com
* @date 2014-4-2 下午7:13:14
*
*/
public interface IHelloWorld {
/**
* @Title: getContent
* @Description: TODO(定义接口方法获取内容)
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getContent();
}
___________________________________________________________________________________________
FileHelloWorld实现接口的方法
___________________________________________________________________________________________
package com.michael.spring;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @ClassName: FileHelloWorld
* @Description: TODO(文件操作的helloworld)
* @author huangbin 876301469@qq.com
* @date 2014-4-2 下午8:33:50
*
*/
public class FileHelloWorld implements IHelloWorld{
private String propertyFileName ;
public FileHelloWorld(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: HelloWorldStr
* @Description: TODO(注入了接口)
* @author huangbin 876301469@qq.com
* @date 2014-4-2 下午7:04:26
*
*/
public class HelloWorldStr {
private IHelloWorld helloStr;
/**
* @param helloStr
* 构造方法注入了接口
*/
public HelloWorldStr(IHelloWorld helloStr) {
this.helloStr = helloStr;
}
/**
* @Title: getContent
* @Description: TODO(从配置文件中读取内容)
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getContent() {
return helloStr.getContent();
}
}
————————————————————————————————————————————————
客户端类调用方法测试结果
------------------------------------------------------------------------------------------------------------------------------------------------
package com.michael.spring;
/**
* @ClassName: HelloWorldClient
* @Description: TODO(客户端调用)
* @author huangbin 876301469@qq.com
* @date 2014-4-2 下午8:48:28
*
*/
public class HelloWorldClient {
public static void main(String[] args) {
FileHelloWorld hello = new FileHelloWorld("helloworld.properties");
HelloWorldStr helloStr = new HelloWorldStr(hello);
System.out.println(helloStr.getContent());
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------