gpt4 book ai didi

spring-mvc - 如何在 spring-boot 中设置 context-param

转载 作者:行者123 更新时间:2023-12-03 06:48:32 24 4
gpt4 key购买 nike

在经典的 web.xml 类型配置中,您可以像这样配置上下文参数

web.xml

...
<context-param>
<param-name>p-name</param-name>
<param-value>-value</param-value>
</context-param>
...

这是如何在 spring-boot 中实现的。我有一个需要参数的过滤器。

我正在使用@EnableAutoConfiguration并已包括<artifactId>spring-boot-starter-jetty</artifactId>在我的 pom 中。

最佳答案

您可以使用server.servlet.context-parameters应用程序属性来设置参数。例如:

server.servlet.context-parameters.p-name=p-value

在不再支持的 Spring Boot 1.x 中,此属性被命名为 server.context-parameters:

servlet.context-parameters=p-name=p-value

或者,您可以通过声明 ServletContextInitializer bean 以编程方式配置参数:

@Bean
public ServletContextInitializer initializer() {
return new ServletContextInitializer() {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("p-name", "-value");
}
};
}

关于spring-mvc - 如何在 spring-boot 中设置 context-param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com