Springboot核心
一、是什么
SpringBoot:基于Spring框架的快速开发脚手架,自动配置、开箱即用,不用繁琐XML配置,快速开发微服务、后台接口。
二、核心优势
-
自动配置:约定大于配置,自动加载Spring组件
-
内嵌容器:内置Tomcat/Jetty,不用单独部署Tomcat
-
依赖管理:父工程统一版本,无需手动管版本冲突
-
极简开发:几行代码就能写一个Web接口
-
天然适配微服务:SpringCloud 底层基石
三、核心注解(必记)
• @SpringBootApplication:启动类主注解,整合自动配置、包扫描
• @RestController:接口控制器,返回JSON
• @RequestMapping/@GetMapping/@PostMapping:接口路径映射
• @Autowired:依赖注入
• @Configuration:自定义配置类
• @Bean:手动注册Bean到容器
• @Value:读取配置文件参数
四、快速写一个HelloWorld接口
- 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2. 控制器接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello SpringBoot!";
}
}
启动项目,访问:http://localhost:8080/hello 即可。
五、核心配置
配置文件两种:
• application.properties(老式)
• application.yml(缩进格式,常用)
示例 yml:
server:
port: 8081 # 修改端口
spring:
application:
name: demo-service
六、常用集成
• SpringBoot + MyBatis/MyBatis-Plus 操作数据库
• SpringBoot + Redis 缓存
• SpringBoot + RabbitMQ/Kafka 消息队列
• SpringBoot + SpringCloud 微服务
更多推荐
所有评论(0)