如何将 spring boot 参数传递给 tomcat 部署?

Cha*_*min 5 java tomcat spring-boot

我有一个 spring boot 项目,在 pom 文件中说明了打包战争。

<packaging>war</packaging>   
...
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
</parent>
Run Code Online (Sandbox Code Playgroud)

使用 maven clean package 命令,我可以构建 war 文件。下一步是启动war文件

java -jar <artifactId>.war --spring.config.name=application-<profile>
Run Code Online (Sandbox Code Playgroud)

重要的是我传递了一个工作正常的参数 (spring.config.name)。但我的问题是如何在 tomcat 环境中部署这场战争时传递这个论点?我把war复制到tomcat的webapps文件夹中。但是我可以在哪里传递提到的论点?

编辑以获得更多说明:我不是通过设置系统变量或其他东西来寻找解决方案。从我的角度来看,一个合适的解决方案是在 Maven 配置文件上进行配置。例如,如果我用

mvn clean package -P<profile>
Run Code Online (Sandbox Code Playgroud)

参数被传递到 spring boot 中的适当位置。

编辑 2:我的 ServletInitializer 扩展自 SpringBootServletInitializer,它扩展自 WebApplicationInitializer

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

而我的应用程序类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Arl*_*rlo 0

您可以使用 context.xml 将上下文参数传递到 servlet 上下文。将其作为 context.xml 添加到您的 pom 旁边:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Context>
<Context>
    <Parameter
        name="spring.profiles.active"
        value="${spring.profiles.active}"
        override="false" />
</Context>
Run Code Online (Sandbox Code Playgroud)

然后像这样使用maven-war-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <containerConfigXML>context.xml</containerConfigXML>
        <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

然后使用配置文件设置 spring.profiles.active 属性。Spring 实际上会在没有配置的情况下拾取这些,但不知何故 Spring Boot 却不会。为了让它在 Spring Boot 中工作,你可以使用这样的东西:

package com.example;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    private ServletContext servletContext;

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        String activeProfiles =
                servletContext.getInitParameter("spring.profiles.active");
        if (activeProfiles != null) {
            builder.profiles(activeProfiles.split(","));
        }
        return builder.sources(DemoApplication.class);
    }

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        this.servletContext = servletContext;
        super.onStartup(servletContext);
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)