You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # spring-boot-demo-properties
  2. > 本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-properties</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-properties</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <!--
  30. 在 META-INF/additional-spring-configuration-metadata.json 中配置
  31. 可以去除 application.yml 中自定义配置的红线警告,并且为自定义配置添加 hint 提醒
  32. -->
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-configuration-processor</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.projectlombok</groupId>
  45. <artifactId>lombok</artifactId>
  46. <optional>true</optional>
  47. </dependency>
  48. <dependency>
  49. <groupId>cn.hutool</groupId>
  50. <artifactId>hutool-all</artifactId>
  51. </dependency>
  52. </dependencies>
  53. <build>
  54. <finalName>spring-boot-demo-properties</finalName>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>
  63. ```
  64. ## ApplicationProperty.java
  65. ```java
  66. /**
  67. * <p>
  68. * 项目配置
  69. * </p>
  70. *
  71. * @author yangkai.shen
  72. * @date Created in 2018-09-29 10:50
  73. */
  74. @Data
  75. @Component
  76. public class ApplicationProperty {
  77. @Value("${application.name}")
  78. private String name;
  79. @Value("${application.version}")
  80. private String version;
  81. }
  82. ```
  83. ## DeveloperProperty.java
  84. ```java
  85. /**
  86. * <p>
  87. * 开发人员配置信息
  88. * </p>
  89. *
  90. * @author yangkai.shen
  91. * @date Created in 2018-09-29 10:51
  92. */
  93. @Data
  94. @ConfigurationProperties(prefix = "developer")
  95. @Component
  96. public class DeveloperProperty {
  97. private String name;
  98. private String website;
  99. private String qq;
  100. private String phoneNumber;
  101. }
  102. ```
  103. ## PropertyController.java
  104. ```java
  105. /**
  106. * <p>
  107. * 测试Controller
  108. * </p>
  109. *
  110. * @author yangkai.shen
  111. * @date Created in 2018-09-29 10:49
  112. */
  113. @RestController
  114. public class PropertyController {
  115. private final ApplicationProperty applicationProperty;
  116. private final DeveloperProperty developerProperty;
  117. @Autowired
  118. public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
  119. this.applicationProperty = applicationProperty;
  120. this.developerProperty = developerProperty;
  121. }
  122. @GetMapping("/property")
  123. public Dict index() {
  124. return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty);
  125. }
  126. }
  127. ```
  128. ## additional-spring-configuration-metadata.json
  129. > 位置: src/main/resources/META-INF/additional-spring-configuration-metadata.json
  130. ```json
  131. {
  132. "properties": [
  133. {
  134. "name": "application.name",
  135. "description": "Default value is artifactId in pom.xml.",
  136. "type": "java.lang.String"
  137. },
  138. {
  139. "name": "application.version",
  140. "description": "Default value is version in pom.xml.",
  141. "type": "java.lang.String"
  142. },
  143. {
  144. "name": "developer.name",
  145. "description": "The Developer Name.",
  146. "type": "java.lang.String"
  147. },
  148. {
  149. "name": "developer.website",
  150. "description": "The Developer Website.",
  151. "type": "java.lang.String"
  152. },
  153. {
  154. "name": "developer.qq",
  155. "description": "The Developer QQ Number.",
  156. "type": "java.lang.String"
  157. },
  158. {
  159. "name": "developer.phone-number",
  160. "description": "The Developer Phone Number.",
  161. "type": "java.lang.String"
  162. }
  163. ]
  164. }
  165. ```