根据对您的问题的评论:
我有一个包含一些键值对的属性文件,这是整个应用程序所需要的,这就是为什么我在考虑单例类。
此类将从文件中加载属性并将其保留,您可以在应用程序中的任何位置使用它
不要使用单例。您显然不需要一次性的 延迟 初始化(这就是单例的全部内容)。您需要一次性 直接 初始化。只需使其静态并加载到静态初始化程序中即可。
例如
public class Config { private static final Properties PROPERTIES = new Properties(); static { try { PROPERTIES.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties")); } catch (IOException e) { throw new ExceptionInInitializerError("Loading config file failed.", e); } } public static String getProperty(String key) { return PROPERTIES.getProperty(key); } // ...}


