简体   繁体   English

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

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

I need a configuration free, deployable war, myapp1.war that can retrieve the configuration files from the tomcat/lib folder. 我需要一个免配置,可部署的war,myapp1.war,它可以从tomcat / lib文件夹中检索配置文件。 As I have other web applications coexisting on the same Tomcat: myapp2.war, myapp3.war, I need this layout: 由于我有其他Web应用程序共存于同一个Tomcat:myapp2.war,myapp3.war,我需要这个布局:

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

This way I can build the war files without any properties files inside the war and deploy on any server. 通过这种方式,我可以在战争中构建war文件而不需要任何属性文件,并在任何服务器上部署。

I have read the Spring documentation but it explains how to set the location when running as a jar: 我已经阅读了Spring文档,但它解释了如何在作为jar运行时设置位置:

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

I cannot figure out how to do this for the case of multiple coexisting war files. 对于多个共存战争文件的情况,我无法弄清楚如何做到这一点。

I would like to know if this is possible or should I give up on Spring Boot and go back to the traditional Spring MVC applications. 我想知道这是否可行,还是应该放弃Spring Boot并回到传统的Spring MVC应用程序。

A solution could be to load application-{profile}.properties as @PropertySource annotations as this question suggests, but then the logging system wont work, as you can see in the documentation . 解决方案可能是将application- {profile} .properties加载为@PropertySource注释,如此问题所示,但随后日志记录系统无法正常工作,如文档中所示

The logging system is initialized early in the application lifecycle and as such logging properties will not be found in property files loaded via @PropertySource annotations. 日志记录系统在应用程序生命周期的早期初始化,因此在通过@PropertySource注释加载的属性文件中找不到这样的日志记录属性。

This means that your logging properties in application-{profiles}.properties like: 这意味着您在application- {profiles} .properties中的日志记录属性如:

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

will be ignored and the logging system wont work. 将被忽略,日志系统不会工作。

To solve this I have used the SpringApplicationBuilder.properties() method to load properties at the beginning, when the application is configured. 为了解决这个问题,我在配置应用程序时使用SpringApplicationBuilder.properties()方法在开头加载属性。 There I set the 'spring.config.location' used by Spring Boot to load all the application-{profiles}.properties: 在那里,我设置了Spring Boot使用的'spring.config.location'来加载所有application- {profiles} .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;
   }
}

Then I have moved the properties files from src/main/resources to src/main/resources/myapp1 然后我将属性文件从src / main / resources移动到src / main / resources / myapp1

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

In the pom.xml I have to set the scope of embedded tomcat libraries as "provided". 在pom.xml中,我必须将嵌入式tomcat库的范围设置为“提供”。 Also, to exclude all properties files in src/main/resources/myapp1 from the final war and generate a configuration free, deployable war: 此外,要从最终的战争中排除src / main / resources / myapp1中的所有属性文件,并生成一个配置免费,可部署的战争:

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

Then in Tomcat I have 然后在Tomcat我有

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

Now I can generate the configuration free war and drop it into the apache-tomcat-7.0.59/webapps folder. 现在我可以生成配置免费战争并将其放入apache-tomcat-7.0.59 / webapps文件夹中。 Properties files will be resolved using the classpath, independently for each webapp: 将使用类路径解析属性文件,每个webapp都是独立的:

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

With Spring 4.2 and @Annotation config and tomcat on linux serveur 使用Spring 4.2和@Annotation配置以及linux上的tomcat服务器

In your Application class set the @PropertySource like that : 在您的Application类中,设置@PropertySource:

@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. 在tomcat上部署.war文件(或任何东西),并将您的application-youpppp.properties放在生产机器上。 ( for exemple in /opt/applyconfigfolder/application-yourapp.properties" ) (例如/opt/applyconfigfolder/application-yourapp.properties中的例子)

Then in your tomcat ( here tomcat 7 ) open bin\\catalina.sh 然后在你的tomcat(这里是tomcat 7)打开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 只需添加包含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 我没有试过Windows但我认为没有问题

In Dev ( with eclipse ) 在Dev(与eclipse)

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

instead of src/main/resources/application-yourapp.properties 而不是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 现在在eclipse中将config文件夹添加到classpath,转到tomcat服务器(或等效服务器)的“Run Configurations”并将文件夹Config添加到用户条目

在此输入图像描述

Ok that's it, your application.properties is out of the application and your project run perfectly in dev environment. 好的就是它,你的application.properties不在应用程序中,你的项目在开发环境中运行得很好。

Daniel Mora gave a good solution but instead of using spring.config.location you can use spring.config.name ( https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files ), so you can have different properties file for different web apps in the same tomcat/lib directory: Daniel Mora提供了一个很好的解决方案但是你可以使用spring.config.location而不是spring.config.location( https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features- external-config.html #boot-features-external-config-application-property-files ),因此您可以在同一个tomcat / lib目录中为不同的Web应用程序提供不同的属性文件:

    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.name", "myapp1");
      return props;
   }
}

I think that the lib directory is for third party libraries not for storing configuration properties for your web apps. 我认为lib目录适用于第三方库,不适用于存储Web应用程序的配置属性。 So I think that a better solution is to add an external folder as additional classpath folder using shared.loader property in conf/catalina.properties: 所以我认为更好的解决方案是使用conf / catalina.properties中的shared.loader属性添加外部文件夹作为附加的classpath文件夹:

shared.loader=${catalina.base}/shared/configurations shared.loader = $ {catalina.base} /共享/配置

You can put your application properties app1.properties, app2.properties, ecc.. in apache-tomcat-7.0.59/shared/configurations. 您可以将应用程序属性app1.properties,app2.properties,ecc ..放在apache-tomcat-7.0.59 / shared / configurations中。

Before finding Daniel Mora solution of overridding configure method of SpringBootServletInitializer my solution was to add a context.xml in src/main/webapp/META-INF with this content: 在找到覆盖SpringBootServletInitializer的配置方法的Daniel Mora解决方案之前,我的解决方案是在src / main / webapp / META-INF中添加一个带有以下内容的context.xml:

<Context>
    <Environment name="spring.config.name" value="myapp1" type="java.lang.String" override="false" description="define the property file for srping boot application"/>
</Context>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Tomcat中为Spring Boot应用程序访问externalize application.properties? - access externalize application.properties in Tomcat for Spring boot application? 作为WAR在Springcat上部署的Spring Boot,如何针对不同配置文件(Dev,Test,Staging,Prod)外部化application.properties - Spring boot deployed as WAR on tomcat, How to Externalize application.properties for different profiles(Dev,Test,Staging,Prod) 如何在 Spring 的 Tomcat Web 服务器中外部化 application.properties? - How to externalize application.properties in Tomcat webserver for Spring? 如何将Spring Boot中的application.properties文件外部化为外部依赖JAR? - How to externalize application.properties files in Spring Boot to e.g. external dependent JAR? 在插件中通过Tomcat覆盖Spring Boot application.properties属性? - Overwriting Spring Boot application.properties properties in plugin and through Tomcat? 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? Spring启动jar没有读取/解析application.properties根文件夹 - Spring boot jar not reading/parsing application.properties root folder 如何在Spring Boot中的application.properties中指定外部属性文件? - How to specify external properties files in application.properties in Spring Boot? Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM