Spring Boot Hello World
入门案例
浏览器发送hello请求,控制器接受请求并且响应给浏览器 Hello World 字符串。
1、创建一个maven工程
2、导入spring boot相关的依赖
<!--springboot父依赖-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.7.RELEASE</version>
</parent>
<!--需要的起步依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、编写一个spring boot主程序启动spring boot项目
package com.pan;
import org.springframework.boot.SpringApplication;
/**
* User: 开封彭于晏
* ClassName:SpringBootApplication 这是一个springboot的启动类
* Package:com.pan
*
* @date:2021/9/15 17:02
* @author:383301644@qq.com
*/
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
4、创建 controller 控制器
//相当于@controller+@responebody的结合体 表示整个类都返回的是json格式的数据
@RestController
public class HelloController {
@RequestMapping("hello")
public String helloSpringBoot() {
return "helloSpringBoot";
}
}
5、默认访问出现的是Spring Boot的错误页面
6、简化部署
7、hello world探究
1、pom.xml文件
1、父项目
<!--父项目,他的引入的父项目是下面的 spring-boot-dependencies-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.7.RELEASE</version>
</parent>
<!--dependencies是真正管理springboot所有的版本依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
spring boot的版本仲裁中心: 我们导入依赖时默认不再需要导入依赖。注意:没有在dependencies中声明的依赖需要声明依赖版本。
2、导入的依赖
spring-boot-starter-web:
spring boot场景启动器,帮我们导入了web场景正常运行的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring boot将所有的工程场景都抽取出来做成一个个的(starters)启动器,著需要在项目中引入启动器相关的所有场景依赖都会带入进来。要用什么功能就导入什么场景启动器。
3、主程序类/主入口类
@SpringBootApplication
public class SpringBootApplication {
//运行main方法让项目启动
public static void main(String[] args) {
//SpringApplication.run(当前类.class文件,main方法中的可变参数 args);
SpringApplication.run(SpringBootApplication.class, args);
}
}
@SpringBootApplication:spring boot应用 该注解标注在某个类上说明该类是spring boot的主配置类。spring boot就应该运行这个类的main方法来启动spring boot应用程序。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)})
@SpringBootConfiguration: springboot的配置类
标注在某个类上就表示该类是一个配置类
@Configuration 配置类上标注这个注解。
配置类就相当于配置文件配置类也是容器中的一个组件
@EnableAutoConfiguration: 开启自动配置功能
springboot帮我们自动配置,这个注解就是告诉spring boot开启自动配置功能。这样spring boot才会帮我们自动配置。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
@AutoConfigurationPackage: 自动配置包
@Import(AutoConfigurationPackages.Registrar.class), spring的底层注解 @Import 给容器中导入一个组件。导入的组件由 AutoConfigurationPackages.Registrar.class 来指定导入那些组件。将主配置类(@SpringBootApplication注解标注的类)的所在包以及子包所有的组件扫描到spring容器中。