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

...省略其余
相关文章
相关标签/搜索