SpringBoot自定义配置属性
  Bp84FnUMsDVM 2023年11月02日 64 0

(目录)

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   2024年03月22日   64   0   0 javascript
  Fo7woytj0C0D   2023年12月23日   31   0   0 pythonsedidepythonidesed
Bp84FnUMsDVM