小编典典

使用web.xml配置spring-boot应用程序

spring-boot

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

这是现有的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构建脚本进行了一些更改以使其正常运行:

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

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

现在我想删除的进口PropertiesConfiguration.classApplicationConfiguration.classSpringDispatcherServletConfiguration.classJettyApplication.java,并以某种方式取代那些通过加载或导入的内容web.xml到嵌入式servlet容器。但我忽略这是否是正确的策略,是否可以实现。我将不胜感激任何帮助。

-解决方案

这是JettyApplication.class基于戴夫的答案的最终答案:

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 …敬请期待;)


阅读 342

收藏
2020-05-30

共1个答案

小编典典

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

SpringBootServletInitializer如果需要,您始终可以始终支持战争部署。

2020-05-30