使用 web.xml 配置 spring-boot 应用程序 [英] Configuring a spring-boot application using web.xml

查看:59
本文介绍了使用 web.xml 配置 spring-boot 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在引导一个现有的 Spring Web 应用程序,因此生成的 war 文件嵌入了一个 Jetty Web 服务器.我想尽可能地坚持现有配置以限制回归.

I'm bootifying an existing Spring Web application so the generated war file embed a Jetty web server. I want to stick to the existing configuration as much as I can in order to limit the regressions.

这是现有的web.xml:

<web-app id="fbecart-webapp" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.fbecart.ApplicationConfiguration</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>com.fbecart.MyDispatcherServlet</servlet-class>
    <init-param>
        <param-name>dispatchOptionsRequest</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.fbecart.SpringDispatcherServletConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>GzipFilter</filter-name>
    <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>GzipFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openSessionInView</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

这是我的主类JettyApplication.java:

package com.fbecart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ ApplicationConfiguration.class, SpringDispatcherServletConfiguration.class,
    EmbeddedServletContainerAutoConfiguration.class })
public class JettyApplication {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(JettyApplication.class, args);
  }
}

我对 Gradle 构建脚本进行了一些更改以使其正常工作:

I performed a few changes to my Gradle build scripts to make it work:

  • 为 spring-boot-starter 和 spring-boot-starter-jetty 添加依赖
  • spring-boot 插件的配置

应用程序启动正常,控制器已加载,我可以查询服务器.但是没有启用 web.xml 中定义的任何过滤器.

The app starts fine, the controllers are loaded and I can query the server. But none of the filters defined in the web.xml are enabled.

现在我想删除JettyApplication 中PropertiesConfiguration.classApplicationConfiguration.classSpringDispatcherServletConfiguration.class 的导入.java,并通过将 web.xml 的内容加载或导入到嵌入式 servlet 容器中来以某种方式替换它们.但我忽略了这是否是一个正确的策略以及我是否能做到.如有任何帮助,我将不胜感激.

Now I would like to remove the imports of PropertiesConfiguration.class, ApplicationConfiguration.class and SpringDispatcherServletConfiguration.class in JettyApplication.java, and somehow replace those by loading or importing the content of web.xml into the embedded servlet container. But I ignore if that is a right strategy and if I can make it. I would greatly appreciate any help.

-- 解决方案

这是基于 Dave 回答的最终 JettyApplication.class:

Here is the final JettyApplication.class based on Dave's answer:

package com.fbecart;

import org.eclipse.jetty.servlets.GzipFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

@Configuration
@Import({ ApplicationConfiguration.class, SpringDispatcherServletConfiguration.class,
    EmbeddedServletContainerAutoConfiguration.class })
public class JettyApplication {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(JettyApplication.class, args);
  }

  @Bean
  public DispatcherServlet dispatcherServlet() {
    return new MyDispatcherServlet();
  }

  @Bean
  public GzipFilter gzipFilter() {
    return new GzipFilter();
  }

  @Bean
  public CharacterEncodingFilter characterEncodingFilter() {
    final CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    return characterEncodingFilter;
  }

  @Bean
  public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() {
    return new OpenEntityManagerInViewFilter();
  }
}

我将在不久的将来用 ServletContainerInitializer 替换 web.xml...敬请期待;)

I will replace web.xml by a ServletContainerInitializer in a near future... stay tuned ;)

推荐答案

如果我是你,我会慢慢尝试剥离 web.xml 中的层并将其完全删除.这样,整个应用程序只有一个配置,包括所有过滤器和 servlet(无论如何都是这个想法).您可以在稳定过滤器在 web.xml 中重复的位置的同时进行一些并行运行,然后当您在主应用程序中具有相同的功能时,您可以删除 web.xml.要将过滤器添加到您的主应用程序,只需为 FilterFilterRegistrationBean 实例创建 @Bean 定义.

If I were you I would slowly try to peel away the layers in web.xml and remove it altogether. That way you will only have one configuration for the whole application including all filters and servlets (that's the idea anyway). You can do some parallel running while you stabilize where the filters are duplicated in web.xml, and then when you have the same functionality in a main application you can just delete web.xml. To add filters to your main application just create @Bean definitions for Filter or FilterRegistrationBean instances.

如果需要,您也可以始终通过 SpringBootServletInitializer 支持战争部署.

You can always support a war deployment through SpringBootServletInitializer as well if needed.

这篇关于使用 web.xml 配置 spring-boot 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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