java配置文件加载

本是古典 何须时尚 2022-05-29 05:46 420阅读 0赞
  1. package com.cn.util;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4. public class PropertyUtil {
  5. private static Properties properties;
  6. private static volatile PropertyUtil instance = null;
  7. public static PropertyUtil getInstance() {
  8. if (instance == null) {
  9. synchronized (PropertyUtil.class) {
  10. if (instance == null) {
  11. instance = new PropertyUtil();
  12. }
  13. }
  14. }
  15. return instance;
  16. }
  17. private PropertyUtil() {
  18. init();
  19. }
  20. private void init() {
  21. try {
  22. properties = new Properties();
  23. properties.load(getClass().getResourceAsStream("/conf.properties"));
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. public String getProperty(String key) {
  29. return properties.getProperty(key);
  30. }
  31. }

发表评论

表情:
评论列表 (有 0 条评论,420人围观)

还没有评论,来说两句吧...

相关阅读