spring mvc 读取.properties文件

来源:互联网 发布:linux udp客户端程序 编辑:程序博客网 时间:2024/06/05 15:22

暂时了解到两种方式

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


0 0
原创粉丝点击