如何从传统的Java Web应用程序(带有web.xml)迁移到Spring Boot? [英] How to migrate from traditional java web application (with web.xml) to spring boot?

查看:861
本文介绍了如何从传统的Java Web应用程序(带有web.xml)迁移到Spring Boot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将项目切换到基于Spring的产品.

我的第一步是将Java Web应用程序从生成的WAR文件转换为由spring boot驱动的独立可执行jar.

让我们以github为例的开放源Web应用程序示例:Vaadin-Spring Web应用程序

web.xml文件可以在这里找到.

根目录-上下文文件可以在这里找到.

我希望有一些指导让我执行转换.

我也在spring-boot项目中提交了一个问题.

就我所知,此应用程序不是Spring MVC应用程序-如果可以的话,迁移起来可能会容易得多. (根据github问题)的目标是获取可执行的JAR.不过,基本计划可能是先使用Spring Boot迁移到WAR,然后在可行时迁移到JAR.这是一个非常简单的应用程序,因此我们真正需要做的就是查看web.xml并将其转换为相关的Spring Boot功能.以下是一些一般指南:

通过扩展SpringBootServletInitializer(例如,在名为Application的类中)创建可部署的WAR,并添加Spring Boot @EnableAutoConfiguration批注.示例:

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

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

然后添加一些配置:

  • 类型为ServletServletRegistrationBean@Bean将该bean安装在容器中,就好像它是web.xml

  • 中的<servlet/><servlet-mapping/>一样.
  • 类型为FilterFilterRegistrationBean@Bean的行为类似(类似于<filter/><filter-mapping/>).

  • 在这种情况下,ApplicationContext植根于XML文件中,因此最简单的第一步是将@Import放入Spring Application中.这个很简单,可以在几行中作为@Bean定义重新创建.

  • 可以将
  • 静态资源移动到类路径根目录中的/public(或/static/resources/META-INFO/resources)

WAR运行后,我们可以通过在Application中添加main方法使其成为可执行文件,例如

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

另请参见将JAR转换为WAR的入门指南.

>

正如我所说,此特定应用程序的最大问题是它不是Spring MVC应用程序.正如爱尔兰人可能会说:先生,如果我想去那里,我不会从这里开始."总的来说,这是一个有趣的问题,但是我建议任何其他希望将Spring应用程序迁移到Spring Boot的人阅读此处的一般建议,但也许可以在其他地方开始另一个讨论.

无论如何,我会在转换此特定应用程序时大吃一惊(源代码罐会很好),如果我学到新知识,就会更新此响应.

I wanna switch my projects to spring-based product.

My first step is to transform my java web application from a generated WAR file, to a standalone executable jar, powered by spring boot.

Let's take a open source web application example from github.: Vaadin-Spring Web Application

The web.xml file can be found here.

The root-context file can be found here.

I hope that there are some guides for me to perform the transformation.

I have also submit an issue in the spring-boot project.

解决方案

This application is not a Spring MVC application as far as I can tell - it would probably be a lot easier to migrate if it was. The goal (per the github issue) is to obtain an executable JAR. The basic plan though might be to migrate first to a WAR using Spring Boot and then to a JAR once that is working. It's a pretty simple app so all we really need to do is look at the web.xml and translate it into the relevant Spring Boot features. Here are some general guides:

Create a deployable WAR by extending SpringBootServletInitializer (e.g. in a class called Application), and add the Spring Boot @EnableAutoConfiguration annotation. Example:

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

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

Then add some configuration:

  • A @Bean of type Servlet or ServletRegistrationBean installs that bean in the container as if it was a <servlet/> and <servlet-mapping/> in web.xml

  • A @Bean of type Filter or FilterRegistrationBean behaves similarly (like a <filter/> and <filter-mapping/>).

  • The ApplicationContext in this case is rooted in an XML file, so the easiest first step is to @Import that into the Spring Application. This one is so simple that it can be recreated in a few lines as @Bean definitions.

  • Static resources can be moved to /public (or /static or /resources or /META-INFO/resources) in the classpath root

Once the WAR is working we make it executable by adding a main method to our Application, e.g.

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

See also the Getting Started Guide on Converting a JAR to a WAR.

As I said, the biggest problem with this specific app is that it isn't a Spring MVC app. As the Irishman might say "If I wanted to get to there, sir, I wouldn't be starting from here." This is an interesting question in general, but I recommend anyone else looking to migrate a Spring application to Spring Boot read the general advice here but maybe start another discussion somewhere else.

Anyway, I'll have a bash at converting this specific app (source code jars would be nice), and update this response if I learn anything new.

这篇关于如何从传统的Java Web应用程序(带有web.xml)迁移到Spring Boot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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