SpringBoot1.5x upgrade to 2.x analysis

springboot 2.x requires at least JDK support 8, there are many ways 2.x application of many advanced new features of JDK 8, so you have to upgrade to version 2.0, make sure your application must be compatible with JDK 8.
In addition, 2.x beginning of support for JDK 9.

Third-party libraries upgrades
2.x for third-party libraries upgraded all stable version can upgrade, upgrade some interesting libraries I listed.

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

Related rely more on version Reference:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml

  1. Start class error

Problem:
Start class SpringBootServletInitializer marked red error, not the imported class.
The reason:
the Spring the Boot deployed to Tomcat need to add SpringBootServletInitializer start time to start the class, the difference between 2.0 and 1.0.
solution:

    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. Profiles error

Question:
profile project name configuration error: server.context-path: / spring
reasons:
A large number of Servlet dedicated server * properties was moved to the next server.servlet:
image.png
It can be seen some clues, that is, server is no longer the only servlet, there are others to join.

Solution:
server.context-path: / Spring into server.servlet.context-path: / spring either

  1. Web Starter as transitive dependencies

Question:
formwork using a thymeleaf, start error prompt can not find the spring-boot-starter-web
reason:
before there are several Spring Boot starter is spring-boot-starter-web relies on Spring MVC and delivery. Under the new support Spring WebFlux, spring-boot-starter -mustache, spring-boot-starter-freemarker and spring-boot-starter-thymeleaf no longer rely on it. Developer is responsible for selecting and adding spring-boot-starter-web or spring-boot-starter-webflux.
Solution:
Import spring-boot-starter-web can

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

  
  
  1. Thymeleaf 3.0 does not include the default layout module

Problem:
start the project, it was found home blank, background check and there is no error message
reasons:
the Spring 2.0 in the Boot spring-boot-starter-thymeleaf package does not contain the default layout module, you need the time to add separately.
solution:

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

  
  
  1. Interceptor outdated

Question:
After the upgrade, WebMvcConfigurerAdapter tips outdated
reasons:
springBoot upgraded, using the characteristics of the default method java8, so this interface can be implemented directly WebMvcConfigurer.
Solution:
Old:
public class MyWebMvcConfigurerAdapter the extends WebMvcConfigurerAdapter
new:
public class MyWebMvcConfigurerAdapter the implements WebMvcConfigurer

  1. Static resource is intercepted

Question:
When accessing the system login style is not loaded
Cause: The
1.5 version when META-INF / resources / resources / static / public static position is spring boot believe resources should be placed, will automatically look for static resources, and in the spring boot 2.0 when the static resources have also been intercepted, when the interceptor to intercept the request, but the controller and there is no corresponding request, the request will be treated as a request for static resources. At this point the handler is ResourceHttpRequestHandler, it throws the above error.
Solution:
The solution is to exclude the request path interceptor where static resources

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

// addPathPatterns to add blocking rules
// excludePathPatterns user to exclude interception
registry.addInterceptor (new new myInterceptor ()). AddPathPatterns ( "/ ") .excludePathPatternss ( "/ toLogin", "/ the Login", "/ Assets / ", " / JS / ** ");
}

assets that I put static files directory image.png

  1. Global exception special treatment

Question:
Previous mentioned some mistakes you might want to be treated special, and now corresponds to the code marked red, the class can not find the corresponding
reason:
the new version of the method to remove the need to replace a new approach to
solutions :
old Code:

@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);
}
}

After being treated for more than a few mistakes, you can start a project, there are other hidden errors encountered subsequent replenishment.

Summarize
this, the corresponding code to upgrades to the Spring Boot 2.x, you can also try to look at junior partner, although there are some pit, but others are estimated with the time to fill up, follow-up on a Spring Experience new features Boot 2.x it.

Reprinted: 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>
Published 45 original articles · won praise 0 · Views 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);
	}
}

Guess you like

Origin blog.csdn.net/qq_44813090/article/details/104218948