SpringMVC项目转SpringBoot

SpringMVC项目转SpringBoot

  • 前言
    • 1、更改依赖
      • mvc依赖文件
      • springboot依赖文件
    • 2、更改配置文件
    • 3、更改资源目录,设置WebMvcConfig
      • 将webapp目录下的资源放进resources目录下
      • 删除web.xml,并配置webmvcconfig文件来替代web.xml
    • 4、添加启动类
  • 目录结构:

前言

我写的第一个项目终于迎来了尾声,将spirngmvc转成成spirngboot可能是这个项目的最后一次重构了。在期间遇见了一些问题,随着问题的攻破也收获了很多宝贵经验,用此文章分享一下我的项目重构过程。

1、更改依赖

mvc依赖文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>net.wlgzsgroupId>
    <artifactId>Diet_HomeartifactId>
    <version>1.0-SNAPSHOTversion>


    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>


    <packaging>warpackaging>
    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-mailartifactId>
            <version>2.2.6.RELEASEversion>
        dependency>


        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.4version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>com.alipay.sdkgroupId>
            <artifactId>alipay-sdk-javaartifactId>
            <version>4.10.90.ALLversion>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.32version>
        dependency>

        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.2.3version>
            <scope>compilescope>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.5version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.46version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.1version>
        dependency>


        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.12version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.12.2version>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.2.1.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>aliyun-spring-boot-dependenciesartifactId>
                <version>1.0.0version>
                <type>pomtype>
                <scope>importscope>
            dependency>

        dependencies>
    dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.1version>
                <configuration>
                    <port>80port>
                    <path>/path>
                configuration>
            plugin>
        plugins>
    build>
project>

springboot依赖文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.12version>
        <relativePath/> 
    parent>
    <groupId>net.wlgzsgroupId>
    <artifactId>springBootMybatisartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>springBootMybatisname>
    <description>springBootMybatisdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>com.aliyungroupId>
            <artifactId>aliyun-java-sdk-coreartifactId>
            <version>4.4.5version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-mailartifactId>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>aliyun-oss-spring-boot-starterartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

        <dependency>
            <groupId>com.alipay.sdkgroupId>
            <artifactId>alipay-sdk-javaartifactId>
            <version>4.10.90.ALLversion>
        dependency>

        <dependency>
            <groupId>org.apache.tikagroupId>
            <artifactId>tika-coreartifactId>
            <version>1.24.1version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.8version>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        <dependency>
            <groupId>com.mysqlgroupId>
            <artifactId>mysql-connector-jartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>javax.activationgroupId>
            <artifactId>activationartifactId>
            <version>1.1version>
        dependency>
        <dependency>
            <groupId>javax.mailgroupId>
            <artifactId>mailartifactId>
            <version>1.4version>
        dependency>


        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.4version>
        dependency>


    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

注:由于依赖过多影响篇幅,删去一些

springboot只是对依赖进行了版本控制,但并不会将依赖全部导入,需要哪些依赖还需手动添加。

2、更改配置文件

将原来的spirng.xml、springmvc.xml等配置文件删除,添加application.yml文件:
SpringMVC项目转SpringBoot_第1张图片
SpringMVC项目转SpringBoot_第2张图片
根据spring、springmvc中的配置对application配置进行修改。
以下是部分application配置文件:


spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
    hiddenmethod:
      filter:
        enabled: true
    static-path-pattern: /static/** 
  thymeleaf:
    prefix: classpath:/templates/ # thymeleaf解析器前缀
    suffix: .html # thymeleaf解析器后缀
  servlet:
    multipart:
      max-file-size: 10MB # 设置文件上传的最大大小
      max-request-size: 100MB # 设置请求的最大大小
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    default-statement-timeout: 3000
    map-underscore-to-camel-case: true # 开启驼峰转下划线
server:
  port: 8080 # 端口
  servlet:
    context-path: /Diet # 项目名
    encoding:
      charset: UTF-8
  reactive:
    session:
      timeout: 30m # session最大保留时间

3、更改资源目录,设置WebMvcConfig

将webapp目录下的资源放进resources目录下

SpringMVC项目转SpringBoot_第3张图片
SpringMVC项目转SpringBoot_第4张图片

删除web.xml,并配置webmvcconfig文件来替代web.xml

@Configuration //继承WebMvcConfigurer接口
public class MySpringMvcConfigurer implements WebMvcConfigurer {
	//视图解析器
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/home");
    }
	//配置ResourceHandler访问静态资源
	//笔者此处是为了解决swagger-ui被莫名拦截的问题,无此问题的小伙伴可以跳过
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
	//添加登录拦截器、管理员拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**","/home","/login","/","/users/login"
                ,"/register","/users/register","/users/initUserInfo","/mainPicture/getMainPictures"
                ,"/RecipeControl/ToRecipe","/notes/toNoteById","/getMenuToMeduMainPage","/note"
                ,"/users/personalSpace","/toMenu","/notes/getNote","/admins/**","/adminlogin"
                ,"/webjars/**","/v2/**","/swagger-ui.html/**","/users/sendCode","/mainPicture/**"
                ,"/swagger-resources/**","/comment/getComment");
        registry.addInterceptor(new AdminInterceptor())
                .addPathPatterns("/admins/**","/mainPicture/**")
                .excludePathPatterns("/mainPicture/getMainPictures","/admins/adminLogin");
    }

}

4、添加启动类

启动类位置应比项目文件高一级


@org.springframework.boot.autoconfigure.SpringBootApplication//启动类注解
@ComponentScan("net.wlgzs")//
@EnableSwagger2 //swagger注解,在生产环境可以删除
public class SpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplication.class,args);
    }

}

目录结构:

spirngboot:
SpringMVC项目转SpringBoot_第5张图片
springmvc:
SpringMVC项目转SpringBoot_第6张图片
springmvc转spirngboot并不是很难,因为spirngboot本就是为了简化开发而生。
(完)

你可能感兴趣的:(spring,boot,后端,java,spring,maven)