简体   繁体   English

如何从一个类到另一个类获取我的配置文件?

[英]How can I get my config file from class to class?

Just a quick and simple question. 只是一个快速而简单的问题。 I have a program with several classes that read information off of a .properties file. 我有一个程序,有几个类从.properties文件读取信息。 Is it better practice to pass the file from class to class as an argument in the constructor, or open the file directly in each class? 将文件从类传递给类作为构造函数中的参数是更好的做法,还是直接在每个类中打开文件?

If you're going to do this by hand, I would recommend you create a configuration class, that takes the file via the constructor, and reads the property values into member variables. 如果您要手动执行此操作,我建议您创建一个配置类,通过构造函数获取文件,并将属性值读取到成员变量中。 Then every other class that needs configuration takes a Configuration class via it's constructor. 然后,需要配置的每个其他类通过它的构造函数获取Configuration类。 However, almost no one does this, and instead uses a framework like spring, which handles property injection for you. 但是,几乎没有人这样做,而是使用像spring这样的框架来处理属性注入。

In spring, it would look something like this: 在春天,它看起来像这样:

<!-- application context xml file -->
<context:property-placeholder location="file:///some/path/to/file" />

Then in your java classes: 然后在你的java类中:

public class SomeClass {
    @Value("${some.property}")
    private String someProp;

    @Value("${some.other.prop}")
    private Integer someOtherProp;

    // ...
}

At application startup the properties get injected into your class. 在应用程序启动时,属性会被注入到您的类中。

My suggestion is to have a Util class which loads the properties file and get values from that Util to the required classes. 我的建议是让一个Util类加载属性文件,并从该Util获取所需类的值。

Note: I dont think you have any issues on loading the properties file. 注意:我不认为您在加载属性文件时遇到任何问题。

I would suggest that you create an immutable class that takes in the file as a constructor argument and sets all the instance variables. 我建议您创建一个不可变类,它将文件作为构造函数参数接收并设置所有实例变量。 I'd call it PropertyConfiguration. 我称之为PropertyConfiguration。 Then since the class is immutable, you won't have to worry about passing it to everyone. 然后,由于该类是不可变的,您不必担心将它传递给每个人。 You could even have a class that holds it. 你甚至可以拥有一个持有它的类。

For example, the code below would set you up to have a nice set up to have several things available project wide. 例如,下面的代码会让你有一个很好的设置,可以在项目范围内提供一些可用的东西。 I would just ensure that anything that's shared be immutable to ensure thread safety. 我只是确保共享的任何内容都是不可变的,以确保线程安全。

public class ClientUtils {

    private static ClientContext _clientContext = null;

    public static void setClientContext(ClientContext cc) {
        _clientContext = cc;
    }

    public static ClientContext getContext() {
        return _clientContext;
    }
}

public class ClientContext {

    private final Configuration _configuration;

    public ClientContext(Configuration config){
        _configuration = config;
    }

    public Configuration getClientContext() {
        return _configuration;
    }

}

If your program contains data which need not be a part of compilation and can vary from deployment to deployment, you must add it to the properties file : ( Things like database connection string, email addresses ). 如果您的程序包含的数据不需要是编译的一部分,并且可能因部署而异,则必须将其添加到属性文件中:(例如数据库连接字符串,电子邮件地址)。

Just in case you need this, I'm adding the code for accessing the properties files. 万一你需要这个,我正在添加访问属性文件的代码。 Drop the file in build directory. 将文件放在构建目录中。

Properties properties = new Properties();       
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("credentials.properties"));

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

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