简体   繁体   English

Java - 在启动期间读出系统环境

[英]Java - Read out system environment during startup

What is the best way to read out system environment in an Java application and class visibility function? 在Java应用程序和类可见性函数中读取系统环境的最佳方法是什么?

I need to eg os.name and have designed a class like 我需要例如os.name并设计了类似的类

private String osName; 

private void readSystemSettings() {
    osName = System.getProperty("os.name");
}

public void printSystemSettings() {
    System.out.println(this.osName);
    ...
}

public SystemEnvironment() {
    readSystemSettings();       
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
}
  1. What are the best practice for getting those information? 获取这些信息的最佳做法是什么? Call this function always on start-up or only from time to time? 始终在启动时或仅不时调用此功能?

  2. I want to read out those information as soon as the class get instantiated. 我想在实例化类之后立即读出这些信息。 Therefor the constructor is calling the readSystemSettings() function. 因此构造函数调用readSystemSettings()函数。

As this is information will always be the same during runtime, I actually need only no stance. 因为这是在运行时期间信息总是相同的,我实际上只需要没有立场。 Means all variables + functions shall be final. 意味着所有变量+函数都是最终的。 Or am I wrong? 还是我错了?

  1. If 2) is correct understanding, how to do? 如果2)正确理解,该怎么办?

You can have a class with all the variables marked as final and then initialize in a static block. 您可以拥有一个类,其中所有变量都标记为final,然后在静态块中初始化。

public class SystemProperties{

  public static final String OS_NAME;
  // other properties

  static{
    OS_NAME = System.getProperty("os.name");
    // initialize other properties

  }

}

Else, if you're in a managed environment like Spring or EJB, you can mark SystemProperties as singleton and initialize the variable in a method annotated with @PostContruct . 否则,如果您处于Spring或EJB等托管环境中,则可以将SystemProperties标记为singleton并在使用@PostContruct注释的方法中初始化变量。

public class SystemProperties{

  public static String OS_NAME;
  // other properties

  @PostConstruct
  private void init(){
    OS_NAME = System.getProperty("os.name");
    // initialize other properties

  }

}

You could use a utility class: 您可以使用实用程序类:

public final class Utilities {

    public static final String OS_NAME = System.getProperty("os.name");

    private Utilities() { } // utility classes can't be instantiated

}

This way, this property will only be initialized once, during your application startup. 这样,在应用程序启动期间,此属性只会初始化一次。 You can then access this property from everywhere in your code with Utilities.OS_NAME . 然后,您可以使用Utilities.OS_NAME从代码中的任何位置访问此属性。

Since the value is set globally you could do 由于该值是全局设置的,您可以这样做

enum SystemEnvironment {
    ;
    public static final String OS_NAME = System.getProperty("os.name");

or you could look ti up each time as you shouldn't be call it that often. 或者你可以每次都看起来因为你不应该经常这样称呼它。

enum SystemEnvironment {
    ;
    public static getOsName() {
         return System.getProperty("os.name");
    }

if you are using it often I suggest creating some tests for it. 如果你经常使用它我建议为它创建一些测试。

enum SystemEnvironment {
    ;
    public static final boolean IS_WINDOWS = getOsName().startsWith("Window");
    public static getOsName() {
         return System.getProperty("os.name");
    }

You can use the OWNER library. 您可以使用OWNER库。

The OWNER API is a Java library with the goal of minimizing the code required to handle application configuration via Java properties files. OWNER API是一个Java库,其目标是最小化通过Java属性文件处理应用程序配置所需的代码。

So to load properties you should create class like this 所以要加载属性,你应该像这样创建类

public interface MyConfig extends Config {
    @Key("os.name")
    String osName();
}

Then you can load config whenever you need: 然后您可以在需要时加载配置:

ServerConfig cfg = ConfigFactory.create(MyConfig.class,
        System.getProperties(), 
        System.getenv()
);
System.out.println("Os name:" + cfg.osName());

For more information you can see the documentation http://owner.aeonbits.org/docs/usage/ 有关更多信息,请参阅文档http://owner.aeonbits.org/docs/usage/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM