From 3e5002dae674981422669d0c597be86c74148d15 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Mon, 3 Aug 2020 19:14:11 +0800 Subject: [PATCH] Example --- .../core/SpringWebApplication.java | 22 ++++++++++---- .../config/SystemEnvironmentProperties.java | 18 +++++++++++ .../SystemPropertySourceValueProperties.java | 30 +++++++++++++++++++ .../core/config/SystemValueProperties.java | 25 ++++++++++++++++ core/src/main/resources/application.yml | 6 +++- .../main/resources/config/system.properties | 4 +++ 6 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/com/duanledexianxianxian/core/config/SystemEnvironmentProperties.java create mode 100644 core/src/main/java/com/duanledexianxianxian/core/config/SystemPropertySourceValueProperties.java create mode 100644 core/src/main/java/com/duanledexianxianxian/core/config/SystemValueProperties.java create mode 100644 core/src/main/resources/config/system.properties diff --git a/core/src/main/java/com/duanledexianxianxian/core/SpringWebApplication.java b/core/src/main/java/com/duanledexianxianxian/core/SpringWebApplication.java index 382f67e..1ed6b5f 100644 --- a/core/src/main/java/com/duanledexianxianxian/core/SpringWebApplication.java +++ b/core/src/main/java/com/duanledexianxianxian/core/SpringWebApplication.java @@ -1,24 +1,36 @@ package com.duanledexianxianxian.core; +import com.duanledexianxianxian.core.config.SystemEnvironmentProperties; import com.duanledexianxianxian.core.config.SystemProperties; +import com.duanledexianxianxian.core.config.SystemPropertySourceValueProperties; +import com.duanledexianxianxian.core.config.SystemValueProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; /** * springboot 应用程序入口 + * * @author duanledexianxianxian */ @SpringBootApplication public class SpringWebApplication { public static void main(String[] args) { - ApplicationContext applicationContext= (ApplicationContext) SpringApplication.run(SpringWebApplication.class, args); - SystemProperties properties=applicationContext.getBean(SystemProperties.class); - System.out.println("用户的当前工作目录:"+System.getProperty("user.dir")); - // 打印出配置参数 - System.out.println(properties.getName()); + ApplicationContext applicationContext = + SpringApplication.run(SpringWebApplication.class, args); + SystemProperties systemProperties = applicationContext.getBean(SystemProperties.class); + SystemValueProperties valueProperties = applicationContext.getBean(SystemValueProperties.class); + SystemEnvironmentProperties environmentProperties = applicationContext.getBean(SystemEnvironmentProperties.class); + SystemPropertySourceValueProperties propertySourceValueProperties = applicationContext.getBean(SystemPropertySourceValueProperties.class); + + System.out.println("用户的当前工作目录:" + System.getProperty("user.dir")); + // 通过@value加载配置方式 + System.out.println("@Value:" + valueProperties); + System.out.println("@ConfigurationProperties:" + systemProperties); + System.out.println("Environment:" + environmentProperties.getEnvironment().getProperty("system.name")); + System.out.println("@PropertySource+@Value+@ConfigurationProperties:" + propertySourceValueProperties); } diff --git a/core/src/main/java/com/duanledexianxianxian/core/config/SystemEnvironmentProperties.java b/core/src/main/java/com/duanledexianxianxian/core/config/SystemEnvironmentProperties.java new file mode 100644 index 0000000..1ac2488 --- /dev/null +++ b/core/src/main/java/com/duanledexianxianxian/core/config/SystemEnvironmentProperties.java @@ -0,0 +1,18 @@ +package com.duanledexianxianxian.core.config; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +/** + * 通过Environment加载配置参数 + * @author duanledexianxianxian + */ +@Component +@Data +public class SystemEnvironmentProperties { + @Autowired + private Environment environment; +} diff --git a/core/src/main/java/com/duanledexianxianxian/core/config/SystemPropertySourceValueProperties.java b/core/src/main/java/com/duanledexianxianxian/core/config/SystemPropertySourceValueProperties.java new file mode 100644 index 0000000..bb90a30 --- /dev/null +++ b/core/src/main/java/com/duanledexianxianxian/core/config/SystemPropertySourceValueProperties.java @@ -0,0 +1,30 @@ +package com.duanledexianxianxian.core.config; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +/** + * 通过@value加载配置参数 + * @author duanledexianxianxian + */ +@Component +@ConfigurationProperties(prefix = "system.property") +@PropertySource(value = { "/config/system.properties" }) +@Data +public class SystemPropertySourceValueProperties { + /** + * 名称 + * //最好设置默认值,否则配置文件即使有相应key,也会应用启动失败 + */ + @Value("${name:null}") + private String name; + /** + * 编码 + * //最好设置默认值,否则配置文件即使有相应key,也会应用启动失败 + */ + @Value("${code:null}") + private String code; +} diff --git a/core/src/main/java/com/duanledexianxianxian/core/config/SystemValueProperties.java b/core/src/main/java/com/duanledexianxianxian/core/config/SystemValueProperties.java new file mode 100644 index 0000000..6f687a8 --- /dev/null +++ b/core/src/main/java/com/duanledexianxianxian/core/config/SystemValueProperties.java @@ -0,0 +1,25 @@ +package com.duanledexianxianxian.core.config; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * 通过@value加载配置参数 + * @author duanledexianxianxian + */ +@Component +@Data +public class SystemValueProperties { + /** + * 名称 + */ + @Value("${system.name}") + private String name; + /** + * 编码 + */ + @Value("${system.code}") + private String code; +} diff --git a/core/src/main/resources/application.yml b/core/src/main/resources/application.yml index 4956d98..1641117 100644 --- a/core/src/main/resources/application.yml +++ b/core/src/main/resources/application.yml @@ -1,5 +1,9 @@ server: port: 8080 +#系统相关配置 system: - name: "classpath root config" \ No newline at end of file +# 系统名称 + name: "classpath root config" +# 系统编码 + code: "classpath root code" \ No newline at end of file diff --git a/core/src/main/resources/config/system.properties b/core/src/main/resources/config/system.properties new file mode 100644 index 0000000..d4855c0 --- /dev/null +++ b/core/src/main/resources/config/system.properties @@ -0,0 +1,4 @@ +system.property.name=\u7CFB\u7EDF\u540D\u79F0 +system.property.code=\u7CFB\u7EDF\u7F16\u7801 +#name=ssss +#code=ssss \ No newline at end of file -- GitLab