The difference between SpringBoot 1.x and 2.x
Spring Boot relies on Spring, and Spring Cloud relies on Spring Boot, so the release of Spring Boot 2.0 officially integrates many features of Spring 5.0, and the latest version of Spring Cloud also needs to be released later. Integrate the latest Spring Boot2.0 content.
New features of SpringBoot2
-
Based on Java 8, supporting Java 9
means that the minimum version of Spring Boot 2.0 is JDK8 -
Reactive programming uses Spring WebFlux/WebFlux.fn to provide responsive Web programming support. Webflux is a brand new non-blocking functional Reactive Web framework that can be used to build asynchronous, non-blocking, event-driven services, in terms of scalability The performance is very good, this feature comes from Spring5.0. Spring Boot 2.0 also provides automatic configuration of reactive programming, such as: Reactive Spring Data, Reactive Spring Security, etc. -
HTTP/2 support HTTP/2 is already supported
in Tomcat, Undertow and Jetty -
Support for Kotlin
Introduces support for Kotlin 1.2.x and provides a runApplication function that allows you to run Spring Boot applications through the idiomatic Kotlin. -
The new actuator architecture The
new actuator architecture supports Spring MVC, WebFlux and Jersey -
Support Quartz
Spring Boot 1.0 does not provide support for Quartz. Various integration schemes have appeared before. Spring Boot 2.0 provides the simplest integration method. -
Security
greatly simplifies security automatic configuration -
In terms of Metrics
Metrics, Spring Boot 2 introduces Micrometer to unify the specifications of metrics, so that developers can better understand and use the metrics module without worrying about the specific storage of the docking. -
In terms of monitoring,
Spring Boot 2 enhances the integration of Micrometer. RabbitMQ, JVM threads and garbage collection metrics will be automatically monitored by instrument, and asynchronous controllers will also be automatically added to the monitoring. Through integration, the InfluxDB server can also be monitored. -
In terms of data
db, HikariCP is introduced by default, replacing the previous tomcat-pool as the underlying database connection pool. Compared with tomcat-pool, HikariCP has better performance. In short, it improves the access speed of db and supports JOOQ. For Redis, Lettuce is introduced by default, which replaces the previous jedis as the underlying redis link method. MongoDB\Hibernate optimization -
Thymeleaf3
Spring Boot 2 supports Thymeleaf 3. The performance improvement of Thymeleaf 3 compared to Thymeleaf 2 is not a little bit, because the performance of 2.0 is really not good, and it also uses a new page parsing system. -
OAuth 2.0
also adds support for OAuth 2.0, making it more friendly for developers to use spring-security to complete the development of permission modules
The design concept is
completely in the form of annotations
Core idea
- Can help developers to quickly integrate third-party frameworks (principle: Maven dependency packaging, starter)
- Remove the xml configuration and use annotations (principle: built-in annotations in the Spring system)
- No need for external Tomcat, internal server implementation (principle: Java language supports the creation of a Tomcat server, and then the local class file is handed over to tomcat for loading)
Let's first understand how to write by the built-in tomcat server. (SpringMVC+Tomcat annotation start)
Let's create a SpringBoot project first, find the maven dependency of spring-boot-starter-web in it, and we can click into it to see
that there is a starter about tomcat. Let's take a look at what configuration
has been introduced inside. There will be such a core package. Then we can also introduce this core package.
Before manually starting tomcat, we need to configure the annotation form to start MVC. As mentioned in the SpringMVC source code analysis-basic knowledge (2) , I won't repeat it here. Go directly to the code.
SpringMVC+Tomcat annotation startup project code: SpringMVC_Tomcat
SpringMVCConfig.java
package com.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @author 龙小虬
* @date 2021/3/20 17:31
*/
@Configuration
@EnableWebMvc
@ComponentScan("com.controller")
public class SpringMVCConfig {
}
MyWebApplicationInitializer.java
package com.initializer;
import com.config.SpringMVCConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
/**
* @author 龙小虬
* @date 2021/3/20 17:34
*/
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(annotationConfigWebApplicationContext);
ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcherServlet", dispatcherServlet);
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
}
Write a controller yourself to access
MyController.java
package com.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 龙小虬
* @date 2021/3/20 17:44
*/
@RestController
public class MyController {
@RequestMapping("/test")
public String test(){
return "tesat";
}
}
Now we begin to configure tomcat to
create Application.java and write the main function
package com;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import java.io.File;
/**
* @author 龙小虬
* @date 2021/3/20 18:20
*/
public class Application {
public static void main(String[] args) throws Exception {
//创建Tomcat服务器
Tomcat tomcatServer = new Tomcat();
//指定端口号
tomcatServer.setPort(8080);
//读取项目路径
StandardContext ctx = (StandardContext) tomcatServer.addWebapp("/", new File("src/main").getAbsolutePath());
//禁止重新载入
ctx.setReloadable(false);
//class文件读取地址
File additionWebInfClasses = new File("target/classes");
//创建webroot
WebResourceRoot resources = new StandardRoot(ctx);
//tomcat内部读取class执行
resources.addPreResources(new DirResourceSet(resources, "/target/classes", additionWebInfClasses.getAbsolutePath(), "/"));
tomcatServer.start();
//异步等待请求执行
tomcatServer.getServer().await();
}
}
The notes are all above, we just need to understand.
Run, you will find that the following error is
probably meaning that jsp is not supported, then we directly add the configuration in the pom
<!-- Tomcat对jsp支持 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.16</version>
</dependency>
Just run it again.
You can see this folder, which is the running data of tomcat.
Well, we understand how springboot built-in tomcat method, let's take a look again, how to quickly integrate third-party frameworks, that is, custom starter.
Before customizing the starter, let me talk about the starter customization rules. In the naming convention, spring-boot-starter-XXX is the official starter defined by SpringBoot. If XXX-spring-boot-starter is the starter defined by a third party, it is unofficial of. For example, the starte of mybatis
now let us define a token-redis-spring-boot-starter
project address: token-redis-spring-boot-starter to
create a SpringBoot project. And introduce pom configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
Create TokenProperties.java
package com.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author 龙小虬
* @date 2021/3/20 18:32
*/
@ConfigurationProperties(prefix = "lxq")
public class TokenProperties {
private String host;
private String pwd;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "TokenProperties{" +
"host='" + host + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
TokenService.java
package com.service;
import com.properties.TokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author 龙小虬
* @date 2021/3/20 18:36
*/
public class TokenService {
@Autowired
TokenProperties tokenProperties;
public String getToken(){
return "result:" + tokenProperties.toString();
}
}
TokenAutoConfiguration.java
package com.config;
import com.properties.TokenProperties;
import com.service.TokenService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 龙小虬
* @date 2021/3/20 18:34
*/
@EnableConfigurationProperties(TokenProperties.class)
@Configuration
public class TokenAutoConfiguration {
@Bean
public TokenService tokenService(){
return new TokenService();
}
}
After writing, you will find
such a warning. He probably means that no processor is found, and this processor is used for prompting in application.yml. We introduce this configuration
<!-- 能够给开发者引入该jar包后 有一定提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
His role is similar to
this prompt.
After solving this prompt, we also need to configure an EnableAutoConfiguration to
add dataorg.springframework.boot.autoconfigure.EnableAutoConfiguration=com.config.TokenAutoConfiguration
Based on this, we have finished writing a custom starter.
Go to this project from the command window and run the command mvn clean install.
If it prompts that the mvn command cannot be found , we have to find our own maven folder, configure global variables, and configure maven's bin directory in the path environment variable, such as
.
Now we can directly introduce and
create new projects. Import and use.
Initial understanding of SpringBoot startup When
we use SpringBoot, we will use @SpringBootApplication
this annotation. Go in and see.
Let’s take a look. If we @SpringBootConfiguration
don’t see anything special, there is one @Configuration
. Why should @SpringBootConfiguration
we pack it in China?
In fact, it is just to facilitate the addition of third-party jar packages in main and to manage them.