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 5.0 kB

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

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

Contributors (1)