Jetty如何在程序中设置Spring profile?

5
我正在寻找一种以编程方式设置Jetty中Spring配置文件的方法,以便服务器上的war文件使用给定配置文件。以下是我的代码:
final WebAppContext context = new WebAppContext();
context.setLogUrlOnStart(true);
context.setWar("target/deployables/myapp-rest/myapp-rest.war");
context.setContextPath("/" + TEST_APP_CONTEXT);
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();

我尝试了几种方法,但似乎都没起作用…… 我需要传递-Dspring.profiles.active=myProfile。

1个回答

0

这是在 Jetty 9.3 上测试过的,但似乎 webdefault.xml 也适用于较低版本(尽管其位置可能不同)。

前往 $JETTY_HOME/etc 并打开 webdefault.xml。在文件中搜索 context-param。在下面的某个地方添加以下代码:

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>prod</param-value>
</context-param>

如果您的web.xml(在your-app.war文件中)不包含此context-param,则此方法将起作用。

否则,您也可以使用override-web.xmldocs),但您需要在jetty-web.xml中进行配置,并且jetty-web.xml必须捆绑在war内...所以YMMV,但我不想改变我的war,webdefault.xml对我来说是一个更简单的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接