HM-SpringBoot1.2【SpringBoot快速入门、快速构建SpringBoot工程、起步依赖原理分析】
SpringBoot快速入门
1、创建maven项目
2、导入springboot起步依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.haifei</groupId> 8 <artifactId>springboot1-helloworld</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <!--springboot工程需要继承的父工程--> 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.1.8.RELEASE</version> 16 </parent> 17 18 <dependencies> 19 <!--web开发的起步依赖--> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-web</artifactId> 23 </dependency> 24 </dependencies> 25 26 </project>
3、定义controller类
1 package com.haifei.controller; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 @RestController 7 public class HelloController { 8 9 @RequestMapping("/hello") 10 public String hello(){ 11 return "hello springboot !"; 12 } 13 14 }
4、编写引导类
1 package com.haifei; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 /** 7 * 引导类:SpringBoot项目的入口 8 */ 9 @SpringBootApplication 10 public class HelloApplication { 11 12 public static void main(String[] args) { 13 SpringApplication.run(HelloApplication.class, args); 14 } 15 16 }
5、启动测试
快速构建springboot工程
1、构建
2、自动生成的
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 5 <modelVersion>4.0.0</modelVersion> 6 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>2.5.3</version> 11 <relativePath/> <!-- lookup parent from repository --> 12 </parent> 13 14 <groupId>com.haifei</groupId> 15 <artifactId>springboot2-init</artifactId> 16 <version>0.0.1-SNAPSHOT</version> 17 <name>springboot2-init</name> 18 <description>Demo project for Spring Boot</description> 19 20 <properties> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-test</artifactId> 32 <scope>test</scope> 33 </dependency> 34 </dependencies> 35 36 <build> 37 <plugins> 38 <plugin> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-maven-plugin</artifactId> 41 </plugin> 42 </plugins> 43 </build> 44 45 </project>
1 package com.haifei.springboot2init; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class Springboot2InitApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(Springboot2InitApplication.class, args); 11 } 12 13 }
1 package com.haifei.springboot2init; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.boot.test.context.SpringBootTest; 5 6 @SpringBootTest 7 class Springboot2InitApplicationTests { 8 9 @Test 10 void contextLoads() { 11 } 12 13 }
3、手动编写controller进行测试
1 package com.haifei.springboot2init; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 @RestController 7 public class HelloController { 8 9 @RequestMapping("/hello") 10 public String hello(){ 11 return "hi springboot"; 12 } 13 14 }
4、其他
springboot起步依赖原理分析
1、spring-boot-starter-parent
2、spring-boot-starter-web