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

2024-01-16

我有一个 Spring Boot 项目,在 pom 文件中声明了打包 war 。

<packaging>war</packaging>   
...
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/>
</parent>

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

java -jar <artifactId>.war --spring.config.name=application-<profile>

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

Edit更多说明:我不是通过设置系统变量或其他东西来寻找解决方案。从我的角度来看,一个正确的解决方案是通过 Maven 配置文件进行配置。例如,如果我用以下命令构建我的项目

mvn clean package -P<profile>

该参数被传递到 Spring Boot 中的适当位置。

Edit 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);
    }

}

我的应用程序类:

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);
    }
}

您可以使用 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>

然后像这样使用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>

然后使用配置文件设置 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);
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 spring boot 参数传递给 tomcat 部署? 的相关文章

随机推荐