Is it bad practice to include properties/configuaration files within jars?

前端 未结 11 992
谎友^
谎友^ 2021-02-07 00:34

For example:

MyApp is a web app that contains a properties file (server.properties) that describes config data (e.g. server names) for the app. In the development phase

11条回答
  •  离开以前
    2021-02-07 01:10

    If you are using Spring then you can make use of the property placeholder to do the work.

    
    
    

    You can specify a resource on the filesystem and also on the classpath, Spring will first try and resolve all of the properties from the filesystem and then fallback to the classpath. By specifying the "ignore-resource-not-found" attribute as "true" it will prevent Spring from throwing an exception if the file isn't present on the filesystem and allow it to resolve the properties from the classpath instead.

    Using this combination also allows you to split the properties over two files, for example you might never want to specify passwords in the file on the classpath and expect them to be specified externally on the filesystem. Any properties missing from the filesystem will be resolved from the classpath.

    In a folder containing:

    application.jar
    config.properties
    

    You should be able to use:

    java -jar application.jar
    

    This is a sample config.properties for reference:

    # This file contains properties that are used to configure the application
    database.url=127.0.0.1
    database.port=3306
    database.user=root
    database.pass=p4ssw0rd
    

提交回复
热议问题