apache commons (email,configuration)学习

非常实用的两个工具类,学习成本超低。

 

commons-email 见名之意,邮件发送的一个工具包

用户指南:

http://commons.apache.org/proper/commons-email/userguide.html

看了一会。。copy过来,自己封装一下,绝对够用了。

 

 

commons-configuration  配置文件读取的工具类

用户指南:

http://commons.apache.org/proper/commons-configuration/userguide/user_guide.html

快速入门:

http://commons.apache.org/proper/commons-configuration/userguide/quick_start.html

其实主要看这个接口,见名之意...

public interface Configuration
{

    public abstract Configuration subset(String s);

    public abstract boolean isEmpty();

    public abstract boolean containsKey(String s);

    public abstract void addProperty(String s, Object obj);

    public abstract void setProperty(String s, Object obj);

    public abstract void clearProperty(String s);

    public abstract void clear();

    public abstract Object getProperty(String s);

    public abstract Iterator getKeys(String s);

    public abstract Iterator getKeys();

    public abstract Properties getProperties(String s);

    public abstract boolean getBoolean(String s);

    public abstract boolean getBoolean(String s, boolean flag);

    public abstract Boolean getBoolean(String s, Boolean boolean1);

    public abstract byte getByte(String s);

    public abstract byte getByte(String s, byte byte0);

    public abstract Byte getByte(String s, Byte byte1);

    public abstract double getDouble(String s);

    public abstract double getDouble(String s, double d);

    public abstract Double getDouble(String s, Double double1);

    public abstract float getFloat(String s);

    public abstract float getFloat(String s, float f);

    public abstract Float getFloat(String s, Float float1);

    public abstract int getInt(String s);

    public abstract int getInt(String s, int i);

    public abstract Integer getInteger(String s, Integer integer);

    public abstract long getLong(String s);

    public abstract long getLong(String s, long l);

    public abstract Long getLong(String s, Long long1);

    public abstract short getShort(String s);

    public abstract short getShort(String s, short word0);

    public abstract Short getShort(String s, Short short1);

    public abstract BigDecimal getBigDecimal(String s);

    public abstract BigDecimal getBigDecimal(String s, BigDecimal bigdecimal);

    public abstract BigInteger getBigInteger(String s);

    public abstract BigInteger getBigInteger(String s, BigInteger biginteger);

    public abstract String getString(String s);

    public abstract String getString(String s, String s1);

    public abstract String[] getStringArray(String s);

    public abstract List getList(String s);

    public abstract List getList(String s, List list);
}

描述一个我在项目中真实使用的案例:关于configraction

我的需求是读取resource目录下的config.properties

首先在spring配置文件中注入

!-- 配置文件读取 -->
<bean id="proconfig" class="org.apache.commons.configuration.PropertiesConfiguration">
	<constructor-arg index="0" value="config.properties"/>
</bean>

在统一的工具类中获取配置的对象实例

/** Config.properties */
private static PropertiesConfiguration proconfig = SpringUtil.getBean("proconfig",PropertiesConfiguration.class);

包装成静态方法

/***
 * 获取配置文件值数组形式
 * @param key 配置文件中的key
* @return
*/
public static String[] getStringArrayProperty(String key){
	return proconfig.getStringArray(key);
}
	
/***
 * 获取配置文件值
 * @param key 配置文件中的key
 * @return
 */
public static Integer getIntegerProperty(String key){
	return proconfig.getInteger(key,null);
}

...省略其他

 

转载于:https://my.oschina.net/lis1314/blog/702457

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值