From 2d770a1f425e578810be34ed216d1eb4781b7ef8 Mon Sep 17 00:00:00 2001
From: "Yangkai.Shen" <237497819@qq.com>
Date: Sat, 29 Sep 2018 11:35:42 +0800
Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-properties=20?=
=?UTF-8?q?=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
spring-boot-demo-properties/.gitignore | 25 +++
spring-boot-demo-properties/README.md | 208 ++++++++++++++++++
spring-boot-demo-properties/pom.xml | 71 ++++++
.../SpringBootDemoPropertiesApplication.java | 25 +++
.../controller/PropertyController.java | 38 ++++
.../property/ApplicationProperty.java | 27 +++
.../property/DeveloperProperty.java | 28 +++
...itional-spring-configuration-metadata.json | 34 +++
.../src/main/resources/application-dev.yml | 8 +
.../src/main/resources/application-prod.yml | 8 +
.../src/main/resources/application.yml | 7 +
...ingBootDemoPropertiesApplicationTests.java | 16 ++
12 files changed, 495 insertions(+)
create mode 100644 spring-boot-demo-properties/.gitignore
create mode 100644 spring-boot-demo-properties/README.md
create mode 100644 spring-boot-demo-properties/pom.xml
create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/properties/SpringBootDemoPropertiesApplication.java
create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java
create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/ApplicationProperty.java
create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java
create mode 100644 spring-boot-demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json
create mode 100644 spring-boot-demo-properties/src/main/resources/application-dev.yml
create mode 100644 spring-boot-demo-properties/src/main/resources/application-prod.yml
create mode 100644 spring-boot-demo-properties/src/main/resources/application.yml
create mode 100644 spring-boot-demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java
diff --git a/spring-boot-demo-properties/.gitignore b/spring-boot-demo-properties/.gitignore
new file mode 100644
index 0000000..82eca33
--- /dev/null
+++ b/spring-boot-demo-properties/.gitignore
@@ -0,0 +1,25 @@
+/target/
+!.mvn/wrapper/maven-wrapper.jar
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/build/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
\ No newline at end of file
diff --git a/spring-boot-demo-properties/README.md b/spring-boot-demo-properties/README.md
new file mode 100644
index 0000000..721dd85
--- /dev/null
+++ b/spring-boot-demo-properties/README.md
@@ -0,0 +1,208 @@
+# spring-boot-demo-properties
+
+> 本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取
+
+## pom.xml
+
+```xml
+
+
+ * 项目配置 + *
+ * + * @package: com.xkcoding.properties.property + * @description: 项目配置 + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:50 AM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@Component +public class ApplicationProperty { + @Value("${application.name}") + private String name; + @Value("${application.version}") + private String version; +} +``` + +## DeveloperProperty.java + +```java +/** + *+ * 开发人员配置信息 + *
+ * + * @package: com.xkcoding.properties.property + * @description: 开发人员配置信息 + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:51 AM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@ConfigurationProperties(prefix = "developer") +@Component +public class DeveloperProperty { + private String name; + private String website; + private String qq; + private String phoneNumber; +} +``` + +## PropertyController.java + +```java +/** + *+ * 测试Controller + *
+ * + * @package: com.xkcoding.properties.controller + * @description: 测试Controller + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:49 AM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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); + } +} +``` + +## 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" + } + ] +} +``` + diff --git a/spring-boot-demo-properties/pom.xml b/spring-boot-demo-properties/pom.xml new file mode 100644 index 0000000..b5b059a --- /dev/null +++ b/spring-boot-demo-properties/pom.xml @@ -0,0 +1,71 @@ + ++ * 启动类 + *
+ * + * @package: com.xkcoding.properties + * @description: 启动类 + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:48 AM + * @copyright: Copyright (c)2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@SpringBootApplication +public class SpringBootDemoPropertiesApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoPropertiesApplication.class, args); + } +} diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java new file mode 100644 index 0000000..5e7b49c --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/controller/PropertyController.java @@ -0,0 +1,38 @@ +package com.xkcoding.properties.controller; + +import cn.hutool.core.lang.Dict; +import com.xkcoding.properties.property.ApplicationProperty; +import com.xkcoding.properties.property.DeveloperProperty; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + *+ * 测试Controller + *
+ * + * @package: com.xkcoding.properties.controller + * @description: 测试Controller + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:49 AM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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); + } +} diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/ApplicationProperty.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/ApplicationProperty.java new file mode 100644 index 0000000..0ae920d --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/ApplicationProperty.java @@ -0,0 +1,27 @@ +package com.xkcoding.properties.property; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + *+ * 项目配置 + *
+ * + * @package: com.xkcoding.properties.property + * @description: 项目配置 + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:50 AM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@Component +public class ApplicationProperty { + @Value("${application.name}") + private String name; + @Value("${application.version}") + private String version; +} diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java new file mode 100644 index 0000000..d8c486f --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/properties/property/DeveloperProperty.java @@ -0,0 +1,28 @@ +package com.xkcoding.properties.property; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + *+ * 开发人员配置信息 + *
+ * + * @package: com.xkcoding.properties.property + * @description: 开发人员配置信息 + * @author: yangkai.shen + * @date: Created in 2018/9/29 10:51 AM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@ConfigurationProperties(prefix = "developer") +@Component +public class DeveloperProperty { + private String name; + private String website; + private String qq; + private String phoneNumber; +} diff --git a/spring-boot-demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 0000000..b3a887d --- /dev/null +++ b/spring-boot-demo-properties/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,34 @@ +{ + "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 diff --git a/spring-boot-demo-properties/src/main/resources/application-dev.yml b/spring-boot-demo-properties/src/main/resources/application-dev.yml new file mode 100644 index 0000000..98170cc --- /dev/null +++ b/spring-boot-demo-properties/src/main/resources/application-dev.yml @@ -0,0 +1,8 @@ +application: + name: dev环境 @artifactId@ + 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 diff --git a/spring-boot-demo-properties/src/main/resources/application-prod.yml b/spring-boot-demo-properties/src/main/resources/application-prod.yml new file mode 100644 index 0000000..9dc37ea --- /dev/null +++ b/spring-boot-demo-properties/src/main/resources/application-prod.yml @@ -0,0 +1,8 @@ +application: + name: prod环境 @artifactId@ + 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 diff --git a/spring-boot-demo-properties/src/main/resources/application.yml b/spring-boot-demo-properties/src/main/resources/application.yml new file mode 100644 index 0000000..22ebb3e --- /dev/null +++ b/spring-boot-demo-properties/src/main/resources/application.yml @@ -0,0 +1,7 @@ +server: + port: 8080 + servlet: + context-path: /demo +spring: + profiles: + active: prod \ No newline at end of file diff --git a/spring-boot-demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java b/spring-boot-demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java new file mode 100644 index 0000000..7a325a7 --- /dev/null +++ b/spring-boot-demo-properties/src/test/java/com/xkcoding/properties/SpringBootDemoPropertiesApplicationTests.java @@ -0,0 +1,16 @@ +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() { + } + +}