暂时了解到两种方式
1 使用注解@value方式
2 采用jdk中的java.util.Properties
第二种方式写的util类
public class PropertyUtil
{
private static final Logger logger = Logger.getLogger(PropertyUtil.class);
private static Properties props;
static
{
loadProps();
}
synchronized static private void loadProps()
{
props = new Properties();
InputStream in = null;
try
{
// 第一种通过类加载器进行获取properties文件流
in = PropertyUtil.class.getClassLoader().getResourceAsStream("key.properties");
// 第二种,通过类进行获取properties文件流
// in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
props.load(in);
}
catch (FileNotFoundException e)
{
logger.error("key.properties文件未找到");
}
catch (IOException e)
{
logger.error("出现IOException");
}
finally
{
try
{
if (null != in)
{
in.close();
}
}
catch (IOException e)
{
logger.error("key.properties文件流关闭出现异常");
}
}
}
public static String getProperty(String key)
{
if (null == props)
{
loadProps();
}
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue)
{
if (null == props)
{
loadProps();
}
return props.getProperty(key, defaultValue);
}
}