如何在spring-boot中设置context-param [英] How to set context-param in spring-boot

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

问题描述

在经典的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中包含了<artifactId>spring-boot-starter-jetty</artifactId>.

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中设置context-param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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