我正在寻找一种以编程方式在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
最佳答案
这是使用 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.xml
( docs ),但您需要在 jetty-web.xml
和 jetty 中配置它-web.xml
必须捆绑在 war 中...所以 YMMV,但我不想改变我的 war ,webdefault.xml
对我来说是一个更简单的解决方案。
关于spring - 以编程方式设置 Jetty Spring 轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163447/