当前位置: 代码迷 >> J2EE >> spring如何获得properties文件里面的数据
  详细解决方案

spring如何获得properties文件里面的数据

热度:14   发布时间:2016-04-22 01:20:45.0
spring怎么获得properties文件里面的数据?
我写了一个解密类读取properties里面的数据库帐号和密码然后解密,为什么在需要操作数据库的时候,拿的还是我加密的密文,而没经过解密的方法了?有什么办法可以让spring去拿的是解密后的帐号密码,而不是密文

------解决方案--------------------
提供个比较使用的读取某种命名类型的properties文件都Map中:
使用:PropertiesUtils.getAll();
Java code
package com.commons;import java.io.IOException;import java.io.InputStreamReader;import java.io.Reader;import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;public class PropertiesUtils {    private static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();    private static final String DEFAULT_RESOURCE_PATTERN = "classpath:*-tag.properties";    private static final Map<Object, Object> all = new HashMap<Object, Object>();    public static Map<Object, Object> getAll() {    return all;    }    static {    try {        Resource[] resources = resourcePatternResolver            .getResources(DEFAULT_RESOURCE_PATTERN);        if (resources != null) {        for (Resource r : resources) {            Reader in = new InputStreamReader(r.getInputStream(),                "UTF-8");            Properties p = new Properties();            p.load(in);            all.putAll(p);            in.close();        }        }    } catch (IOException e) {        e.printStackTrace();    }    }}
  相关解决方案