跳到主要内容

Spring Boot 3.x特性-配置元数据

系列文章目录

系列文章:Spring Boot 3.x 系列教程


文章目录

  • 系列文章目录
  • 前言
  • 一、元数据格式化
  • 二、元数据提示
    • Value 提示
  • Value 提供程序
    • Any
    • Class Reference
    • Handle As
    • Spring Bean Reference
    • Spring Profile Name
  • 三、生成自己的元数据
    • 配置注解处理器
  • 元数据自动生成
  • 嵌套属性
  • 添加额外的元数据
  • 总结

前言

Spring Boot jar包含元数据文件,提供所有支持的配置属性的详细信息。
这些文件让IDE开发人员在配置application.propertiesapplication.yml 时提供上下文帮助和“code completion”。

大部分元数据文件是在编译时通过处理所有带有@ConfigurationProperties注解的项自动生成的。然而,可以为极端用例或更高级的用例手工编写部分元数据。


一、元数据格式化

配置元数据文件位于META-INF/spring-configuration-metadata.json下的jar文件中。
它们使用JSON格式,项目分类在“groups”或“properties”下,附加值提示分类在“hints”下,如下所示:

{

"groups": [
{


"name": "server",
"type": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{


"name": "spring.jpa.hibernate",
"type": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate",
"sourceType": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties",
"sourceMethod": "getHibernate()"
}
...
],"properties": [
{


"name": "server.port",
"type": "java.lang.Integer",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{


"name": "server.address",
"type": "java.net.InetAddress",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{


"name": "spring.jpa.hibernate.ddl-auto",
"type": "java.lang.String",
"description": "DDL mode. This is actually a shortcut for the \"hibernate.hbm2ddl.auto\" property.",
"sourceType": "org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate"
}
...
],"hints": [
{


"name": "spring.jpa.hibernate.ddl-auto",
"values": [
{


"value": "none",
"description": "Disable DDL handling."
},
{


"value": "validate",
"description": "Validate the schema, make no changes to the database."
},
{


"value": "update",
"description": "Update the schema if necessary."
},
{


"value": "create",
"description": "Create the schema and destroy previous data."
},
{


"value": "create-drop",
"description": "Create and then destroy the schema at the end of the session."
}
]
}
]}