如何将 spring boot 参数传递给 tomcat 部署?
我有一个 spring boot 项目,在 pom 文件中说明了打包战争.
I have a spring boot project with packaging war stated in the pom file.
<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 文件.下一步是使用
With a maven clean package command I'm able to build the war file. Next step is to launch the war file with
java -jar <artifactId>.war --spring.config.name=application-<profile>
重要的是我传递了一个工作正常的参数 (spring.config.name).但我的问题是如何在 tomcat 环境中部署这场战争时传递这个论点?我把war复制到tomcat的webapps文件夹中.但是我可以在哪里传递提到的参数?
Important is that I pass an argument (spring.config.name) which is working fine. But my question is how can I pass this argument while deploying this war in a tomcat environment? I copy the war in the webapps folder of tomcat. But where I'm able to pass the mentioned argument?
编辑以获得更多说明:我不是通过设置系统变量或其他东西来寻找解决方案.从我的角度来看,一个合适的解决方案是在 Maven 配置文件上进行配置.例如,如果我使用
Edit for more clarification: I'm not searching for a solution by setting a system variable or something. A proper solution from my point of view would be a configuration over a maven profile. For example if I build my project with
mvn clean package -P<profile>
参数被传递到 spring boot 中的适当位置.
the argument is passed to the appropriate location in spring boot.
编辑 2:我的 ServletInitializer 继承自 SpringBootServletInitializer,后者继承自 WebApplicationInitializer
Edit 2: My ServletInitializer extends from SpringBootServletInitializer which extends from 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 旁边:
You can pass context parameters to the servlet context with a context.xml. Add this as context.xml next to your 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
Then use the maven-war-plugin like this
<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 中工作,你可以使用这样的东西:
Then use a profile to set the spring.profiles.active property. Spring actually will pick these up without config but somehow Spring Boot does not. For it to work in Spring Boot you can use something like this:
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);
}
}