简体   繁体   中英

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. 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. However, almost no one does this, and instead uses a framework like spring, which handles property injection for you.

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:

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.

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. 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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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