Spring boot 学习笔记
参考资料
https://spring.io/projects/spring-boot/#learn
官方文档和API
2.2.0 CURRENT GA | Reference Doc. | API Doc. |
---|---|---|
2.2.1 SNAPSHOT | Reference Doc. | API Doc. |
2.1.10 SNAPSHOT | Reference Doc. | API Doc. |
2.1.9 GA | Reference Doc. | API Doc. |
1.5.22 GA | Reference Doc. | API Doc. |
项目初始化
在线初始化:https://start.spring.io/
Eclipse 安装Spring Tools插件 或者下载Spring官方的STS整合版。
IntelliJ IDEA 安装Spring Assistant插件。
Spring Boot CLI初始化项目:
You can download the Spring CLI distribution from the Spring software repository:
开发系统要求
使用Spring Boot 2.1.9.RELEASE
JDK版本:
Java 8 and is compatible up to Java 12 (included).
Spring版本:
Spring Framework 5.1.10.RELEASE or above is also required.
构建工具版本:
Build Tool | Version |
---|---|
Maven | 3.3+ |
Gradle | 4.4+ |
Servlet 容器
Spring Boot supports the following embedded servlet containers:
Name | Servlet Version |
---|---|
Tomcat 9.0 | 4.0 |
Jetty 9.4 | 3.1 |
Undertow 2.0 | 4.0 |
You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.
第一个HelloWorld
参考官方文档10. Installing Spring Boot:
步骤:
-
新建一个基于Maven 的 Spring boot Java 工程
-
自动生成启动类
-
配置文件中修改默认端口
-
编写Controller类
-
运行程序
下面是一个典型的程序结构:
com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
|
+- repository
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java
Application.java 将声明 main 方法和 @SpringBootApplication
1.使用插件或者在线网页进行spring boot项目的初始化
-
jdk版本 8
-
maven项目
-
java语言
-
设置项目的路径等配置
2.选择spring boot版本 和 工具支持
-
创建spring web项目 就要勾选 Web>Spring Web
-
以后需要spring data jpa项目 就要勾选 SQL>Spring Data JPA -对应需要配置数据源
-
如此类推
3.自动生成程序入口启动类:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemo01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo01Application.class, args);
}
}
通常建议将应用的main类放到其他类所在包的顶层(root package),项目启动时自动扫描main启动类所在的根路径com.example.demo下所有带spring组件注解的类:
@Component -无法界定属于下面哪个具体的层的时候使用,例如:AOP、LOG、工具类…
@Repository - 持久层
@Service -业务层
@Controller -控制层
@RestController -控制层-Restful风格API设计
4.默认配置文件
src\main\resources\application.properties
可以修改下端口号防止端口占用:
server.port=8081
5.自定义一个web层类
package com.example.demo.hello.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController
{
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}
}
6.启动项目
选中 SpringBootDemo01Application.java文件 Run as…>Spring Boot App
启动成功后打开浏览器访问:http://localhost:8081/hello
7.Maven介绍
Maven用户可以继承 spring-boot-starter-parent 项目来获取合适的默认设
置。该parent项目提供以下特性:
-
默认编译级别为Java 1.6
-
源码编码为UTF-8
-
一个Dependency management节点,允许你省略常见依赖的 标
签,继承自 spring-boot-dependencies POM。 -
恰到好处的资源过滤
-
恰到好处的插件配置(exec插件,surefire,Git commit ID,shade)
-
恰到好处的对 application.properties 和 application.yml 进行筛选,
包括特定profile(profile-specific)的文件,比如 applicationfoo.
properties 和 application-foo.yml最后一点:由于配置文件默认接收Spring风格的占位符( ${…} ),所以Maven
filtering需改用 @…@ 占位符(你可以使用Maven属性 resource.delimiter 来覆
盖它)。
继承starter parent
如果你想配置项目,让其继承自 spring-boot-starter-parent ,只需
将 parent 按如下设置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/><