首页 文章

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

提问于
浏览
1

我有一个 spring 启动项目,包含pom文件中的包装战争 .

<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环境中部署此战争时传递此参数?我在tomcat的webapps文件夹中复制war . 但是我能够通过上述论点?

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

1 回答

  • 0

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

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

相关问题