Spring Boot 基础学习笔记
Spring Boot 基础学习笔记一、Spring Boot 概述1. 定义Spring Boot 是 Pivotal 团队基于 Spring 框架开发的快速开发脚手架核心宗旨是简化 Spring 应用的初始化搭建和开发流程通过「约定优于配置」的思想大幅减少 XML 配置和繁琐的依赖管理工作让开发者聚焦业务逻辑。2. 解决的核心问题消除 Spring 应用繁琐的 XML 配置和手动依赖管理内嵌 Web 服务器Tomcat/Jetty/Undertow应用可直接打包为 JAR 包运行无需手动部署基于类路径依赖自动配置 Spring 容器自动配置降低配置成本统一的依赖版本管理避免版本冲突问题。二、Spring Boot 核心特性特性说明约定优于配置内置默认配置如默认端口 8080仅需在约定不满足时做少量自定义配置起步依赖Starter官方封装的依赖集合如spring-boot-starter-web一键引入相关依赖自动管理版本自动配置根据类路径依赖、环境变量等自动初始化 Bean可通过exclude排除不需要的配置内嵌服务器内置 Tomcat默认、Jetty 等无需外部容器即可启动应用无 XML 配置基于注解驱动开发完全摒弃 XML 配置也可兼容Actuator可选提供应用健康检查、指标监控等运维能力三、Spring Boot 入门程序HelloWorld1. 开发环境JDK1.8Spring Boot 2.x/ 17Spring Boot 3.x构建工具Maven/Gradle示例用 MavenIDEIntelliJ IDEA / Eclipse2. 实现步骤1配置 Maven 依赖pom.xml?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!-- 父工程核心依赖版本管理 --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.18/version!-- 稳定版本按需选择 --relativePath//parentgroupIdcom.example/groupIdartifactIdspringboot-hello/artifactIdversion1.0-SNAPSHOT/version!-- 核心依赖web场景包含Spring MVC 内嵌Tomcat --dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies!-- 打包插件生成可执行JAR包 --buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2编写主启动类packagecom.example;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/** * 主启动类Spring Boot应用入口 * SpringBootApplication复合注解包含 * - SpringBootConfiguration标记配置类 * - EnableAutoConfiguration开启自动配置 * - ComponentScan扫描当前包及子包的组件 */SpringBootApplicationpublicclassHelloApplication{publicstaticvoidmain(String[]args){// 启动Spring Boot应用SpringApplication.run(HelloApplication.class,args);}}3编写控制器Controllerpackagecom.example.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;/** * RestController复合注解 Controller ResponseBody * 作用返回字符串/JSON而非视图页面 */RestControllerpublicclassHelloController{// 映射GET请求访问路径/helloGetMapping(/hello)publicStringhello(){returnHello Spring Boot!;}}4运行与测试方式1直接运行主启动类的main方法方式2Maven 打包后运行mvn clean package# 打包生成JAR包java-jarspringboot-hello-1.0-SNAPSHOT.jar# 运行JAR包测试浏览器访问http://localhost:8080/hello页面显示Hello Spring Boot!。四、Spring Boot 配置文件1. 配置文件基础作用覆盖默认配置、自定义应用参数如端口、应用名类型与优先级application.properties高 application.yml/application.yaml低位置默认放在src/main/resources目录下。1application.properties键值对格式# 服务器端口 server.port8081 # 应用名称 spring.application.namespringboot-hello # 自定义参数 user.name张三 user.age202application.ymlYAML 层级格式# 注意YAML中冒号后必须加空格层级用2个空格缩进禁止用Tabserver:port:8081spring:application:name:springboot-hello# 自定义参数user:name:张三age:202. 读取配置文件参数方式1Value 注解单个参数读取packagecom.example.controller;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;RestControllerpublicclassConfigController{// 读取配置中的user.nameValue(${user.name})privateStringuserName;// 读取配置中的user.age指定默认值配置无该参数时生效Value(${user.age:18})privateIntegeruserAge;GetMapping(/config)publicStringgetConfig(){return姓名userName年龄userAge;}}方式2ConfigurationProperties批量绑定推荐步骤1编写实体类绑定配置packagecom.example.entity;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;/** * Component纳入Spring容器管理 * ConfigurationProperties绑定配置前缀prefix user 对应yml中的user节点 */ComponentConfigurationProperties(prefixuser)publicclassUser{privateStringname;privateIntegerage;// 必须生成getter/setter否则无法绑定值publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.namename;}publicIntegergetAge(){returnage;}publicvoidsetAge(Integerage){this.ageage;}OverridepublicStringtoString(){returnUser{namename\, ageage};}}步骤2注入并使用实体类packagecom.example.controller;importcom.example.entity.User;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;RestControllerpublicclassUserController{AutowiredprivateUseruser;GetMapping(/user)publicStringgetUser(){returnuser.toString();}}3. 多环境配置开发/测试/生产实际开发中需区分环境Spring Boot 支持多环境配置文件命名规则application-{环境名}.yml/properties。示例开发环境application-dev.ymlserver:port:8080spring:application:name:springboot-hello-dev生产环境application-prod.ymlserver:port:80spring:application:name:springboot-hello-prod激活指定环境在主配置文件application.yml中spring:profiles:active:dev# 激活开发环境改为prod则切换到生产环境五、核心总结Spring Boot 核心是「约定优于配置」通过起步依赖和自动配置简化开发流程主启动类 SpringBootApplication是应用入口需放在根包下保证组件扫描范围配置文件支持properties键值对和yml层级清晰多环境配置通过spring.profiles.active激活读取配置参数Value适合单个参数ConfigurationProperties适合批量绑定推荐。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2471018.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!