非常实用的两个工具类,学习成本超低。
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);
}
...省略其他