如何在 spring-boot 中设置上下文参数 [英] How to set context-param in spring-boot

查看:249
本文介绍了如何在 spring-boot 中设置上下文参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the classic web.xml type configuration you could configure context parameters like so

web.xml

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

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

How is this achieved in spring-boot. I have a filter that requires parameters.

我正在使用 @EnableAutoConfiguration 并且在我的 pom 中包含了 spring-boot-starter-jetty.

I'm using @EnableAutoConfiguration and have included <artifactId>spring-boot-starter-jetty</artifactId> in my pom.

推荐答案

您可以通过声明一个 ServletContextInitializer bean 来对整个 ServletContext 设置参数:

You can set parameters on the whole ServletContext by declaring a ServletContextInitializer bean:

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

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

更新:在 Spring Boot 1.2 中不再需要使用 ServletContextInitializer.您现在可以在 application.properties 中的一行中配置 ServletContext 上的参数:

Update: in Spring Boot 1.2 using a ServletContextInitializer is no longer necessary. You can now configure a parameter on the ServletContext in a single line in application.properties:

server.context_parameters.p-name=-value

这篇关于如何在 spring-boot 中设置上下文参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆