Spring Boot 2关于配置文件的使用

本文详细介绍SpringBoot中配置文件的应用方式,包括使用不同格式的配置文件、动态加载配置、通过多种方式读取配置值等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置文件使用

  • properties文件
  • yml文件
    当前例子全部使用yml配置文件,区分不同环境的配置

application-dev.yml
application-devDb.yml
application-devRedis.yml

application-test.yml
application-testDb.yml
application-testRedis.yml

application-pre.yml
application-preDb.yml
application-preRedis.yml

application-prod.yml
application-prodDb.yml
application-prodRedis.yml

在yml文件中引入其他yml文件

spring:
  profiles:
    include: devDb,devRedis

通过启动参数-Dspring.profiles.active=test来决定配置文件的使用

通过@Value取值

对于单个值的获取很方便,支持SpEL 表达式

 @Value("${datasource.url}")
 private String dbUrl;

通过@ConfigurationProperties

这个主要针对有特殊意义的多个值放在一起,在使用的时候可以直接当做一个Bean来使用

@Component
@ConfigurationProperties(prefix = "datasource")
public class DbProperties implements Serializable {
    private String type;
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    //省略get,set方法
}    

通过Spring的Environment来获取

public static String getValue(String key) {
        if (StringUtils.isNotBlank(key)) {
            Environment environment = getEnvironment();
            if (environment != null) {
                return environment.getProperty(key);
            }
        }

        return null;
}

通过@PropertySource引入其他properties文件

这个只能针对properties,

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"}, encoding = "UTF-8")
public class OtherProperties implements Serializable {
    private String name;
    private Integer age;
   //省略get,set方法
}    

通过@ImportResource引入Spring配置

首先需要配置相关的Bean,然后再入口类上面引用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="testComponent" class="org.ghost.springboot.demo.common.component.TestComponent"
          init-method="init" destroy-method="destory">
        <property name="name" value="测试Hello"/>
    </bean>
</beans>

@ImportResource(value = {"classpath:other.xml"})
@SpringBootApplication(scanBasePackages = "org.ghost.springboot.demo")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

通过启动参数spring.config.location来处理

如下目录结构,将不同环境的配置文件放在不同目录:

  • dev
    • application.yaml
    • person.properties
    • logback.xml
  • test
    • application.yaml
    • person.properties
    • logback.xml
  • prod
    • application.yaml
    • person.properties
    • logback.xml

通过启动参数来控制:

  • -Dspring.profiles.active=dev -Dspring.config.location=classpath:/env/dev/
  • -Dspring.profiles.active=test -Dspring.config.location=classpath:/env/test/
  • -Dspring.profiles.active=prod -Dspring.config.location=classpath:/env/prod/

通过@PropertySource需要指定路径的要动态拼接路径

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"${spring.config.location}person.properties"}, encoding = "UTF-8")
public class PersonProperties implements Serializable {
    //
}

源码位置:
https://gitee.com/ceclar123/spring-boot-demo/tree/master/ch07
https://gitee.com/ceclar123/spring-boot-demo/tree/master/ch08

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值