diff --git a/core/src/main/java/com/duanledexianxianxian/core/SpringWebApplication.java b/core/src/main/java/com/duanledexianxianxian/core/SpringWebApplication.java index 382f67eea1c11089e20b66f5893e2290b742295c..1ed6b5fcc47f094cbbe7498ae9c00704a9a8efa1 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 0000000000000000000000000000000000000000..1ac24888c6eed6863f438ec3abf28fc61ed84941 --- /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 0000000000000000000000000000000000000000..bb90a30564fb43cf580391c4c6927e7a1c551b88 --- /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 0000000000000000000000000000000000000000..6f687a89f9b33f37e72091a7f8ddb60295cf5e02 --- /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 4956d986cb8cbbec8726025d8e67e0b3962fc9b3..1641117b0521e9ffbe582f84f9788dbc2bd20053 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 0000000000000000000000000000000000000000..d4855c0f70f80af8ab2edccf0c4f7ba4a1b22f3a --- /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