SpringBoot 配置文件

本文详细介绍了SpringBoot的配置文件,包括配置文件的位置、格式和优先级。讲解了如何设置端口和context-path,以及如何通过@ConfigurationProperties注解读取配置。还探讨了自定义配置文件的加载,如XML和Properties文件,并提到了命令行参数的使用。
摘要由CSDN通过智能技术生成

配置文件

SpringBootresources 目录下,有一个默认的全局配置文件,名称为 application.properties 或者 application.yml

SpringBoot 支持两种格式配置文件,propertiesyml

配置文件优先级

SpringBoot 自动从多个目录进行配置文件加载,加载优先级从高到底:

  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

SpringBoot 启动时会自动扫描以下目录的 application.propertiesapplication.yml 文件,如果存在多个配置文件则遵循以下规则:

  • 多个配置文件使用并集
  • 配置存在冲突时,高优先级覆盖低优先级

常用配置

设置端口

配置项:server.port,默认值:8080

application.properties

server.port = 8080

application.yml

server:
  port: 8080

设置 context-path

配置项:server.servlet.context-path,默认值:``

application.properties

server.servlet.context-path = /example

application.yml

server:
  servlet: 
    context-path: /example

读取配置

编写配置

example.username = mm
example.age = 20

通过 @Value 注解读取单个配置

在 Spring 的 Bean 中使用 @Value 注解进行注入

@Value("${example.username}")
private String username;

如果使用 @Value 注入的配置不存在,则程序在启动时会报错

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'example.username' in value "${example.username}"

可以通过添加默认值,规避这个错误

@Value("${example.username:hello}")
private String username;

通过 @ConfigurationProperties 注解批量读取配置

1、创建一个实体类,在类上使用 @ConfigurationProperties 注解,prefix 表示需要读取配置的前缀,会把以 prefix 前缀的所有配置,与类中的属性名进行匹配加载。

@Data
@ConfigurationProperties(prefix = "example")
public class UserProperties {
    private String username;
    private Integer age;
}

2、启动加载配置

有两种方法可以在启动时进行配置加载。

  • 在启动类上添加 @EnableConfigurationProperties(UserProperties.class) 注解,这样启动时,会自动对 UserProperties 类进行配置加载。
  • 在启动类上添加 @ConfigurationPropertiesScan 注解,这样启动时,会自动扫描当前包及子包下的所有 @ConfigurationProperties 标签,进行配置加载。

3、依赖注入

@ConfigurationPropertiesScan
@RestController
@SpringBootApplication
public class Application {

    @Autowired
    private UserProperties properties;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @GetMapping("/showUser")
    public UserProperties showUser() {
        return properties;
    }
}

通过 @ConfigurationProperties 注入的配置如果不存在,启动不会报错,而是通过类加载机制自动赋了一个默认值。

实际开发中建议使用 @ConfigurationProperties 来读取自定义属性。

配置自动提示

通过 @ConfigurationProperties 在应用中添加一个自定义配置类,在集成开发环境中对 application.properties 编辑时,不会自动提示这个自定义配置,但是 SpringBoot 自带的属性则会自动提示。

如何在编辑 application.properties 时也会自动提示自定义配置?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

自定义配置文件

除了在默认配置文件 application.properties 中进行配置外,SpringBoot 也可以自定义配置文件

加载自定义 XML 文件

1、编写配置文件 mybatis-config.xml

2、在启动类上添加 @ImportResource("classpath:mybatis-config.xml") 注解

@SpringBootApplication
@ImportResource("classpath:app-config.xml")
public class Application {  
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

加载自定义 Properties 文件

1、编写配置文件 jdbc.properties

2、在启动类上添加 @PropertySource("classpath:jdbc.properties") 注解

@SpringBootApplication
@PropertySource("classpath:jdbc.properties")
public class Application {  
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

命令行参数

1、使用 --server.port=8080

java -jar --server.servlet.context-path=/example --server.port=8888 my-app.jar

2、使用 -Dserver.port=8080

java -jar -Dserver.servlet.context-path=/example -Dserver.port=8888 my-app.jar
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值