Skip to content

Other Solution 2

Wuyi Chen edited this page Aug 6, 2018 · 5 revisions

Code

@PropertySourceX({ "config/app.properties" })
public class AppConfig3 extends ConfigurationY {
    @ValueX("app.host")
    static String host;

    @ValueX("app.api_key")
    static String apiKey;

    @ValueX("app.username")
    static String username;

    @ValueX("app.password")
    static String password;
}

Description

This solution uses a static class. It loads the parameters from the config file by inheriting the ConfigurationY class. The ConfigurationY class has the ability to detect all the sub-classes and help them load parameters. This is the most automatic solution with the minimal amount of code. It also uses the @PropertySourceX annotation for storing the path and uses the @ValueX annotation for the name of parameters.

Pros & Cons

Pros

Cons

  • If an exception occurs when loading the parameter, it can not be captured (Because this process is in the ConfigurationY class).
  • This class can only map to one external configuration.
  • The ConfigurationY class need to scan all the sub-classes. So the performance will be degraded by this reason.
Clone this wiki locally