apache commons (email,configuration)學習

很是實用的兩個工具類,學習成本超低。html

 

commons-email 見名之意,郵件發送的一個工具包java

用戶指南:spring

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

看了一會。。copy過來,本身封裝一下,絕對夠用了。數組

 

 

commons-configuration  配置文件讀取的工具類ide

用戶指南:工具

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

快速入門:ui

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

其實主要看這個接口,見名之意...

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);
}

...省略其餘
相關文章
相關標籤/搜索