在静态初始化程序块中加载java属性 [英] Load java properties inside static initializer block

查看:96
本文介绍了在静态初始化程序块中加载java属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态util类,它对位敏感数据执行一些字符串操作。
在使用此类之前,我需要使用值(例如usernames / password)初始化某些静态变量,我希望将其存储在 .properties 文件中。

I have a static util class that does some string manipulation on a bit sensitive data. Prior to use of this class I need to initialize certain static variables with values, such as usernames/password, that I prefer to store in a .properties file.

我不太熟悉如何在Java中加载 .properties 文件,特别是在* Spring DI之外*容器。
任何人都可以帮我解决一下如何做到这一点?

I am not very familiar with how loading of .properties file work in Java, especially outside of *Spring DI *container. Anyone can give me a hand/insight on how this can be done?

谢谢!

添加: .properties 文件精确位置未知,但它将在类路径上。排序类似 classpath:/my/folder/name/myproperties.propeties

Addition: .properties file precise location is unknown, but it will be on the classpath. Sorta like classpath:/my/folder/name/myproperties.propeties

推荐答案

首先,获取要加载属性的 InputStream 。这可能来自多个地点,包括一些最有可能的地点:

First, obtain an InputStream from which the properties are to be loaded. This can come from a number of locations, including some of the most likely:


  • A FileInputStream ,使用硬编码或通过 system property。名称可以是相对的(对于Java进程的当前工作目录)或绝对名称。 / li>
  • 通过调用 Class (相对于类文件)或 ClassLoader (相对于类路径的根目录)。请注意,如果缺少资源,这些方法将返回null,而不是引发异常。

  • A URL ,与文件名一样,可以是硬编码或通过系统属性指定。

  • A FileInputStream, created with a file name that is hard-coded or specified via a system property. The name could be relative (to the current working directory of the Java process) or absolute.
  • A resource file (a file on the classpath), obtained through a call to getResourceAsStream on the Class (relative to the class file) or ClassLoader (relative to the root of the class path). Note that these methods return null if the resource is missing, instead of raising an exception.
  • A URL, which, like a file name, could be hard-coded or specified via a system property.

然后创建一个新的属性 object,并将 InputStream 传递给它的 load() 方法。无论是否有任何例外,请务必关闭流。

Then create a new Properties object, and pass the InputStream to its load() method. Be sure to close the stream, regardless of any exceptions.

在类初始化程序中,检查的异常如 IOException 必须被处理。可以抛出未经检查的异常,这将阻止该类被初始化。反过来,这通常会阻止您的应用程序运行。在许多应用程序中,可能需要使用默认属性,或者回退到另一个配置源,例如提示在交互式上下文中使用。

In a class initializer, checked exceptions like IOException must be handled. An unchecked exception can be thrown, which will prevent the class from being initialized. That, in turn, will usually prevent your application from running at all. In many applications, it might be desirable to use default properties instead, or fallback to another source of configuration, such as prompting a use in an interactive context.

总之,它可能看起来像这样:

Altogether, it might look something like this:

private static final String NAME = "my.properties";

private static final Properties config;

static {
  Properties fallback = new Properties();
  fallback.put("key", "default");
  config = new Properties(fallback);

  URL res = MyClass.getResource(NAME);
  if (res == null) throw new UncheckedIOException(new FileNotFoundException(NAME));
  URI uri;
  try { uri = res.toURI(); }
  catch (URISyntaxException ex) { throw new IllegalArgumentException(ex); }

  try (InputStream is = Files.newInputStream(Paths.get(uri))) { config.load(is); } 
  catch (IOException ex) { throw new UncheckedIOException("Failed to load resource", ex); }
}

这篇关于在静态初始化程序块中加载java属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆