1、springboot与spring framework 的与别是什么?
springboot 就是使用了spring framework的核心组件,然后添加自动装配的功能,可以理解为springboot就是封装了spring framework,致力于快速构建spring framework项目。
2、springboot 初体验
2.1、我们在IDEA中使用Spring Initializer 来快速构建SpringBoot项目。
2.2、构建完成后项目的结构如下:
其中SpringBootBaseusedApplication是整个SpringBoot的启动类,启动类的原理后面我们会分析:
@SpringBootApplication
public class SpringBootBaseusedApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootBaseusedApplication.class, args);
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
创建的项目会默认继承spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzy</groupId>
<artifactId>spring-boot-baseused</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-baseused</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
依赖spring-boot-starter这个包,这个包会引入自动配置包spring-boot-autoconfigure、spring-core等基础包。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
依赖测试包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
默认会添加sprongboot 的maven 打包插件,默认打jar包。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、整合springmvc的启动包
我们上面创建的项目不会依赖springMVC相关的依赖,因此需要手动添加SpringMVC的启动包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
引入这个SpringMVC的启动包我们可以发现我们依赖了spring-webmvc、spring-web的依赖包。
4、配置tomcat的端口
映入SpringMVC的启动包后,我们可以配置tomcat的端口,是因为springboot 默认嵌入了tomcat,我们在配置文件application.properties中配置一下端口:不配置默认是8080
server.port=8080
5、编写一个Controller进行测试
@RestController
public class TestController {
@RequestMapping("test")
public String test(){
return "test success";
}
}
启动项目,直接运行启动类SpringBootBaseusedApplication中的main方法输出如下表示成功启动:
使用浏览器进行测试:http://localhost:8080/test 结果如下:
6、以上就是springboot 的初体验,整体来说很简单,这符合springboot的特性快速构建,至于整个和其他框架如mybatis、jpa等在后面实战的时候会有,接下来我们会开始分析springboot的原理,原理清除了的话,不管继承什么框架,你会发现其实很简单。