设为首页 加入收藏

TOP

SpringBoot系列三:SpringBoot自定义Starter(四)
2018-11-21 22:08:37 】 浏览:585
Tags:SpringBoot 系列 定义 Starter
ork.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @auther: wangzb * @date: 2018/11/17 11:17 */ @Controller public class HelloWorldController { @Autowired private HelloWorldService helloWorldService; @RequestMapping(value = "/sayHello") public String sayHello(String name){ return helloWorldService.sayHello(name); } }

启动项目,在浏览器输入:http://localhost:8080/sayHello?name=wangzb,浏览器返回:

hello.msg = 你好

我们在重启项目,重复以上步骤,浏览器响应:

Hello wangzb 你好

3 元数据的配置

到目前为止,helloworld 的自动配置功能已实现,并且正确使用了,但还有一点不够完美,如果你也按上面步骤实现了自己的 helloworld 自动配置,在 application.properties 中配置 hello.msg 属性时,你会发现并没有提示你有关该配置的信息,但是如果你想配置 Tomcat 端口时,输入 server.port 是有提示的:

这种功能我们如何做呢,我们打开 SpringBoot入门 章节下载的 “spring-boot-reference.pdf” 文件,在目录中找到 “Appendix B. Configuration Metadata(附录B.配置元数据)”,下面我们使用谷歌来翻译下相关原文:

   Spring Boot jars include metadata files that provide details of all supported configuration properties.The files are designed to let IDE developers offer contextual help and “code completion” as users are working with application.properties or application.yml files.
Spring Boot jar 包含元数据文件,提供所有支持的配置属性的详细信息。这些文件旨在让 IDE 开发人员使用 application.properties或 application.yml 文件像用户一样提供上下文帮助和“代码完成”
……
B.1 Metadata Format
Configuration metadata files are located inside jars under META-INF/spring-configurationmetadata.json They use a simple JSON format with items categorized under either “groups” or “properties” and additional values hints categorized under “hints”, as shown in the following example:
B.1 元数据格式
配置元数据文件位于 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"
},
...
],
"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"
},
...
],
"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."
     }]
]}

所以元数据是在位于 META-INF/spring-configuration-metadata.json 下配置的,下面就列出有关 groups、properties、hints 使用。

3.1 Group属性

“groups” 中包含的 JSON 对象可以包含下表中显示的属性:

3.2 Property属性

properties 数组中包含的 JSON 对象可由以下属性构成:

3.3 hints属性

hints 数组中包含的 JSON 对象可以包含以下属性:

每个 “hints” 元素的 values 属性中包含的 JSON

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇让SpringBoot启动更快一点 下一篇SpringBoot系列二:SpringBoot自..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目