How to externalize Spring Boot application.properties to tomcat/lib folder

前端 未结 3 449
故里飘歌
故里飘歌 2020-11-30 02:02

I need a configuration free, deployable war, myapp1.war that can retrieve the configuration files from the tomcat/lib folder. As I have other web applications coexisting on

3条回答
  •  执念已碎
    2020-11-30 02:47

    With Spring 4.2 and @Annotation config and tomcat on linux serveur

    In your Application class set the @PropertySource like that :

    @Configuration
    @EnableWebMvc
    @PropertySource(value = { "classpath:application-yourapp.properties"})
    @ComponentScan(basePackages = "com.yourapp")
    public class YourAppWebConfiguration extends WebMvcConfigurerAdapter {
    
        ...
    }
    

    Now you just need to include the property file in your classpath

    In production

    Deploy your .war files ( or anything ) on tomcat, and put your application-yourapp.properties anyway on your production machine. ( for exemple in /opt/applyconfigfolder/application-yourapp.properties" )

    Then in your tomcat ( here tomcat 7 ) open bin\catalina.sh

    You have this line

    # Ensure that any user defined CLASSPATH variables are not used on startup,
    # but allow them to be specified in setenv.sh, in rare case when it is needed.
    CLASSPATH=
    

    Just add the path of the folder which contains application.properties

    CLASSPATH=:/opt/applyconfigfolder
    

    If you have already some classpath define you can add it

    CLASSPATH=:/opt/applyconfigfolder:/yourpath1:/yourpath2:
    

    I haven't try with windows but I think there is no problem

    In Dev ( with eclipse )

    ├src
    | └main
    |   └ ....
    └config
    | └application-yourapp.properties
    

    instead of src/main/resources/application-yourapp.properties

    Now in eclipse add your config folder to classpath, go to "Run Configurations" of your tomcat server ( or equivalent ) and add the folder Config to User Entries

    Ok that's it, your application.properties is out of the application and your project run perfectly in dev environment.

提交回复
热议问题