SpringBoot
SpringBoot简介
- SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
Spring程序缺点
- 配置繁琐
- 依赖设置繁琐
SpringBoot程序优点
- 自动配置
- 起步依赖(简化依赖配置)
- 辅助功能(内置服务器,…)
原生开发SpringMVC程序过程
<dependencies><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId> <version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.10.RELEASE</version></dependency>
</dependencies>
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {protected Class<?>[] getRootConfigclasses() {return new Class[]{SpringConfig.class};}protected class<?>[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}protected String[] getServletMappings() {return new String[]{"/"};}
}
@Configuration
@ComponentScan("com.jihua.controller")
@EnablewebMvc
public class SpringMvcConfig {
}
@RestController
@RequestMapping( "/books" )
public class BookController {@Autowiredprivate BookService bookService;@GetMapping("/{id}")public Result getById(@PathVariable Integer id) {Book book = bookService.getById(id);Integer code = book != null ? code.GET_OK : Code.GET_ERR;String msg = book != null ?"":"数据查询失败,请重试! ";return new Result(code, book, msg);}
}
SpringBoot入门案列
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
- 选择当前模块需要使用的技术集
-
开发控制器类
@RestController @RequestMapping("/book") public class BookController {@GetMapping("/{id}")public String getBookById(@PathVariable String id) {System.out.println("id==>" + id);return "book id:" + id;} }
-
运行自动生成的Application类
- 测试功能正常
入门案例相关
最简SpringBoot程序所包含的基础文件
-
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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.9</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.jihua</groupId><artifactId>SpringDemo</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies> </project>
-
Application类
package com.jihua;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} }
Spring程序与SpringBoot程序对比
类/配置文件 | Spring | SpringBoot |
---|---|---|
pom文件中的坐标 | 手工添加 | 勾选添加 |
web3.0配置类 | 手工制作 | 无 |
Spring/SpringMVC配置类 | 手工制作 | 无 |
控制器 | 手工制作 | 手工制作 |
不使用IDEA如何创建SpringBoot项目?
-
进入SpringBoot官网
-
网站底部点击Spring Initializr
- 配置SpringBoot项目
- 点击
GENERATE
后开始下载项目
- 也可使用阿里云脚手架平台Cloud Native App Initializer (aliyun.com)
SpringBoot项目脱离IDEA运行
-
使用maven生命周期的package打包
- IDEA中默认生成路径为当前项目的target目录下
-
使用java命令启动项目
java -jar jar包文件名
注意:使用maven打包需要在pom文件中添加如下坐标:
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
- 使用该插件会同时打包相关依赖的jar包,并指定jar包的入口程序