Spring 没有解析 application.yml 中的#{systemEnvironment['NAME']}
Spring doesn't resolve #{systemEnvironment['NAME']} in application.yml
当我在 application.yml
中指定一个简单的绝对路径时,一切正常,但是当我尝试用环境变量的值替换项目主目录时,它不起作用。
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
native:
# searchLocations: file:#{systemEnvironment['PROJECT_HOME']}/backend/cloud-config/{application}
searchLocations: file:/temp/pets/backend/cloud-config/{application}
在运行时,未解析的值 (file:#{systemEnvironment['PROJECT_HOME']}/backend/cloud-config/some-service
) 被传递到 org.springframework.cloud.config.server.environment.NativeEnvironmentRepository#getLocations
,并且
if (isDirectory(value)) {
output.add(value);
}
显然 isDirectory(value)
returns 错误,最终 Spring 的 spring.config.location=
为空。
PS:
使用:spring-cloud-config-server-1.2.2.RELEASE
堆栈跟踪:
2018-10-12 11:02:27.221 INFO 10392 --- [ main] t.m.c.ConfigServerApplication : Started ConfigServerApplication in 4.058 seconds (JVM running for 4.67)
2018-10-12 11:02:27.437 ERROR 10392 --- [on(4)-127.0.0.1] o.s.boot.SpringApplication : Application startup failed
java.lang.IllegalArgumentException: Invalid argument syntax: --spring.config.location=
at org.springframework.core.env.SimpleCommandLineArgsParser.parse(SimpleCommandLineArgsParser.java:75) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.core.env.SimpleCommandLinePropertySource.<init>(SimpleCommandLinePropertySource.java:87) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.boot.DefaultApplicationArguments$Source.<init>(DefaultApplicationArguments.java:76) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.DefaultApplicationArguments.<init>(DefaultApplicationArguments.java:42) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.config.server.environment.NativeEnvironmentRepository.findOne(NativeEnvironmentRepository.java:110) [spring-cloud-config-server-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.cloud.config.server.config.ConfigServerHealthIndicator.doHealthCheck(ConfigServerHealthIndicator.java:51) [spring-cloud-config-server-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:38) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:81) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:35) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.endpoint.jmx.DataEndpointMBean.getData(DataEndpointMBean.java:48) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) [na:1.8.0_181]
at sun.reflect.GeneratedMethodAccessor102.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) [na:1.8.0_181]
at javax.management.modelmbean.RequiredModelMBean.run(RequiredModelMBean.java:1252) [na:1.8.0_181]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_181]
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) [na:1.8.0_181]
at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1246) [na:1.8.0_181]
at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:1085) [na:1.8.0_181]
at org.springframework.jmx.export.SpringModelMBean.invoke(SpringModelMBean.java:90) [spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at javax.management.modelmbean.RequiredModelMBean.getAttribute(RequiredModelMBean.java:1562) [na:1.8.0_181]
at org.springframework.jmx.export.SpringModelMBean.getAttribute(SpringModelMBean.java:109) [spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:647) [na:1.8.0_181]
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1445) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.access0(RMIConnectionImpl.java:76) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:639) [na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) [na:1.8.0_181]
at sun.rmi.transport.Transport.run(Transport.java:200) [na:1.8.0_181]
at sun.rmi.transport.Transport.run(Transport.java:197) [na:1.8.0_181]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_181]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=15=](TCPTransport.java:688) [na:1.8.0_181]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_181]
bash:
user@userspc:~$ printenv | grep PROJECT_HOME
PROJECT_HOME=/temp/pets
application.yml
不支持 SpEL;但是,您可以使用 属性 占位符。使用 ${PROJECT_HOME:unspecified}/...
(如果未设置环境变量,将使用 unspecified
。
当我在 application.yml
中指定一个简单的绝对路径时,一切正常,但是当我尝试用环境变量的值替换项目主目录时,它不起作用。
spring:
profiles:
active: native
cloud:
config:
discovery:
enabled: true
server:
native:
# searchLocations: file:#{systemEnvironment['PROJECT_HOME']}/backend/cloud-config/{application}
searchLocations: file:/temp/pets/backend/cloud-config/{application}
在运行时,未解析的值 (file:#{systemEnvironment['PROJECT_HOME']}/backend/cloud-config/some-service
) 被传递到 org.springframework.cloud.config.server.environment.NativeEnvironmentRepository#getLocations
,并且
if (isDirectory(value)) {
output.add(value);
}
显然 isDirectory(value)
returns 错误,最终 Spring 的 spring.config.location=
为空。
PS:
使用:spring-cloud-config-server-1.2.2.RELEASE
堆栈跟踪:
2018-10-12 11:02:27.221 INFO 10392 --- [ main] t.m.c.ConfigServerApplication : Started ConfigServerApplication in 4.058 seconds (JVM running for 4.67)
2018-10-12 11:02:27.437 ERROR 10392 --- [on(4)-127.0.0.1] o.s.boot.SpringApplication : Application startup failed
java.lang.IllegalArgumentException: Invalid argument syntax: --spring.config.location=
at org.springframework.core.env.SimpleCommandLineArgsParser.parse(SimpleCommandLineArgsParser.java:75) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.core.env.SimpleCommandLinePropertySource.<init>(SimpleCommandLinePropertySource.java:87) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.boot.DefaultApplicationArguments$Source.<init>(DefaultApplicationArguments.java:76) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.DefaultApplicationArguments.<init>(DefaultApplicationArguments.java:42) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.config.server.environment.NativeEnvironmentRepository.findOne(NativeEnvironmentRepository.java:110) [spring-cloud-config-server-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.cloud.config.server.config.ConfigServerHealthIndicator.doHealthCheck(ConfigServerHealthIndicator.java:51) [spring-cloud-config-server-1.2.2.RELEASE.jar:1.2.2.RELEASE]
at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:38) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.health.CompositeHealthIndicator.health(CompositeHealthIndicator.java:68) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:81) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.endpoint.HealthEndpoint.invoke(HealthEndpoint.java:35) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.boot.actuate.endpoint.jmx.DataEndpointMBean.getData(DataEndpointMBean.java:48) [spring-boot-actuator-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) [na:1.8.0_181]
at sun.reflect.GeneratedMethodAccessor102.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) [na:1.8.0_181]
at javax.management.modelmbean.RequiredModelMBean.run(RequiredModelMBean.java:1252) [na:1.8.0_181]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_181]
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) [na:1.8.0_181]
at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1246) [na:1.8.0_181]
at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:1085) [na:1.8.0_181]
at org.springframework.jmx.export.SpringModelMBean.invoke(SpringModelMBean.java:90) [spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at javax.management.modelmbean.RequiredModelMBean.getAttribute(RequiredModelMBean.java:1562) [na:1.8.0_181]
at org.springframework.jmx.export.SpringModelMBean.getAttribute(SpringModelMBean.java:109) [spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:647) [na:1.8.0_181]
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1445) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.access0(RMIConnectionImpl.java:76) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) [na:1.8.0_181]
at javax.management.remote.rmi.RMIConnectionImpl.getAttribute(RMIConnectionImpl.java:639) [na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) [na:1.8.0_181]
at sun.rmi.transport.Transport.run(Transport.java:200) [na:1.8.0_181]
at sun.rmi.transport.Transport.run(Transport.java:197) [na:1.8.0_181]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_181]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=15=](TCPTransport.java:688) [na:1.8.0_181]
at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_181]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_181]
bash:
user@userspc:~$ printenv | grep PROJECT_HOME
PROJECT_HOME=/temp/pets
application.yml
不支持 SpEL;但是,您可以使用 属性 占位符。使用 ${PROJECT_HOME:unspecified}/...
(如果未设置环境变量,将使用 unspecified
。