Spring Boot 常用配置以及自定义配置

Spring Boot 常用配置简单介绍及使用

多环境配置

Spring Boot Profile

在 Spring Boot 中多环境配置文件名需要满足 application-{profile}.properties 的格式,其中 {profile} 对应环境标识

  • application-dev.properties 开发环境
  • application-test.properties 测试环境
  • application-prod.properties 生产环境

在 application.properties 文件中通过 spring.profiles.active 属性来设置,其值对应 {profile} 值。比如:

spring.profiles.active=dev

Spring Boot 在启动时会加载 application-dev.properties 配置文件

Maven Profile

如果我们使用的构建工具是 Maven,也可以通过 Maven 的 Profile 特性来实现多环境配置以及打包。这样做的好处是我们不需要每次打包的时候改变 spring.profiles.active 的值,打包的时候 mvn clean package -P ${profile} 使用 -P 参数指定

pom.xml 配置如下:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileActive>dev</profileActive>
        </properties>
        <activation>
            <!-- 开发启动 Spring Boot 时默认加载 application-dev.properties -->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profileActive>test</profileActive>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <profileActive>prod</profileActive>
        </properties>
    </profile>
</profiles>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>application-dev.properties</exclude>
                <exclude>application-test.properties</exclude>
                <exclude>application-prod.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application.properties</include>
                <include>application-${profileActive}.properties</include>
            </includes>
        </resource>
    </resources>
</build>

application.properties 配置文件如下

## profile (dev,test,prod) default dev
spring.profiles.active=@profileActive@

日志配置

Spring Boot 使用 Commons Logging 进行日志记录,但保留底层日志实现(Java Util Logging, Log4J2 and Logback),默认使用 Logback 作为底层实现。

日志常用配置


## Logback 日志级别 ERROR, WARN, INFO, DEBUG or TRACE
logging.level.com.renguangli.config=debug
logging.level.org.springframework.web=info

## 日志文件名称 相对或绝对路径
logging.file=./logs/springboot.log
## 控制台日志格式
# logging.pattern.console=
## 日志文件格式
# logging.pattern.file=

除了在 application.properties 文件中配置日志外还可以使用 xml 配置文件格式。对于 Logback 日志框架来说 Spring Boot 默认加载文件为名为 logback-spring.xml, logback.xml 的配置文件,log4j2 默认加载 log4j2-spring.xml or log4j2.xml 的配置文件。当让也可以使用如下配置指定:

logging.config=classpath:logback.xml

下面是一个完整的 Logback 日志配置文件


下面是一个完整的 log4j2 日志配置文件


自定义配置

@Value

config.name=hello
config.value=world

对于像上面的自定义配置我们可以这样使用

@Value("${config.name}")
private String name;

@Value("${config.value}")
private String value;

一两个配置的话还好,一旦配置多起来就比较难看,对于这种情况我们可以使用 Spring Boot 提供的 @ConfigurationProperties 注解来简化配置

配置类

@Component
@ConfigurationProperties(prefix = "config")
public class ConfigProperties {

    private String name;

    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

如何使用呢?

@Autowired
private ConfigProperties configProperties;

自定义配置自动提示

在 pom.xml 文件中添加依赖

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

第二步我们需要配置META-INF/spring-configuration-metadata.json文件来描述。为了方便我们可以通过 idea 自动生成。

在 idea 设置中搜索 Annotation Processors,接下来勾住 Enable annonation processing 就完成了。

我们可以在编译后的文件中看到自动生成的spring-configuration-metadata.json。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MfdIS8Iy-1589785423191)(https://renguangli.com/images/spring-boot/spring-boot-metadata.jpg)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v6dd3wEk-1589785423194)(https://renguangli.com/images/spring-boot/spring-boot-config.jpg)]

参考资料

boot-features-logging

Springboot之自定义配置

Spring Boot Profiles实现多环境下配置切换

示例代码在 Github 上地址:spring-boot-config

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值