SpingBoot提供了一种快速使用Spring的方式,基于约定大于配置的思想。
SpringBoot的优点:自动配置,起步依赖(依赖传递),具有多种辅助功能
SpringBoot不是对Spring功能的增强,而是一种快速使用Spring的方式
SpringBoot入门案例
案例需求:Maven搭建SpringBoot工程,实现web的请求响应。浏览器访问在页面中输出'helloworld'。
实现步骤
1.首先创建一个空项目
2.在当前项目创建模块,Maven项目spring-boot
具体的建立模块,包的具体如下:
3.在pom.xml,导入SpringBoot起步依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>study_springboot_01</artifactId>
<version>1.0</version>
<!--spring boot 父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>
<!-- 设置起步依赖 就是一套解决方案 完成web工程-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4.编写SpringBoot启动类---该类在controller包的同级目录下
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
5.定义Controller
@RestController
public class HelloController{
@RequestMapper("/hello")
public String hello(String name ){
return "hello world!";
}
}
6.启动测试
在服务器访问http://localhost:8080/hello进行测试
总结:该案例的中的重点在于:
1.@SpringBootApplication注解代表是SpringBoot的启动类