diff --git a/demo-properties/README.md b/demo-properties/README.md
index f326bef..58fe932 100644
--- a/demo-properties/README.md
+++ b/demo-properties/README.md
@@ -1,191 +1,107 @@
-# spring-boot-demo-properties
+## spring-boot-demo-properties
> 本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取
-## pom.xml
+### 1.开发步骤
+#### 1.1.添加依赖
```xml
-
-
- * 项目配置 - *
- * - * @author yangkai.shen - * @date Created in 2018-09-29 10:50 - */ @Data @Component public class ApplicationProperty { - @Value("${application.name}") - private String name; - @Value("${application.version}") - private String version; + @Value("${application.name}") + private String name; + @Value("${application.version}") + private String version; } -``` - -## DeveloperProperty.java -```java -/** - *- * 开发人员配置信息 - *
- * - * @author yangkai.shen - * @date Created in 2018-09-29 10:51 - */ @Data @ConfigurationProperties(prefix = "developer") @Component public class DeveloperProperty { - private String name; - private String website; - private String qq; - private String phoneNumber; + private String name; + private String website; + private String email; } ``` -## PropertyController.java +#### 1.3.测试入口 ```java -/** - *- * 测试Controller - *
- * - * @author yangkai.shen - * @date Created in 2018-09-29 10:49 - */ @RestController public class PropertyController { - private final ApplicationProperty applicationProperty; - private final DeveloperProperty developerProperty; - - @Autowired - public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) { - this.applicationProperty = applicationProperty; - this.developerProperty = developerProperty; - } - - @GetMapping("/property") - public Dict index() { - return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty); - } + private final ApplicationProperty applicationProperty; + private final DeveloperProperty developerProperty; + + @Autowired + public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) { + this.applicationProperty = applicationProperty; + this.developerProperty = developerProperty; + } + + @GetMapping("/property") + public Dict index() { + return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty); + } } ``` -## additional-spring-configuration-metadata.json - -> 位置: src/main/resources/META-INF/additional-spring-configuration-metadata.json - -```json -{ - "properties": [ - { - "name": "application.name", - "description": "Default value is artifactId in pom.xml.", - "type": "java.lang.String" - }, - { - "name": "application.version", - "description": "Default value is version in pom.xml.", - "type": "java.lang.String" - }, - { - "name": "developer.name", - "description": "The Developer Name.", - "type": "java.lang.String" - }, - { - "name": "developer.website", - "description": "The Developer Website.", - "type": "java.lang.String" - }, - { - "name": "developer.qq", - "description": "The Developer QQ Number.", - "type": "java.lang.String" - }, - { - "name": "developer.phone-number", - "description": "The Developer Phone Number.", - "type": "java.lang.String" - } - ] -} +#### 1.4.配置文件 +**application.yml** +```yml +server: + port: 8080 + servlet: + context-path: /demo +spring: + profiles: + active: prod +``` +**application-dev.yml** +```yml +application: + name: dev环境 @artifactId@ + version: dev环境 @version@ +developer: + name: dev环境 xkcoding + website: dev环境 https://xkcoding.com + email: dev环境 237497819@qq.com ``` +**application-prod.yml** +```yml +application: + name: prod环境 @artifactId@ + version: prod环境 @version@ +developer: + name: prod环境 xkcoding + website: prod环境 https://xkcoding.com + email: prod环境 237497819@qq.com +``` + +#### 1.5. 其他 + +编写配置提示,`additional-spring-configuration-metadata.json` + +参考: src/main/resources/META-INF/additional-spring-configuration-metadata.json + +### 2.测试 +1. 启动 `PropertiesApplication.java` ; +2. 打开任意浏览器,输入 `http://localhost:8080/demo/property` ,检查输出结果; +3. 将 `application.yml` 配置文件中的 `spring.profiles.active` 参数从 `prod` 修改为 `dev` ,重新启动,观察输出结果变化。 diff --git a/demo-properties/pom.xml b/demo-properties/pom.xml index 75e2245..5ab6e17 100644 --- a/demo-properties/pom.xml +++ b/demo-properties/pom.xml @@ -1,75 +1,74 @@- * 项目配置 + * 项目信息配置类 *
* * @author yangkai.shen - * @date Created in 2018-09-29 10:50 + * @date Created in 2022-08-12 21:50 */ @Data @Component diff --git a/demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java b/demo-properties/src/main/java/com/xkcoding/properties/config/DeveloperProperty.java similarity index 68% rename from demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java rename to demo-properties/src/main/java/com/xkcoding/properties/config/DeveloperProperty.java index 635319e..33e899a 100644 --- a/demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java +++ b/demo-properties/src/main/java/com/xkcoding/properties/config/DeveloperProperty.java @@ -1,4 +1,4 @@ -package com.xkcoding.properties.property; +package com.xkcoding.properties.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -6,11 +6,11 @@ import org.springframework.stereotype.Component; /** *- * 开发人员配置信息 + * 开发人员信息配置类 *
* * @author yangkai.shen - * @date Created in 2018-09-29 10:51 + * @date Created in 2022-08-12 21:50 */ @Data @ConfigurationProperties(prefix = "developer") @@ -18,6 +18,5 @@ import org.springframework.stereotype.Component; public class DeveloperProperty { private String name; private String website; - private String qq; - private String phoneNumber; + private String email; } diff --git a/demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java b/demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java index 099cf1c..81c2fe7 100644 --- a/demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java +++ b/demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java @@ -1,19 +1,19 @@ package com.xkcoding.properties.controller; import cn.hutool.core.lang.Dict; -import com.xkcoding.properties.property.ApplicationProperty; -import com.xkcoding.properties.property.DeveloperProperty; +import com.xkcoding.properties.config.ApplicationProperty; +import com.xkcoding.properties.config.DeveloperProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** *- * 测试Controller + * 测试端点 *
* * @author yangkai.shen - * @date Created in 2018-09-29 10:49 + * @date Created in 2022-08-12 21:12 */ @RestController public class PropertyController { diff --git a/demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json index b3a887d..6c116f5 100644 --- a/demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1,34 +1,29 @@ { - "properties": [ - { - "name": "application.name", - "description": "Default value is artifactId in pom.xml.", - "type": "java.lang.String" - }, - { - "name": "application.version", - "description": "Default value is version in pom.xml.", - "type": "java.lang.String" - }, - { - "name": "developer.name", - "description": "The Developer Name.", - "type": "java.lang.String" - }, - { - "name": "developer.website", - "description": "The Developer Website.", - "type": "java.lang.String" - }, - { - "name": "developer.qq", - "description": "The Developer QQ Number.", - "type": "java.lang.String" - }, - { - "name": "developer.phone-number", - "description": "The Developer Phone Number.", - "type": "java.lang.String" - } - ] -} \ No newline at end of file + "properties": [ + { + "name": "application.name", + "description": "Default value is artifactId in pom.xml.", + "type": "java.lang.String" + }, + { + "name": "application.version", + "description": "Default value is version in pom.xml.", + "type": "java.lang.String" + }, + { + "name": "developer.name", + "description": "The Developer Name.", + "type": "java.lang.String" + }, + { + "name": "developer.website", + "description": "The Developer Website.", + "type": "java.lang.String" + }, + { + "name": "developer.email", + "description": "The Developer QQ Number.", + "type": "java.lang.String" + } + ] +} diff --git a/demo-properties/src/main/resources/application-dev.yml b/demo-properties/src/main/resources/application-dev.yml index 98170cc..0db1e77 100644 --- a/demo-properties/src/main/resources/application-dev.yml +++ b/demo-properties/src/main/resources/application-dev.yml @@ -3,6 +3,5 @@ application: version: dev环境 @version@ developer: name: dev环境 xkcoding - website: dev环境 http://xkcoding.com - qq: dev环境 237497819 - phone-number: dev环境 17326075631 \ No newline at end of file + website: dev环境 https://xkcoding.com + email: dev环境 237497819@qq.com diff --git a/demo-properties/src/main/resources/application-prod.yml b/demo-properties/src/main/resources/application-prod.yml index 9dc37ea..0eeb020 100644 --- a/demo-properties/src/main/resources/application-prod.yml +++ b/demo-properties/src/main/resources/application-prod.yml @@ -3,6 +3,5 @@ application: version: prod环境 @version@ developer: name: prod环境 xkcoding - website: prod环境 http://xkcoding.com - qq: prod环境 237497819 - phone-number: prod环境 17326075631 \ No newline at end of file + website: prod环境 https://xkcoding.com + email: prod环境 237497819@qq.com diff --git a/demo-properties/src/main/resources/application.yml b/demo-properties/src/main/resources/application.yml index 22ebb3e..c2f5054 100644 --- a/demo-properties/src/main/resources/application.yml +++ b/demo-properties/src/main/resources/application.yml @@ -4,4 +4,4 @@ server: context-path: /demo spring: profiles: - active: prod \ No newline at end of file + active: prod diff --git a/demo-properties/src/test/java/com/xkcoding/properties/PropertiesApplicationTests.java b/demo-properties/src/test/java/com/xkcoding/properties/PropertiesApplicationTests.java new file mode 100644 index 0000000..532591b --- /dev/null +++ b/demo-properties/src/test/java/com/xkcoding/properties/PropertiesApplicationTests.java @@ -0,0 +1,13 @@ +package com.xkcoding.properties; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PropertiesApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java b/demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java deleted file mode 100644 index 79cdf57..0000000 --- a/demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xkcoding.properties; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringBootDemoPropertiesApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/pom.xml b/pom.xml index 263dc00..09cd2ce 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@