如何从传统的Java Web应用程序(带有web.xml)迁移到Spring Boot? [英] How to migrate from traditional java web application (with web.xml) to spring boot?
问题描述
我想将项目切换到基于Spring的产品.
我的第一步是将Java Web应用程序从生成的WAR文件转换为由spring boot驱动的独立可执行jar.
让我们以github为例的开放源Web应用程序示例:Vaadin-Spring Web应用程序
我希望有一些指导让我执行转换.
就我所知,此应用程序不是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);
}
然后添加一些配置:
-
类型为
Servlet
或ServletRegistrationBean
的@Bean
将该bean安装在容器中,就好像它是web.xml
中的 -
类型为
Filter
或FilterRegistrationBean
的@Bean
的行为类似(类似于<filter/>
和<filter-mapping/>
). -
在这种情况下,
ApplicationContext
植根于XML文件中,因此最简单的第一步是将@Import
放入SpringApplication
中.这个很简单,可以在几行中作为@Bean
定义重新创建.
可以将 -
静态资源移动到类路径根目录中的
/public
(或/static
或/resources
或/META-INFO/resources
)
<servlet/>
和<servlet-mapping/>
一样.
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 typeServlet
orServletRegistrationBean
installs that bean in the container as if it was a<servlet/>
and<servlet-mapping/>
inweb.xml
A
@Bean
of typeFilter
orFilterRegistrationBean
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 SpringApplication
. 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屋!