properties文件读取


可以使用key和文件名的组合获取value



工具类1

package dsf.atest.test;



import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


public class PropertiesUtil {
private static ResourceLoader loader = ResourceLoader.getInstance();  
    private static ConcurrentMap<String, String> configMap = new ConcurrentHashMap<String, String>();  
    private static final String DEFAULT_CONFIG_FILE = "test.properties";  
  
    private static Properties prop = null;  
  
    public static String getStringByKey(String key, String propName) {  
        try {  
            prop = loader.getPropFromProperties(propName);  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
        key = key.trim();  
        if (!configMap.containsKey(key)) {  
            if (prop.getProperty(key) != null) {  
                configMap.put(key, prop.getProperty(key));  
            }  
        }  
        return configMap.get(key);  
    }  
  
    public static String getStringByKey(String key) {  
        return getStringByKey(key, DEFAULT_CONFIG_FILE);  
    }  
  
    public static Properties getProperties() {  
        try {  
            return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    } 

}


工具类2

package dsf.atest.test;


import java.io.File;  
import java.io.FileInputStream;  
import java.util.HashMap;  
import java.util.Map;  
import java.util.Properties;  
  
public final class ResourceLoader {  
  
    private static ResourceLoader loader = new ResourceLoader();  
    private static Map<String, Properties> loaderMap = new HashMap<String, Properties>();  
  
    private ResourceLoader() {  
    }  
  
    public static ResourceLoader getInstance() {  
        return loader;  
    }  
      
    public Properties getPropFromProperties(String fileName) throws Exception {  
          
        Properties prop = loaderMap.get(fileName);  
        if (prop != null) {  
            return prop;  
        }  
        String filePath = null;  
        String configPath = System.getProperty("configurePath");  
  
        if (configPath == null) {  
            filePath = this.getClass().getClassLoader().getResource(fileName).getPath();  
        } else {  
            filePath = configPath + "/" + fileName;  
        }  
        prop = new Properties();  
        prop.load(new FileInputStream(new File(filePath)));  
  
        loaderMap.put(fileName, prop);  
        return prop;  
    }  
}  


获取实例


public static void main(String[] args) {
String name = PropertiesUtil.getStringByKey("city", "wordBook.properties");
System.out.println(name.split(",")[name.split(",").length-1]);

}



验证通过,何以运行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架提供了一种便捷的方式来读取properties文件中的配置信息。在Spring中,我们可以使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer来加载properties文件PropertyPlaceholderConfigurer是在Spring 3.1之前版本中使用的类,用于从classpath或指定位置加载properties文件。它可以将properties文件中的键值对替换为Spring配置文件中的占位符。通过将PropertyPlaceholderConfigurer配置为Bean的一部分,我们可以在XML配置文件中使用占位符来引用properties文件中的值。例如,我们可以在XML中配置一个DataSource Bean,并通过占位符引用properties文件中的数据库连接信息。 PropertySourcesPlaceholderConfigurer是从Spring 3.1版本引入的新类,它可以更方便地处理properties文件。与PropertyPlaceholderConfigurer不同,PropertySourcesPlaceholderConfigurer允许我们使用@Value注解来直接注入properties文件中的属性。我们可以在Java配置类或XML配置文件中配置PropertySourcesPlaceholderConfigurer,并在需要的地方使用@Value注解来获取properties文件中的属性值。 无论我们使用哪种方式来读取properties文件,我们都需要确保将其配置为Spring容器的一部分,并以适当的方式加载它。通常,我们会在Spring配置文件中添加一个bean来加载properties文件,并将其配置为其他bean的属性或参数的值。 总结起来,Spring提供了两种主要的方法来读取properties文件:使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer。这些类可以让我们轻松地将properties文件中的配置值引入到Spring容器中,并在我们的应用程序中使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值