SpringBoot编程概述
概述
Spring Boot helps you to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
springboot框架=SpringMVC+Spring
开发SpringBoot条件
- springboot 项目中必须在
src/main/resources
中放入application.yml
或者application.properties
核心配置文件,并且名称必须为:application
- springboot 项目中必须在
src/main/java
中所有子包之外构建全局入口类,xxxApplication.java
,并且入口类有且只有一个
第一个工程创建
引入依赖
1
2
3
4
5
6
7
8
9
10
11
12
13<!--集成springboot父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<!--引入springboot的web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>创建配置文件
1
src/main/resources/application.yml
开发入口类
Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package com.buubiu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* @author buubiu
**/
//作用:开启自动配置 初始化Spring环境和SpringMVC环境
//作用:用来扫描相关注解 扫描范围:默认当前入口所在的包及其子包
public class Application {
public static void main(String[] args) {
//springApplication spring应用类 作用:用来启动Springboot应用
//参数1:传入入口类 类对象
//参数2:main函数的参数
SpringApplication.run(Application.class, args);
}
}说明:注解@SpringBootApplication 相当于@EnableAutoConfiguration 和 @ComponentScan 所以上面的代码也可以简化为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package com.buubiu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication
* 组合注解:相当于@EnableAutoConfiguration 和 @ComponentScan
*
* @author buubiu
**/
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}HelloController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package com.buubiu.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author buubiu
**/
public class HelloController {
public String hell() {
System.out.println("hello springboot");
return "hello springboot";
}
}
springboot 相关注解说明
1 |
|
SpringBoot编程概述