SpringBoot3

1. 配置文件

1. 基本使用

  1. 使用
  • 配置文件classpath:application.properties
typescript 复制代码
spring.jdbc.driver=com.mysql.cj.jdbc.Driver
spring.jdbc.url=jdbc:mysql://localhost:3306/batis
spring.jdbc.username=root
spring.jdbc.password=123456
  • 使用配置文件的值:@Value("${key:defaultValue}")
typescript 复制代码
@Service
public class DataSourseTest {
    @Value("${spring.jdbc.driver}")
    private String driver;
    @Value("${spring.jdbc.url}")
    private String url;
    @Value("${spring.jdbc.username:root}")
    private String username;
    @Value("${spring.jdbc.password:123456}")	// 默认值
    private String password;
}
  1. 调用配置文件的顺序

    1. file:./config/ :首先在Spring Boot 当前工作目录下的 config 文件夹中查找。
      注意:如果没有找到,会继续找application.yml,如果这两个都没有找到,才会进入以下位置查找,以此类推。
    2. file:./ :如果在当前工作目录下config目录中找不到时,再从当前工作目录中查找。
    3. classpath:/config/ :如果从工作目录中找不到,会从类路径中找,先从类路径的 /config/ 目 录下寻找配置文件。
    4. classpath:/ :如果在 /config/ 下没有找到,它会在类路径的根目录下查找。
  2. 配置文件的合并

    1. properties文件的合并

      application-mysql.propertiesapplication-redis.properties两个文件合并到application.properties

    2. yml文件的合并

      application-mysql.ymlapplication-redis.yml两个文件合并到application.yml

yaml 复制代码
# application.properties
spring.config.import=classpath:application-mysql.properties,classpath:application-redis.properties

# application.yml
spring:
  config:
    import:
      - classpath:application-mysql.yml
      - classpath:application-redis.yml

2. 用于多环境切换

常用于区分开发(development)、测试(testing)、预生产(staging)和生产(production)等不同阶段的环境

  1. 开发环境的配置文件名一般叫做:application-dev.properties
yml 复制代码
spring.datasource.username=dev
spring.datasource.password=dev123
spring.datasource.url=jdbc:mysql://localhost:3306/dev
  1. 测试环境的配置文件名一般叫做:application-test.properties
properties 复制代码
spring.datasource.username=test
spring.datasource.password=test123
spring.datasource.url=jdbc:mysql://localhost:3306/test
  1. 预生产环境的配置文件名一般叫做:application-preprod.properties
properties 复制代码
spring.datasource.username=preprod
spring.datasource.password=preprod123
spring.datasource.url=jdbc:mysql://localhost:3306/preprod
  1. 生产环境的配置文件名一般叫做:application-prod.properties
properties 复制代码
spring.datasource.username=prod
spring.datasource.password=prod123
spring.datasource.url=jdbc:mysql://localhost:3306/prod
  • 第一种方式:在application.properties文件中添加这个配置:spring.profiles.active=prod
  • 第二种方式:在命令行参数上添加:--spring.profiles.active=prod

3. 配置文件属性值绑定到Bean

  • 配置文件:application.properties
java 复制代码
mydata.names=[tom,jerry]
mydata.adds-array[0].city=Beijing
mydata.adds-array[0].street=CaoYang
mydata.adds-array[1].city=ShangHai
mydata.adds-array[1].street=PuDong
mydata.adds-list[0].city=GuangZhou
mydata.adds-list[0].street=TianHe
mydata.adds-list[1].city=ShenZhen
mydata.adds-list[1].street=NanShan
mydata.adds-map.add1.city=HangZhou
mydata.adds-map.add1.street=XiHu
mydata.adds-map.add2.city=XiAn
mydata.adds-map.add2.street=ChangAn
  • 配置文件:yml格式
yaml 复制代码
mydata:
  names:
    - tom
    - jerry
  adds-array:
    - city: beijing
      street: chaoyang
    - city: shanghai
      street: pudong
  adds-list:
    - city: beijing
      street: chaoyang
    - city: shanghai
      street: pudong
  adds-map:
    add1:
      city: beijing
      street: chaoyang
    add2:
      city: shanghai
      street: pudong
  • 示例,属性值是复杂类型
java 复制代码
// 生命该类为配置类。配置类也会生成bean并纳入ioc管理
@Configuration
//@Component  // 使用Component可以代替Configuration
// 将配置文件绑定到类。prefix:前缀;此前缀的属性对应的类
@ConfigurationProperties(prefix = "mydata")
public class UserConfiguration {
  private String[] names; // 属性名必须与配置文件中的属性相同
  private Address[] addsArray;
  private List<Address> addsList;
  private Map<String, Address> addsMap;
  // set方法
}
java 复制代码
public class Address {
  private String city;
  private String street;
  // set方法
}

4. 配置文件的值赋值给第三方库中的Bean

  • 配置文件
java 复制代码
otherdata.address.city=beijing
otherdata.address.street=chaoyang
java 复制代码
@Configuration  
public class OtherClass {
  // 在注解@Configuration中使用@Bean标注该方法返回值是一个Bean,并纳入IoC管理
  @Bean
  @ConfigurationProperties(prefix = "otherdata.address")
  public Address address() {
    return new Address();
  }
}

2. Environment:获取环境配置

  • 环境配置:1. 正在使用的配置文件; 2. 操作系统的环境变量; 3. 操作系统的版本等
java 复制代码
@Autowired
  private Environment environment;
  @Test
  void getEnvironment(){
    // 直接使用这个环境对象,来获取环境信息,配置信息等。
    String[] activeProfiles = environment.getActiveProfiles();
    for (String activeProfile : activeProfiles) {
      System.out.println(activeProfile);
    }
    // 操作系统信息
    System.out.println(environment.getProperty("os.name"));	//Windows 11
    // 操作系统版本信息
    System.out.println(environment.getProperty("os.version")); // 10.0
    // 操作系统架构信息
    System.out.println(environment.getProperty("os.arch"));	// amd64
    // 获取临时目录
    System.out.println(environment.getProperty("java.io.tmpdir"));	// C:\Users\10539\AppData\Local\Temp\
    // 系统变量
    System.out.println(environment.getProperty("path"));
  }

3. Aop

xml 复制代码
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

4. Mybatis配置

1. mapper相关

yml 复制代码
# application.properties
# mapper.xml文件所在的目录
mybatis.mapper-locations=classpath:mapper/*.xml
# Bean类的别名,可将全限定类名省略为类名,在xml配置文件的resultType中使用
mybatis.type-aliases-package=org.example.learn.bean
# 数据库中下划线的列名与Bean中驼峰写法的映射
mybatis.configuration.map-underscore-to-camel-case=true

# application.yml
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: org.example.pojo
  configuration:
    map-underscore-to-camel-case: true

2. 配置文件中配置数据源

yml 复制代码
# springboot推荐的连接池:HikariDataSource
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/batis
spring.datasource.username=root
spring.datasource.password=123456

# application.yml
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/batis
    username: root
    password: 123456

3. mapper接口文件引入的两个方法:

  1. 给每个mapper接口添加注解:@Mapper
  2. 在springboot的入口程序添加注解以扫描包的方式添加mapper:@MapperScan("org.example.mapper")

5. 控制台logo设置

  • 关闭logo
yml 复制代码
spring.main.banner-mode=off
  • 使用自定义logo
    创建文件src/main/resources/banner.txt,展示文件中的图标
相关推荐
放学-别走13 小时前
基于springboot 以及vue前后端分离架构的求职招聘系统设计与实现
java·mysql·spring·vue·毕业设计·springboot·毕设
陌殇殇20 小时前
004 Kafka异常处理
中间件·kafka·springboot
在线打码1 天前
OpenAPI Generator:API开发的瑞士军刀
后端·jmeter·springboot·yaml·openapi
梦在旅途2 天前
事务中无法切换数据源?DataSourceSwitchInvoker:轻松实现多数据源切换执行工具类
springboot
小闫BI设源码3 天前
springboot005学生心理咨询评估系统(源码+数据库+文档)
毕业设计·springboot·javassm·管理系统源码·web网站
weixin_438335404 天前
springboot多实例部署时,@Scheduled注释的方法重复执行
springboot
LUCIAZZZ6 天前
SkyWalking快速入门
java·后端·spring·spring cloud·微服务·springboot·skywalking
ccmjga7 天前
升级 SpringBoot3 全项目讲解 — Spring Boot 3 中如何发Http请求?
java·spring boot·后端·docker·gradle·springboot·spring security
敲代码的小王!9 天前
在Java项目中跨域的解决办法
java·开发语言·微服务·springboot
蔚一12 天前
微服务SpringCloudAlibaba组件Spring Cloud Gateway网关教程【详解gatway网关以及各种过滤器配置使用,附有示例+代码】
java·spring boot·后端·微服务·gateway·springboot·intellij idea