如何从 Spring 3.x 迁移到 5.x?

我有一个 Spring 3 项目,我希望将其迁移到 Spring 5。


现在,我在下面给出的项目中有 web.xml :


<servlet-mapping>

                    <servlet-name>mvc-dispatcher</servlet-name>

                    <url-pattern>/registrationapplication/*</url-pattern>

                </servlet-mapping>

                <servlet-mapping>

                    <servlet-name>mvc-dispatcher</servlet-name>

                    <url-pattern>/maintenance/lienholder/*</url-pattern>

                </servlet-mapping>

                <servlet-mapping>

                    <servlet-name>mvc-dispatcher</servlet-name>

                    <url-pattern>/login/*</url-pattern>

                </servlet-mapping>

                <servlet-mapping>

                    <servlet-name>mvc-dispatcher</servlet-name>

                    <url-pattern>/messagemap/*</url-pattern>

                </servlet-mapping>


            <servlet>

                    <servlet-name>mvc-dispatcher</servlet-name>

                    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>

                    <load-on-startup>1</load-on-startup>

                </servlet>


            <context-param>

                    <param-name>contextConfigLocation</param-name>

                    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>

                </context-param>

在迁移之前,我有一些疑问。具体如下:


从 Spring 3.0 迁移到 Spring 5 时,如何将 web.xml 中的以下行转换为基于 Java 的配置


我是否需要读取 mvc-dispatcher-servlet.xml 或者是否需要迁移基于 java 的配置以在 spring 5 中转换 mvc-dispatcher-servlet.xml ?


幕布斯6054654
浏览 212回答 1
1回答

犯罪嫌疑人X

1.Spring 5不需要更改。web.xml您可以更改servlet版本(如果需要)。它保持原样:Spring 的典型 web.xml:&nbsp;<?xml version="1.0" encoding="UTF-8"?>&nbsp; &nbsp; <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">&nbsp; &nbsp; &nbsp; <display-name>example</display-name>&nbsp; &nbsp; &nbsp; <servlet>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>mvc-dispatcher</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>&nbsp; &nbsp; &nbsp; &nbsp; <init-param>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <param-name>contextConfigLocation</param-name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>&nbsp; &nbsp; &nbsp; &nbsp; </init-param>&nbsp; &nbsp; &nbsp; &nbsp; <load-on-startup>1</load-on-startup>&nbsp; &nbsp; &nbsp; </servlet>&nbsp; &nbsp; &nbsp; <servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>mvc-dispatcher</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/</url-pattern>&nbsp; &nbsp; &nbsp; </servlet-mapping>&nbsp; &nbsp; &nbsp; <!-- Custom Servlet Mapping-->&nbsp; &nbsp; &nbsp; <servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>mvc-dispatcher</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/registrationapplication/*</url-pattern>&nbsp; &nbsp; &nbsp; </servlet-mapping>&nbsp; &nbsp; &nbsp; <servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>mvc-dispatcher</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/maintenance/lienholder/*</url-pattern>&nbsp; &nbsp; &nbsp; </servlet-mapping>&nbsp; &nbsp; &nbsp; <servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>mvc-dispatcher</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/login/*</url-pattern>&nbsp; &nbsp; &nbsp; </servlet-mapping>&nbsp; &nbsp; &nbsp; <servlet-mapping>&nbsp; &nbsp; &nbsp; &nbsp; <servlet-name>mvc-dispatcher</servlet-name>&nbsp; &nbsp; &nbsp; &nbsp; <url-pattern>/messagemap/*</url-pattern>&nbsp; &nbsp; &nbsp; </servlet-mapping>&nbsp; &nbsp; </web-app>您可以使用基于 Java 的配置或基于 xml 的配置。两者均受支持。您仍然可以使用旧配置。基于Java的配置将是这样的:public class DispatcherServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected Class<?>[] getRootConfigClasses() {&nbsp; &nbsp; &nbsp; &nbsp; return new Class[] { Configure.class };&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected Class<?>[] getServletConfigClasses() {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected String[] getServletMappings() {&nbsp; &nbsp; &nbsp; &nbsp; return new String[] { "/", "/registrationapplication", "/login", "/messagemap", "/maintenance/lienholder"};&nbsp; &nbsp; }}@Configuration@EnableWebMvc&nbsp;@ComponentScan(value = {"com.example"})public class Configure {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public InternalResourceViewResolver getInternalResourceViewResolver() {&nbsp; &nbsp; &nbsp; &nbsp; InternalResourceViewResolver resolver = new InternalResourceViewResolver();&nbsp; &nbsp; &nbsp; &nbsp; resolver.setPrefix("/WEB-INF/views/");&nbsp; &nbsp; &nbsp; &nbsp; resolver.setSuffix(".jsp");&nbsp; &nbsp; &nbsp; &nbsp; return resolver;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java