Spring Boot 2.0.0.RELEASE 需要Java 8 or 9 and Spring Framework 5.0.4.RELEASE 或者更高版本. 支持Maven 3.2+和Gradle 4 版本
支持的Servlet容器
Tomcat 8.5壳澳、Jetty 9.4、Jetty 9.4
官方版本遷移wiki
https://github.com/spring-projects/spring-boot/wiki
HelloWorld異同項(xiàng)目分析
1. 修改 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>springboot-mybatis</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 修改啟動(dòng)類
package springbootmybatis.springbootmybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
@RequestMapping("/")
String home() {
return "Hello World!";
}
}
代碼中三個(gè)注解分析
兩個(gè)MVC注解:
- @RestController 是Spring處理web請(qǐng)求時(shí)用到的注解茫经。告訴Spring將結(jié)果字符串直接呈現(xiàn)給調(diào)用者巷波。
- @RequestMapping 提供了“路由”信息,將
/
請(qǐng)求映射給home()方法卸伞。
SpringBoot注解:
- @EnableAutoConfiguration 這個(gè)注解告訴Springboot 根據(jù)你添加的jar包來猜測配置Spring 抹镊。@SpringBootApplication一樣,也可以使用exclude屬性來禁用不需要自動(dòng)配置的應(yīng)用荤傲。例子:
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
那么垮耳,它和@SpringBootApplication注解有什么關(guān)系呢?
我們來看下@SpringBootApplication注解源碼
所以遂黍,@SpringBootApplication注釋是相當(dāng)于使用@Configuration终佛, @EnableAutoConfiguration以及@ComponentScan與他們的默認(rèn)屬性。
通俗的講:@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan 雾家。前提是默認(rèn)配置铃彰,當(dāng)然,如果你想要覆蓋默認(rèn)配置芯咧,你就需要重寫該注解了牙捉,重寫也很簡單,給注解加上參數(shù)就好敬飒。