mirror of
https://github.com/Estom/notes.git
synced 2026-05-11 19:16:31 +08:00
2.7 KiB
2.7 KiB
1 创建一个springboot项目
- 创建Maven工程
- 引入依赖pom.xml
- 包括
<parent>下是springboot的标签。dependencies下是相关的依赖。
- 包括
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 创建主程序
/**
* 主程序类
* @SpringBootApplication:这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
- 编写具体业务
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle01(){
return "Hello, Spring Boot 2!";
}
}
-
运行测试
-
简化配置 application.properties
server.port=8888
- 简化部署
- 把项目打包成jar包,直接在目标服务器程序执行即可。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2 工程结构
- 主程序Main.java
- 业务程序Controller.java
- maven依赖pom.xml
- 配置Springboot项目application.properties是Spring的集中配置中心,包括项目相关的所有配置。
3 依赖管理
-
mypom.xml
-
parent -- spring-boot-starter-parent
-
parent -- spring-boot-dependencies
-
几乎声明了所有的版本,查看Spring-boot-dependencies中的版本。可以自定义properties标签,修改版本号。
-
stater场景启动器。自动引入相关的所有依赖。可以自定义场景启动器,所有场景启动器最基本的以来。spring-boot-starter。引入依赖一部分可以不写版本号。引入非版本仲裁的版本号,必须要写。
4 自动配置
- 自动配好了SpringMVC
- 引入了SpringMVC全套组件
- 自动配好了SpringMVC常用功能。字符编码问题、多文件上传问题
- 默认程序结构
- 主程序所在包及其所有子包里的文件和组件都能被扫描到。无需配置包扫描
- 可以修改SpringbootApplication注解参数中的扫描路径。或者ComponentScan注解。
- .properties中的文件是绑定到具体的Java类的。这些类会在容器中创建指定的对象。
- 按需加载所有的自动配置,自动配置都在spring-boot-autoconfigure包中