How to set context-param in spring-boot

2019-01-22 13:50发布

问题:

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>
...

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

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

回答1:

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");
        }
    };
}

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


回答2:

You can actually achieve this using Java config. If you have filter that requires some parameters, just put them in your application.yml (or .properties), inject them using @Value in your config class and register them in FilterRegistrationBean.

For example:

@Value("${myFilterParam}")
private String myFilterParam;

@Bean(name="myFilter")
public FilterRegistrationBean myFilter() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
    filterRegistrationBean.setInitParameters(Collections.singletonMap("p-name", "p-value"));
    return filterRegistrationBean;
}

Also JavaDoc for FilterRegistrationBean:

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/embedded/FilterRegistrationBean.html

Update

You can register parameters for servlet context in SpringBootServletInitializer#onStartup() method. Your Application class can extend the SpringBootServletInitializer and you can override the onStartup method and set the parameters there. Example:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

}

Other alternative is to define ServletContextInitializer bean as suggested by Andy Wilkinson.



回答3:

Also you can define InitParameterConfiguringServletContextInitializer in your configuration. Example:

@Bean
public InitParameterConfiguringServletContextInitializer initParamsInitializer() {
    Map<String, String> contextParams = new HashMap<>();
    contextParams.put("p-name", "-value");
    return new InitParameterConfiguringServletContextInitializer(contextParams);
}