因为项目需要需要将配置文件中的键值对读出放到map中
格式为:
001=123456789
Appcontext.xml中添加配置:
<bean id="loadKeyFromProperties" class="com.;landau.init.LoadKeyFormProperties"> <property name="keyFileResource"> <value>classpath:keys.properties</value> </property> </bean>
java代码:
public class LoadKeyFormProperties implements InitializingBean { private Resource keyFileResource; private static Map<String, String> map = new HashMap<String, String>(); protected static volatile boolean initialized = false; public static Map<String, String> getKey() { return map; } public void setKeyFileResource(Resource keyFileResource) { this.keyFileResource = keyFileResource; } /** * 将键值对取到集合内 */ private void loadKeyFormProperties() { if (initialized) { return; } InputStream is = null; try { is = keyFileResource.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = null; while ((str = br.readLine()) != null) { String[] data = str.split("="); map.put(data[0], data[1]); } initialized = true; } catch (Exception e) { } finally { is.close(); } } @Override public void afterPropertiesSet() throws Exception { loadKeyFormProperties(); } }
时间: 2024-10-07 17:48:11