spring学习前奏002

看着直接上传代码的挺好,还要申请什么公钥,先写完这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());
 }

}

 

-------------------------------------------------------------------------------------------------------------------------------------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值