springboot系统属性配置会覆盖自定义属性配置
问题
无法获取正确的自定义属性的值
源码解析
系统配置加载源码
springboot 基于 spring 的
spring AbstractApplicationContext 的 refresh 方法 的 prepareRefresh() 方法加载的环境变量的值
systemProperties 有 user.home 属性
@PropertySource加载配置文件源码
ConfigurationClassPostprocessor.java 通过 ConfigrationClassParse 类解析很多注解
比如 @Component @Configuration @Bean 等注解
我们全局搜索 @PropertySource
processPropertySource(propertySource)方法
属性覆盖源码
控制台打印
04:33:14.134 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key ‘user.home’ in PropertySource ‘systemProperties’ with value of type String
说明:
如果自定义属性值存在系统属性中 , 那么系统属性就会覆盖
系统属性方法
springboot 中可以通过方法直接获取系统的默认属性
通过源码知道系统属性存在与 Environment 对象中
所以通过上下文对象获取 Environment , 通过 Environment 获取系统属性
package com.example.configfilereload;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class PropertySourceTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
ConfigurableEnvironment environment = ac.getEnvironment();
environment.getSystemProperties().forEach((k,v)->{
System.out.println(k+" "+v);
});
}
}
系统一般的默认方法打印
附录在最后
注意事项
1.配置文件中属性使用时候 , 不要和默认系统属性重名
2.我们可以直接获取系统属性
案例
在 springboot 核心配置文件 ( application.properties ) 中定义系统重名属性 user.dir
user.dir=z:/
在 自定义配置文件中定义 , 自定义文件在 resources 目录下的 customconfi/myconfig.properties文件中
@Configuration
@PropertySource(value = "classpath:customconfig/myconfig.properties")
public class MyPropertySource {
@Value("${user.age}")
public Integer age;
@Value("${user.home}")
public String home;
}
测试
public class PropertySourceTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
for (String beanDefinitionName : ac.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
System.out.println("************************************");
System.out.println("测试获取自定义 properties 文件 key - value 的值");
MyPropertySource myPropertySource = ac.getBean(MyPropertySource.class);
System.out.println("myPropertySource.age = " + myPropertySource.age);
System.out.println("myPropertySource.home = " + myPropertySource.home);
System.out.println("myPropertySource.dir = " + myPropertySource.dir);
}
}
控制台打印
测试获取自定义 properties 文件 key - value 的值 myPropertySource.age = 202 myPropertySource.home = C:\Users- myPropertySource.dir = D:\git-res\tmp\springboot-demo
sun.cpu.isalist amd64
sun.desktop windows
sun.io.unicode.encoding UnicodeLittle
sun.cpu.endian little
java.vendor.url.bug http://bugreport.sun.com/bugreport/
file.separator \
java.vendor Oracle Corporation
sun.boot.class.path D:\java-soft\jdk1.8\jre\lib\resources.jar;D:\java-soft\jdk1.8\jre\lib\rt.jar;D:\java-soft\jdk1.8\jre\lib\sunrsasign.jar;D:\java-soft\jdk1.8\jre\lib\jsse.jar;D:\java-soft\jdk1.8\jre\lib\jce.jar;D:\java-soft\jdk1.8\jre\lib\charsets.jar;D:\java-soft\jdk1.8\jre\lib\jfr.jar;D:\java-soft\jdk1.8\jre\classes
java.ext.dirs D:\java-soft\jdk1.8\jre\lib\ext;C:\Windows\Sun\Java\lib\ext
java.version 1.8.0_91
java.vm.info mixed mode
awt.toolkit sun.awt.windows.WToolkit
user.language zh
java.specification.vendor Oracle Corporation
sun.java.command com.example.configfilereload.PropertySourceTest
java.home D:\java-soft\jdk1.8\jre
sun.arch.data.model 64
java.vm.specification.version 1.8
java.class.path D:\java-soft\jdk1.8\jre\lib\charsets.jar;D:\java-soft\jdk1.8\jre\lib\deploy.jar;D:\java-soft\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\java-soft\jdk1.8\jre\lib\ext\cldrdata.jar;D:\java-soft\jdk1.8\jre\lib\ext\dnsns.jar;D:\java-soft\jdk1.8\jre\lib\ext\jaccess.jar;D:\java-soft\jdk1.8\jre\lib\ext\jfxrt.jar;D:\java-soft\jdk1.8\jre\lib\ext\localedata.jar;D:\java-soft\jdk1.8\jre\lib\ext\nashorn.jar;D:\java-soft\jdk1.8\jre\lib\ext\sunec.jar;D:\java-soft\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\java-soft\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\java-soft\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\java-soft\jdk1.8\jre\lib\ext\zipfs.jar;D:\java-soft\jdk1.8\jre\lib\javaws.jar;D:\java-soft\jdk1.8\jre\lib\jce.jar;D:\java-soft\jdk1.8\jre\lib\jfr.jar;D:\java-soft\jdk1.8\jre\lib\jfxswt.jar;D:\java-soft\jdk1.8\jre\lib\jsse.jar;D:\java-soft\jdk1.8\jre\lib\management-agent.jar;D:\java-soft\jdk1.8\jre\lib\plugin.jar;D:\java-soft\jdk1.8\jre\lib\resources.jar;D:\java-soft\jdk1.8\jre\lib\rt.jar;D:\git-res\tmp\springboot-demo\target\classes;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-autoconfigure\2.3.4.RELEASE\spring-boot-autoconfigure-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot\2.3.4.RELEASE\spring-boot-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-core\5.2.9.RELEASE\spring-core-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-jcl\5.2.9.RELEASE\spring-jcl-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-context\5.2.9.RELEASE\spring-context-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-configuration-processor\2.3.4.RELEASE\spring-boot-configuration-processor-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-starter-web\2.3.4.RELEASE\spring-boot-starter-web-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-starter\2.3.4.RELEASE\spring-boot-starter-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-starter-logging\2.3.4.RELEASE\spring-boot-starter-logging-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\java-soft\maven_repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\java-soft\maven_repo\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\java-soft\maven_repo\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\java-soft\maven_repo\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\java-soft\maven_repo\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\java-soft\maven_repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\java-soft\maven_repo\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-starter-json\2.3.4.RELEASE\spring-boot-starter-json-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\com\fasterxml\jackson\core\jackson-databind\2.11.2\jackson-databind-2.11.2.jar;D:\java-soft\maven_repo\com\fasterxml\jackson\core\jackson-annotations\2.11.2\jackson-annotations-2.11.2.jar;D:\java-soft\maven_repo\com\fasterxml\jackson\core\jackson-core\2.11.2\jackson-core-2.11.2.jar;D:\java-soft\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.2\jackson-datatype-jdk8-2.11.2.jar;D:\java-soft\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.2\jackson-datatype-jsr310-2.11.2.jar;D:\java-soft\maven_repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.2\jackson-module-parameter-names-2.11.2.jar;D:\java-soft\maven_repo\org\springframework\boot\spring-boot-starter-tomcat\2.3.4.RELEASE\spring-boot-starter-tomcat-2.3.4.RELEASE.jar;D:\java-soft\maven_repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.38\tomcat-embed-core-9.0.38.jar;D:\java-soft\maven_repo\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\java-soft\maven_repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.38\tomcat-embed-websocket-9.0.38.jar;D:\java-soft\maven_repo\org\springframework\spring-web\5.2.9.RELEASE\spring-web-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-beans\5.2.9.RELEASE\spring-beans-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-webmvc\5.2.9.RELEASE\spring-webmvc-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-aop\5.2.9.RELEASE\spring-aop-5.2.9.RELEASE.jar;D:\java-soft\maven_repo\org\springframework\spring-expression\5.2.9.RELEASE\spring-expression-5.2.9.RELEASE.jar;C:\Users\-\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\211.7628.21\lib\idea_rt.jar
user.name -
file.encoding UTF-8
java.specification.version 1.8
java.awt.printerjob sun.awt.windows.WPrinterJob
user.timezone GMT+08:00
user.home C:\Users\-
os.version 10.0
sun.management.compiler HotSpot 64-Bit Tiered Compilers
java.specification.name Java Platform API Specification
java.class.version 52.0
java.library.path D:\java-soft\jdk1.8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;D:\java-soft\vscode\Microsoft VS Code\bin;D:\java-soft\Git\Git\cmd;D:\java-soft\jdk1.8\bin;D:\java-soft\apache-maven-3.6.1\bin;D:\java-soft\Git\Git\usr\bin;D:\java-soft\vagrant\bin;D:\nodeJs\;C:\Users\-\AppData\Local\Microsoft\WindowsApps;C:\Users\-\AppData\Roaming\npm;C:\Users\-\AppData\Local\JetBrains\Toolbox\scripts;.
sun.jnu.encoding GBK
os.name Windows 10
user.variant
java.vm.specification.vendor Oracle Corporation
java.io.tmpdir C:\Users\-\AppData\Local\Temp\
line.separator
java.endorsed.dirs D:\java-soft\jdk1.8\jre\lib\endorsed
os.arch amd64
java.awt.graphicsenv sun.awt.Win32GraphicsEnvironment
java.runtime.version 1.8.0_91-b15
java.vm.specification.name Java Virtual Machine Specification
user.dir D:\git-res\tmp\springboot-demo
user.country CN
user.script
sun.java.launcher SUN_STANDARD
sun.os.patch.level
java.vm.name Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg sun.io
path.separator ;
java.vm.vendor Oracle Corporation
java.vendor.url http://java.oracle.com/
sun.boot.library.path D:\java-soft\jdk1.8\jre\bin
java.vm.version 25.91-b15
java.runtime.name Java(TM) SE Runtime Environment
|