因為項目需要需要將配置文件中的鍵值對讀出放到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(); } }