一、maven构建springboot
1、需求:
搭建SpringBoot工程,定义HelloController.hello()方法,返回”Hello SpringBoot!”。
2、实现步骤:
-
创建Maven项目 springboot-helloworld
-
导入SpringBoot起步依赖
1<!--springboot工程需要继承的父工程--> 2 <parent> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-parent</artifactId> 5 <version>2.1.8.RELEASE</version> 6 </parent> 7 8 <dependencies> 9 <!--web开发的起步依赖--> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 </dependencies>
-
定义Controller编写引导类
1@SpringBootApplication//表示这个类 是springboot主启动类。 2public class HelloApplication { 3 public static void main(String[] args) { 4 SpringApplication.run(HelloApplication.class,args); 5 } 6} -
定义Controller
1@RestController 2@RequestMapping("/hello") 3public class HelloController { 4 5 @GetMapping("/test") 6 public String test1(){ 7 return "Hello SpringBoot!"; 8 } 9} -
启动测试 访问: http://localhost:8080/hello/test
3、总结
-
启动springboot一个web工程
-
pom 规定父工程,导入web的起步依赖
-
主启动类 @SpringBootApplication、main
-
业务逻辑 controller,service,dao
- SpringBoot在创建项目时,使用jar的打包方式。 java -jar xxx.jar
-
SpringBoot的引导类,是项目入口,运行main方法就可以启动项目。
-
使用SpringBoot和Spring构建的项目,业务代码编写方式完全一样。
二、快速构建springboot




三、SpringBoot起步依赖原理分析-理解
- 在spring-boot-starter-parent中定义了各种技术的版本信息,组合了一套最优搭配的技术版本。
- 在各种starter中,定义了完成该功能需要的坐标合集,其中大部分版本信息来自于父工程。
- 我们的工程继承parent,引入starter后,通过依赖传递,就可以简单方便获得需要的jar包,并且不会存在版本冲突等问题。
