SpringBoot自定义配置属性

SpringBoot 自定义配置属性

本文环境

SpringBoot 2.7.3
语言:Kotlin
JDK: jbr-11
JAVA版本:11

自定义配置

配置文件介绍

配置属性可以帮助我们在不重新打包发布的情况下,改变应用的行为。或者使用不同的配置文件来控制应用的不同版本。

SpringBoot 默认使用 application.properites 文件作为配置文件。

配置内容一般一行一个属性,例如 mysql 数据库配置:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=test

基本注解使用

自定义的属性往往是需要通过解析成 Bean 来供后续使用的。虽然 SpringBoot 会帮助完成解析,但是需要我们自己定义 Bean 。

这里,我们希望添加一个属性用来配置租户信息,租户信息定义如下:

data class Tenant(
    var group: String = "",
    var dbName: String = ""
)

自定义的属性名称是:

mine.tentants

我们会配置多个 Tenant 租户信息,所以将要配置的属性会是一个列表。

@Configuration
@ConfigurationProperties(prefix = "mine.tenants")
class TenantProperties {
    var enable: Boolean = false
    var tenants: List<Tenant> = listOf()
}

这里使用了两个注解,@Configuration 用于指定当前类用作配置,会被自动解析。@ConfigurationProperties 用于指定当前类是配置的属性 Bean,prefix 参数指定对应配置文件的属性名称。

要想配置生效,还需要告诉 Application 启用这一配置。使用**@EnableConfigurationProperties**指定配置类。

@SpringBootApplication
@EnableConfigurationProperties(TenantProperties::class)
class CustomPropertiesApplication
fun main(args: Array<String>) {
    runApplication<CustomPropertiesApplication>(*args)
}

属性参数为列表时,可以使用类似于数组引用的方式指定。
例如:

mine.tenants.enable = true
mine.tenants.tenants[0].group = test1.test1.test1
mine.tenants.tenants[0].dbName = test1
mine.tenants.tenants[1].group = test2.test2.test2
mine.tenants.tenants[1].dbName = test2

测试配置结果

在测试 Application 中添加测试代码:

@SpringBootTest
class CustomPropertiesApplicationTests {
    @Autowired
    private lateinit var tenantProperties: TenantProperties
    @Test
    fun contextLoads() {
        println(tenantProperties.enable)
        println(tenantProperties.tenants)
    }
}

运行会看到正确读取到了配置属性。

属性的识别

虽然已经能名读取到属性信息,但是每次配置的时候 Idea 没有输入提示,不太方便。我们需要一些配置让 Idea 能识别配置属性:
添加 kapt 插件,在 build.gradle.kts 文件 plugins 中添加插件:

kotlin("kapt") version "1.6.21"

在 dependencies 中添加依赖:

kapt("org.springframework.boot:spring-boot-configuration-processor:2.7.3")

当前 kapt 还没有集成到 Idea 中,所以需要手动运行一下加载程序。
在 Terminal 窗口中输入:

./gradlew kaptKotlin

完成后,SpringBoot 即可识别输入的属性。

如果 ./gradlew kaptKotlin 在执行的过程中出现错误
Kotlin counld not find the required JDK tools in the Java installation.
可能是因为没有添加Idea所使用的Java版本环境变量导致的,添加环境变量后重启系统才能生效。

您阅读此文时环境可能已经更新,请参考 SpringBoot 官方文档:
https://spring.io/guides/tutorials/spring-boot-kotlin/
Configuration properties 部分

属性的继承

有时候我们使用的属性需要继承已经定义的属性,这时我们可以直接让 Bean 继承已经定义的 Bean。

比如 spring.datasource 属性由 DataSourceProperties 解析,当我们需要自定义数据源的属性时,可以继承 DataSourceProperties 来避免属性的重复定义。

比如:

data class Tenant(
    var group: String = "",
    var dbName: String = ""
): DataSourceProperties()

配置的属性值

mine.tenants.enable=true
mine.tenants.tenants[0].group = test1.test1.test1
mine.tenants.tenants[0].dbName = test1
mine.tenants.tenants[0].driver-class-name=com.mysql.cj.jdbc.Driver
mine.tenants.tenants[0].url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
mine.tenants.tenants[0].username=root
mine.tenants.tenants[0].password=test1

使用的时候直接 @Autowired 注入 TenantProperties 即可。

总结

本文介绍了如何自定义配置属性,如何配置 Idea 在使用Kotlin的情况下能够识别配置的属性,以及属性参数是列表,属性继承的处理方法。

源码:https://gitee.com/yoshii_x/custom-properties.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
SpringBoot中,我们可以通过自定义属性配置我们的应用程序。首先,我们需要在应用程序的配置文件中定义我们的自定义属性。然后,我们可以使用@ConfigurationProperties注解将这些属性绑定到我们的自定义配置类中。 要自定义属性,我们需要完成以下几个步骤: 1. 在应用程序的配置文件(如application.properties或application.yml)中定义自定义属性。例如,我们可以在application.properties中添加以下内容: myapp.username=johndoe myapp.password=mypassword 2. 创建一个用于存储自定义属性配置类。在该类上使用@ConfigurationProperties注解,并使用prefix属性指定属性的前缀。例如,我们可以创建一个名为MyAppProperties的类: @Component @ConfigurationProperties(prefix = "myapp") public class MyAppProperties { private String username; private String password; // 省略getter和setter方法 } 3. 确保在应用程序的主类或任何其他配置类上添加@EnableConfigurationProperties注解,以启用自定义属性配置类。例如,我们可以在应用程序的主类上添加以下内容: @SpringBootApplication @EnableConfigurationProperties(MyAppProperties.class) public class MyAppApplication { // 程序的入口点 } 现在,我们就可以在我们的应用程序中使用自定义属性了。我们可以通过在需要使用这些属性的地方注入MyAppProperties类,并使用其getter方法获取属性的值。例如,我们可以在任何Spring管理的Bean中注入MyAppProperties类,并使用其getter方法获取username和password属性的值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PeterGamp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值