当前位置: 首页 > news >正文

《SpringBoot+Vue》Chapter02_SpringBoot基础配置

不使用spring-boot-starter-parent

spring-boot-starter-parent主要提供了如下默认配置:

  • Java版本默认1.8
  • 编码格式默认使用UTF-8
  • 提供Dependency Management进行项目依赖的版本管理
  • 默认的资源过滤与插件配置
    可以使用Soringboot的dependencyManagement来代替使用springbootparent
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>3.0.0</version></dependency></dependencies></dependencyManagement>

这样就可以不用继承spring-boot-starter-parent,需要配置一下Java的版本、编码格式

 <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>

@SpirngBootApplication

由三个注解组成

  1. @SpringBootConfiguration
    这个注解是一个@Configuration所以@SpringBootConfiguration的功能就是表明这是一个配置类。开发者可以在这个类中配置Bean。
  2. @EnableAutoConfiguration
    表示开启自动化配置,SpringBoot中的自动化配置时非侵入的,在任意时刻,开发者都可以使用自定义配置代替自动化配置中的某一个配置
  3. @ComponentScan表示包扫描

Properties配置

SpringBoot中采用了大量的自动化配置。需要自己手动配置,承载这些自定义配置的文件就是resources目录下的application.properties文件
SpringBoot项目中的application.properties配置文件一共可以出现在如下位置:

  • 项目根目录下的config文件夹中
  • 项目根目录下
  • classpath下的config文件夹
  • classpath下
    优先级从1~4依次降低。SpringBoot按照这个优先级查找配置信息,加载到Spring Environment中。
    使用yml作为配置文件,优先级与properties一致


    优先级

类型安全配置属性

Spring提供了@Value注解以及EnvironmentAware接口欧来将SpringEnvironment中的数据注入到属性上,在application.properties中添加

book.name=bookName
book.author=luoguanzhong
book.price=100
@Component
@ConfigurationProperties(prefix = "book")
public class Book {private String name;private String author;private Integer price;
..省略getter/setter
}
  • @ConfigurationProperties中的prefix属性描述了要加载的配置文件的前缀
  • SpringBoot采用了宽松的规则来进行属性绑定,如果Bean中的属性名为authorName那么配置文件中的属性可以是 book.author_name、book.author-name、book.authorName
@RestController
public class BookController {@Autowiredprivate Book book;@GetMapping("/book")public String book() {return book.toString();}
}

YAML配置

常规配置

YAML是JSON的超集,简洁而强大,是专门用来书写配置文件的语言,可以替代application.properties。spring-boot-starter-web依赖会简介地引入了snakeyaml依赖,snakeyaml会实现对YAML配置的解析。

server:port: 8081servlet:context-path: /chapter02tomcat:uri-encoding: utf-8

复杂配置

YAML不仅可以配置常规属性,也可以配置复杂属性

book:name: yamlNameauthor: bookAuthorprice: 22
配置数组
book:name: yamlNameauthor: bookAuthorprice: 22kind:- a1- a2- a3

Book中

 private List<String> kind;@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", kind=" + kind +'}';}public List<String> getKind() {return kind;}public void setKind(List<String> kind) {this.kind = kind;}

Profile

创建配置文件

在resources目录下创建两个配置文件:application-dev.properties、application-prod.properties

配置application.properties

spring.profiles.active=dev

这个表示使用application-dev.properties配置文件启动项目,若将dev改为prod则表示使用application-prod.properties启动项目

最后编辑于:2024-10-04 14:46:12


喜欢的朋友记得点赞、收藏、关注哦!!!


http://www.mrgr.cn/news/56159.html

相关文章:

  • 【Linux】 Linux 释放内存脚本
  • 基于SSM大学校医院信息管理系统的设计
  • 本地图像转base64-cnblog
  • 第三章,使用DQL命令查询数据
  • 【STM32学习】PWM学习(四),散热风扇的控制,PWM调速调制,
  • QT中中文显示乱码问题
  • 暴力破解+宝塔+xp_CAPTCHA+WIN2012+DVMA暴力破解+BP-PY+CMS+PY-MG+BP识别XP
  • 初探Vue前端框架
  • AtCoder Beginner Contest 376(C,E题题解)
  • 接口性能优化的11个小技巧
  • 什么是高水位线
  • MySQL 基础查询
  • 数据通路(Data Path)
  • Mybatis中 使用#和$ 需要注意的点
  • 大模型学习路径,零基础入门到精通,收藏这篇就够了
  • Aloop虚拟声卡
  • wsl2配置网络代理,访问外网
  • Qt学习笔记(二)Qt 信号与槽
  • 华为HarmonyOS实现实时语音识别转文本
  • python将1格式化为01
  • k8s dockers 部署 k8s运行docker
  • 使用RRT算法进行路径规划的探索与优化
  • CodeQL和数据流分析的简介
  • 双十一有哪些值得购买的好物品?2024双十一超级好用的五款品牌分享
  • Qt开发笔记(一)Qt的基础知识及环境编译(泰山派)
  • 关于美团外卖霸王餐系统的详细介绍?你了解多少