SpringBoot1.5x 升级到2.x 分析

springboot 2.x 至少需要 JDK 8 的支持,2.x 里面的许多方法应用了 JDK 8 的许多高级新特性,所以你要升级到 2.0 版本,先确认你的应用必须兼容 JDK 8。
另外,2.x 开始了对 JDK 9 的支持。

第三方类库升级
2.x 对第三方类库升级了所有能升级的稳定版本,一些值得关注的类库升级我给列出来了。

  1. Spring Framework 5+
  2. Tomcat 8.5+
  3. Flyway 5+
  4. Hibernate 5.2+
  5. Thymeleaf 3+

相关更多依赖版本参考:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml

  1. 启动类报错

问题:
启动类SpringBootServletInitializer标红报错,导入的类不对。
原因:
Spring Boot 部署到 Tomcat 中去启动时需要在启动类添加SpringBootServletInitializer,2.0 和 1.0 有区别。
解决方案:

    import com.dudu.util.MyMapper;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import javax.sql.DataSource;
@SpringBootApplication
//启注解事务管理
@EnableTransactionManagement  // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@MapperScan(basePackages = "com.dudu.dao", markerInterface = MyMapper.class)

public class Application  extends SpringBootServletInitializer {

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

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
  1. 配置文件报错

问题:
配置文件中项目名称配置报错:server.context-path: /spring
原因:
大量的Servlet专属的server.* properties被移到了server.servlet下:
image.png
由此可以看出一些端倪,那就是server不再是只有servlet了,还有其他的要加入。

解决方案:
server.context-path: /spring改成server.servlet.context-path: /spring既可

  1. Web Starter 作为传递依赖

问题:
工程用的模板是thymeleaf,启动报错提示找不到spring-boot-starter-web
原因:
以前有几个 Spring Boot starter 是依赖于 Spring MVC 而传递的spring-boot-starter-web。在 Spring WebFlux 新的支持下,spring-boot-starter-mustache,spring-boot-starter-freemarker并spring-boot-starter-thymeleaf不再依赖它。开发者有责任选择和添加spring-boot-starter-web或spring-boot-starter-webflux。
解决方案:
导入spring-boot-starter-web既可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

  
  
  1. Thymeleaf 3.0 默认不包含布局模块

问题:
启动项目的时候发现首页空白,查看后台也没有任何的报错信息
原因:
Spring Boot 2.0 中spring-boot-starter-thymeleaf 包默认并不包含布局模块,需要使用的时候单独添加。
解决方案:

<dependency>
   <groupId>nz.net.ultraq.thymeleaf</groupId>
   <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

  
  
  1. 拦截器过时

问题:
升级后,WebMvcConfigurerAdapter提示过时
原因:
升级后的springBoot,使用了java8的特性 default 方法,所以直接实现 WebMvcConfigurer 这个接口即可。
解决方案:
旧:
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter
新:
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer

  1. 静态资源被拦截

问题:
访问系统的时候登录样式没有加载
原因:
1.5版本时候META-INF/resources / resources / static / public 都是spring boot 认为静态资源应该放置的位置,会自动去寻找静态资源,而在spring boot 2.0则对静态资源也进行了拦截,当拦截器拦截到请求之后,但controller里并没有对应的请求时,该请求会被当成是对静态资源的请求。此时的handler就是 ResourceHttpRequestHandler,就会抛出上述错误。
解决方案:
解决办法就是,在拦截器那里排除静态资源的请求路径

/**  
* 拦截器 
* @param registry 
*/ 
@Override 
public void addInterceptors(InterceptorRegistry registry) { 

// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/").excludePathPatternss("/toLogin","/login","/assets/","/js/**");
}

assets就是我放静态文件的目录 image.png

  1. 全局异常特殊处理

问题:
上一篇提到过的有些错误你可能想特殊对待处理的,现在对应代码标红,找不到对应的类
原因:
新版本后该方法去掉了,需要换成新的方法处理
解决方案:
旧代码:

@Configuration
    public class ContainerConfig {
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer(){
            return new EmbeddedServletContainerCustomizer(){
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
                    container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
                }
            };
        }
    }
//新代码:

@Configuration
public class ContainerConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage[] errorPages = new ErrorPage[2];
errorPages[0] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, “/error/500”);
errorPages[1] = new ErrorPage(HttpStatus.NOT_FOUND, “/error/404”);
registry.addErrorPages(errorPages);
}
}

暂时处理了以上几个错误后,项目就可以启动了,还有其他隐藏的错误后续遇到了再补充。

总结
到此,把教程对应的代码升级到Spring Boot 2.x了,各位小伙伴也可以尝试看看,虽然还有一些坑,但是估计用的时候别人都填上了,后续就一起来体验Spring Boot 2.x的新特性吧。

转载:http://tengj.top/2018/07/23/springboot2to1/

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count">1</span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/weixin_38008100">
                <img src="https://profile.csdnimg.cn/B/3/4/3_weixin_38008100" class="avatar_pic" username="weixin_38008100">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/2x/3.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/weixin_38008100" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">行走在江湖</a></span>
                                        </div>
                <div class="text"><span>发布了57 篇原创文章</span> · <span>获赞 94</span> · <span>访问量 6万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=weixin_38008100" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
发布了45 篇原创文章 · 获赞 0 · 访问量 3524

springboot 2.x 至少需要 JDK 8 的支持,2.x 里面的许多方法应用了 JDK 8 的许多高级新特性,所以你要升级到 2.0 版本,先确认你的应用必须兼容 JDK 8。
另外,2.x 开始了对 JDK 9 的支持。

第三方类库升级
2.x 对第三方类库升级了所有能升级的稳定版本,一些值得关注的类库升级我给列出来了。

  1. Spring Framework 5+
  2. Tomcat 8.5+
  3. Flyway 5+
  4. Hibernate 5.2+
  5. Thymeleaf 3+

相关更多依赖版本参考:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml

  1. 启动类报错

问题:
启动类SpringBootServletInitializer标红报错,导入的类不对。
原因:
Spring Boot 部署到 Tomcat 中去启动时需要在启动类添加SpringBootServletInitializer,2.0 和 1.0 有区别。
解决方案:

    import com.dudu.util.MyMapper;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import javax.sql.DataSource;
@SpringBootApplication
//启注解事务管理
@EnableTransactionManagement  // 启注解事务管理,等同于xml配置方式的 &lt;tx:annotation-driven /&gt;
@MapperScan(basePackages = "com.dudu.dao", markerInterface = MyMapper.class)

public class Application  extends SpringBootServletInitializer {

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

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

猜你喜欢

转载自blog.csdn.net/qq_44813090/article/details/104218948