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.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * @package: com.xkcoding.properties.property
  72. * @description: 项目配置
  73. * @author: yangkai.shen
  74. * @date: Created in 2018/9/29 10:50 AM
  75. * @copyright: Copyright (c) 2018
  76. * @version: V1.0
  77. * @modified: yangkai.shen
  78. */
  79. @Data
  80. @Component
  81. public class ApplicationProperty {
  82. @Value("${application.name}")
  83. private String name;
  84. @Value("${application.version}")
  85. private String version;
  86. }
  87. ```
  88. ## DeveloperProperty.java
  89. ```java
  90. /**
  91. * <p>
  92. * 开发人员配置信息
  93. * </p>
  94. *
  95. * @package: com.xkcoding.properties.property
  96. * @description: 开发人员配置信息
  97. * @author: yangkai.shen
  98. * @date: Created in 2018/9/29 10:51 AM
  99. * @copyright: Copyright (c) 2018
  100. * @version: V1.0
  101. * @modified: yangkai.shen
  102. */
  103. @Data
  104. @ConfigurationProperties(prefix = "developer")
  105. @Component
  106. public class DeveloperProperty {
  107. private String name;
  108. private String website;
  109. private String qq;
  110. private String phoneNumber;
  111. }
  112. ```
  113. ## PropertyController.java
  114. ```java
  115. /**
  116. * <p>
  117. * 测试Controller
  118. * </p>
  119. *
  120. * @package: com.xkcoding.properties.controller
  121. * @description: 测试Controller
  122. * @author: yangkai.shen
  123. * @date: Created in 2018/9/29 10:49 AM
  124. * @copyright: Copyright (c) 2018
  125. * @version: V1.0
  126. * @modified: yangkai.shen
  127. */
  128. @RestController
  129. public class PropertyController {
  130. private final ApplicationProperty applicationProperty;
  131. private final DeveloperProperty developerProperty;
  132. @Autowired
  133. public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
  134. this.applicationProperty = applicationProperty;
  135. this.developerProperty = developerProperty;
  136. }
  137. @GetMapping("/property")
  138. public Dict index() {
  139. return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty);
  140. }
  141. }
  142. ```
  143. ## additional-spring-configuration-metadata.json
  144. > 位置: src/main/resources/META-INF/additional-spring-configuration-metadata.json
  145. ```json
  146. {
  147. "properties": [
  148. {
  149. "name": "application.name",
  150. "description": "Default value is artifactId in pom.xml.",
  151. "type": "java.lang.String"
  152. },
  153. {
  154. "name": "application.version",
  155. "description": "Default value is version in pom.xml.",
  156. "type": "java.lang.String"
  157. },
  158. {
  159. "name": "developer.name",
  160. "description": "The Developer Name.",
  161. "type": "java.lang.String"
  162. },
  163. {
  164. "name": "developer.website",
  165. "description": "The Developer Website.",
  166. "type": "java.lang.String"
  167. },
  168. {
  169. "name": "developer.qq",
  170. "description": "The Developer QQ Number.",
  171. "type": "java.lang.String"
  172. },
  173. {
  174. "name": "developer.phone-number",
  175. "description": "The Developer Phone Number.",
  176. "type": "java.lang.String"
  177. }
  178. ]
  179. }
  180. ```

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。