What are Best Practice Recommendations for Java EE 7 Property File Configuration?

前端 未结 2 992
长发绾君心
长发绾君心 2021-02-02 03:55

Where does application configuration belong in modern Java EE applications? What best practice(s) recommendations do people have?

By application configuration, I mean se

2条回答
  •  轮回少年
    2021-02-02 04:36

    I don't know what is best practice, but here is what we do.

    (Note however that this only works well for one installation per application per server and will fail when one wants to use multiple deployments per server, say for multitenancy deployments).

    CDI injection of properties values

    We use a somewhat sophisticated CDI injection approach to inject configuration values from .properties files directly into beans, like this:

    @Inject @ConfigurationValue(value="amazonS3FileContentsAccessKey")
    private String accessKey;
    

    The corresponding @Producer bean reads configuration files from the class path and from a given "local" location:

    global/local .properties files

    1. Each EAR contains a "global" .properties file on the class path for configuration values that change seldom and/or usually remain consistent through environments (such as days for something to expire). Further, the global configuration file contains sane default values (e.g. "localhost" for database server hostname). The global properties files (there are multiple, see below) are maintained in the source tree.
    2. For every development environment/installation/server/deployment, there (possibly) is a "local" properties file that contains the local settings that overwrite the global configuration's settings, e.g., database names, passwords etc.

    The expected path to "local" properties files is configured in the global configuration file (e.g., /etc/myapp/local.properties) or C:\myapp\local.properties.

    Actually, we even allow substitution of some variables in the filename for the local configuration files, such as "${hostname}". The original idea was that the local properties could also be maintained in some central source control by distinguishing them by hostname (local.machineA.properties, local.machineB.properties), but we don't use that at the moment, because our production settings are the same on all machines (Amazon S3 keys, database password/host etc).

    Assembling for dev, testing, production

    We assemble different EARs depending on the stage of development using Maven profiles.
    On assemply, the desired global.${profile.name}.properties file (where profile.name is, e.g., dev or production) is copied to the expected global.properties file in the classpath.

    For example, dev and testing share a common AmazonS3 secret/bucket, which is configured once for all developers in the configuration.dev.properties file, while the configuration.production.properties does not contain our production keys.

    Furthermore, our dev and testing environments have debugging enabled and configured in, say web.xml, but of course staging and production have not. Our .properties-based approach cannot change files such as web.xml, but with Maven build profiles it's easy.

提交回复
热议问题