Spring boot external configuration of property file

后端 未结 4 1171
感动是毒
感动是毒 2020-12-03 05:29

I have a spring boot application that I can package in a war that I want to deploy to different environments. To automate this deployment it\'d be easier to have the configu

相关标签:
4条回答
  • 2020-12-03 05:50

    Yes, you need to use @PropertySource as shown below.

    The important point here is that you need to provide the application_home property (or choose any other name) as OS environment variable or System property or you can pass as a command line argument while launching Spring boot. This property tells where the configuration file (.properties or .yaml) is exactly located (example: /usr/local/my_project/ etc..)

    @Configuration
    @PropertySource("file:${application_home}config.properties")//or specify yaml file
    @ComponentScan({"be.ugent.lca","be.ugent.sherpa.configuration"})
    @EnableAutoConfiguration
    @EnableSpringDataWebSupport
    public class Application extends SpringBootServletInitializer{
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:54

    Using external properties files

    The answer lies in the Spring Boot Docs, I'll try to break it down for you.

    First of all, no you should not use @PropertySource when working with Yaml configuration, as mentioned here under the Yaml shortcomings :

    YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.

    So, how to load propery files? That is explained here Application Property Files

    One is loaded for you: application.yml , place it in one of the directories as mentioned in the link above. This is great for your general configuration.

    Now for your environment specific configuration (and stuff like passwords) you want to use external property files, how to do that is also explained in that section :

    If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).

    So you use the spring.config.location environment property. Imagine you have an external config file: application-external.yml in the conf/ dir under your home directory, just add it like this: -Dspring.config.location=file:${home}/conf/application-external.yml as a startup parameter of your JVM. If you have multiple files, just seperate them with a comma. Note that you can easily use external properties like this to overwrite properties, not just add them.

    I would advice to test this by getting your application to work with just your internal application.yml file , and then overwrite a (test) property in your external properties file and log the value of it somewhere.

    Bind Yaml properties to objects

    When working with Yaml properties I usually load them with @ConfigurationProperties, which is great when working with for example lists or a more complex property structure. (Which is why you should use Yaml properties, for straightforward properties you are maybe better of using regular property files). Read this for more information: Type-Safe Configuration properties

    Extra: loading these properties in IntelliJ, Maven and JUnit tests

    Sometimes you want to load these properties in your maven builds or when performing tests. Or just for local development with your IDE

    If you use IntelliJ for development you can easily add this by adding it to your Tomcat Run Configuration : "Run" -> "Edit Configurations" , select your run configuration under "Tomcat Server" , check the Server tab and add it under "VM Options".

    To use external configuration files in your Maven build : configure the maven surefire plugin like this in your pom.xml:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
         <argLine>-Dspring.config.location=file:${home}/conf/application-external.yml
       </configuration>
    </plugin>    
    

    When running JUnit tests in IntelliJ:

    • Run → Edit Configurations
    • Defaults → JUnit
    • add VM Options -> -ea -Dspring.config.location=file:${home}/conf/application-external.yml
    0 讨论(0)
  • 2020-12-03 06:03

    One of the easiest way to use externalized property file using system environment variable is, in application.properties file you can use following syntax:

    spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/"nameofDB"
    spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME}
    spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PORT}
    

    Now, declare above used environment variables,

    export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost"
    export OPENSHIFT_MYSQL_DB_PORT="3306"
    export OPENSHIFT_MYSQL_DB_USERNAME="root"
    export OPENSHIFT_MYSQL_DB_PASSWORD="123asd" 
    

    This way you can use different value for same variable in different environments.

    0 讨论(0)
  • 2020-12-03 06:06

    Use below code in your boot class:

    @PropertySource({"classpath:omnicell-health.properties"})
    

    use below code in your controller:

    @Autowired
        private Environment env;
    
    0 讨论(0)
提交回复
热议问题