首页 文章

如何将Spring Boot application.properties外部化到tomcat / lib文件夹

提问于
浏览
20

我需要一个免费的,可部署的war,myapp1.war,它可以从tomcat / lib文件夹中检索配置文件 . 由于我有其他Web应用程序共存于同一个Tomcat:myapp2.war,myapp3.war,我需要这样的布局:

tomcat/lib/myapp1/application.properties
tomcat/lib/myapp2/application.properties
tomcat/lib/myapp3/application.properties

通过这种方式,我可以在战争中构建war文件而不需要任何属性文件,并在任何服务器上部署 .

我已经阅读了Spring documentation但它解释了如何在作为jar运行时设置位置:

java -jar myapp.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

对于多个共存战争文件的情况,我无法弄清楚如何做到这一点 .

我想知道这是否可行,还是应该放弃Spring Boot并回到传统的Spring MVC应用程序 .

2 回答

  • 28

    一个解决方案可能是将application- .properties加载为@PropertySource注释,因为question建议,但是日志系统不会工作,正如您在documentation中看到的那样 .

    日志记录系统在应用程序生命周期的早期初始化,因此在通过@PropertySource注释加载的属性文件中找不到这样的日志记录属性 .

    这意味着您在application- .properties中的日志记录属性如:

    logging.config=classpath:myapp1/logback.xml
    logging.path = /path/to/logs
    logging.file = myapp1.log
    

    将被忽略,日志系统不会工作 .

    为了解决这个问题,我在配置应用程序时使用SpringApplicationBuilder.properties()方法在开头加载属性 . 在那里,我设置了Spring Boot使用的'spring.config.location'来加载所有application- .properties:

    public class Application extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
            return springApplicationBuilder
                    .sources(Application.class)
                    .properties(getProperties());
        }
    
        public static void main(String[] args) {
    
            SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
                    .sources(Application.class)
                    .properties(getProperties())
                    .run(args);
        }
    
       static Properties getProperties() {
          Properties props = new Properties();
          props.put("spring.config.location", "classpath:myapp1/");
          return props;
       }
    }
    

    然后我将属性文件从src / main / resources移动到src / main / resources / myapp1

    .
    ├src
    | └main
    |   └resources
    |     └myapp1
    |       └application.properties
    |       └application-development.properties
    |       └logback.xml
    └─pom.xml
    

    在pom.xml中,我必须将嵌入式tomcat库的范围设置为“提供” . 此外,要从最终的战争中排除src / main / resources / myapp1中的所有属性文件,并生成一个配置免费,可部署的战争:

    <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <packagingExcludes>
                  **/myapp1/
                </packagingExcludes>
            </configuration>
        </plugin>
    

    然后在Tomcat我有

    ├apache-tomcat-7.0.59
     └lib
       ├─myapp1
       |  └application.properties        
       |  └logback.xml
       └─myapp2
         └application.properties
         └logback.xml
    

    现在我可以生成配置免费战争并将其放入apache-tomcat-7.0.59 / webapps文件夹中 . 将使用类路径解析属性文件,每个webapp都是独立的:

    apache-tomcat-7.0.59/lib/myapp1
       apache-tomcat-7.0.59/lib/myapp2
       apache-tomcat-7.0.59/lib/myapp3
    
  • 1

    使用Spring 4.2和@Annotation配置以及linux上的tomcat服务器

    First externalize it in Dev environement ( with eclipse )

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

    而不是 src/main/resources/application-yourapp.properties

    然后在你的Application类中不要忘记设置@PropertySource:

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

    现在在eclipse中将config文件夹添加到classpath,转到tomcat服务器(或等效服务器)的“Run Configurations”并将文件夹Config添加到用户条目

    enter image description here

    好的就是它,你的application.properties不在应用程序中,你的项目在开发环境中运行得很好 .

    In production

    现在,在tomcat上部署.war文件(或任何东西),并将您的application-youpp.properties放在 生产环境 机器上 . (例如/opt/applyconfigfolder/application-yourapp.properties中的例子)

    然后在你的tomcat(这里是tomcat 7)打开 bin\catalina.sh

    你有这条线

    # 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=
    

    只需添加包含application.properties的文件夹的路径即可

    CLASSPATH=:/opt/applyconfigfolder
    

    如果您已经有一些类路径定义,则可以添加它

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

    我没有尝试使用Windows,但我认为没有问题

相关问题