tomcat 没有读取环境变量
tomcat not reading environment variables
我在 AKS 上有一个 tomcat 应用程序 运行。我创建了一堆环境变量,所以当 tomcat 启动时,它可以替换值。当我在容器中使用 printenv
时,它显示了通过 deployment.yaml.
设置的所有环境变量
但是tomcat启动的时候,还是不识别环境变量,提示是这样的:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PACSDemoController': Invocation of init method failed; nested exception is java.net.URISyntaxException: Illegal character in path at index 1: ${widgetsRestBaseUrl}
这是我的 class:
public class BaseDemoController {
protected URI restUri;
@Resource(name = "widgetsRestBaseUrl")
protected String widgetsRestBaseUrl;
@Resource(name = "widgetsServerUploaderUrl")
protected String widgetsServerUploaderUrl;
@Resource(name = "widgetsServerViewerUrl")
protected String widgetsServerViewerUrl;
@Resource(name = "widgetsServerStudySelectionUrl")
protected String widgetsServerStudySelectionUrl;
@Resource(name = "widgetsServerHieStudySelectionUrl")
protected String widgetsServerHieStudySelectionUrl;
@Resource(name = "widgetsServerPatientHistoryUrl")
protected String widgetsServerPatientHistoryUrl;
@Resource(name = "widgetsServerShareImageUrl")
protected String widgetsServerShareImageUrl;
@Resource(name = "widgetServerPacsConfigUrl")
protected String widgetServerPacsConfigUrl;
@Resource(name = "apiNominate")
protected String apiNominate;
@Resource(name = "apiPacsConfig")
protected String apiPacsConfig;
@Resource(name = "widgetsServerNominatePacsUrl")
protected String widgetsServerNominatePacsUrl;
@Resource(name = "apiBaseUrl")
protected String apiBaseUrl;
@Resource(name = "widgetsBasicPacsConfigUrl")
protected String widgetsBasicPacsConfigUrl;
@Resource(name = "auth0Domain")
protected String auth0Domain;
@Resource(name = "widgetAuth0Audience")
protected String widgetAuth0Audience;
@Resource(name = "apiAuth0Audience")
protected String apiAuth0Audience;
@Autowired
protected EpicTokenService epicTokenService;
@PostConstruct
public void initialize() throws URISyntaxException {
restUri = new URI(widgetsRestBaseUrl);
}
所有这些资源都来自jndi.xml
:
?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<bean id="apiBaseUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiBaseUrl}"/>
</bean>
<bean id="widgetsRestBaseUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsRestBaseUrl}"/>
</bean>
<bean id="widgetsServerUploaderUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerUploaderUrl}"/>
</bean>
<bean id="widgetsServerViewerUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerViewerUrl}"/>
</bean>
<bean id="widgetsServerStudySelectionUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerStudySelectionUrl}"/>
</bean>
<bean id="widgetsServerPatientHistoryUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerPatientHistoryUrl}"/>
</bean>
<bean id="widgetsServerShareImageUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerShareImageUrl}"/>
</bean>
<bean id="widgetServerPacsConfigUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetServerPacsConfigUrl}"/>
</bean>
<bean id="apiPacsConfig" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiPacsConfig}"/>
</bean>
<bean id="apiNominate" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiNominate}"/>
</bean>
<bean id="widgetsServerNominatePacsUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerNominatePacsUrl}"/>
</bean>
<bean id="widgetsBasicPacsConfigUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsBasicPacsConfigUrl}"/>
</bean>
<bean id="widgetsServerHieStudySelectionUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerHieStudySelectionUrl}"/>
</bean>
<bean id="apiAuth0Audience" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiAuth0Audience}"/>
</bean>
<bean id="widgetAuth0Audience" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetAuth0Audience}"/>
</bean>
<bean id="auth0Domain" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${auth0Domain}"/>
</bean>
</beans>
我不确定为什么这不起作用。我有两个几乎完全相同的项目,一个项目没有问题。有什么建议吗?
更新:PACDemoController 扩展自 BaseDemoController:
@Controller
public class PACSDemoController extends BaseDemoController {
错误来自 BaseDemoController 中的@PostConstruct。这是日志:
Caused by: java.net.URISyntaxException: Illegal character in path at index 1: ${widgetsRestBaseUrl}
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parseHierarchical(URI.java:3105)
at java.net.URI$Parser.parse(URI.java:3063)
at java.net.URI.<init>(URI.java:588)
at com.accelarad.smr.widgets.demo.BaseDemoController.initialize(BaseDemoController.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
... 27 more
Tomcat与将${...}
替换为相应系统环境变量的值没有任何关系。它仅在自己的配置文件(server.xml
、各种 context.xml
描述符等)中替换这些值。
如果您希望 Spring 替换那些 属性 占位符 ${...}
,您需要将 PropertySourcesPlaceholderConfigurer
添加到您的应用程序上下文中。这可以通过以下方式完成:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder />
...
</beans>
然而,直接从 Spring Environment
using the @Value
注释中注入 属性 值比创建数百个 java.lang.String
.[=21 类型的 beans 更容易=]
我在 AKS 上有一个 tomcat 应用程序 运行。我创建了一堆环境变量,所以当 tomcat 启动时,它可以替换值。当我在容器中使用 printenv
时,它显示了通过 deployment.yaml.
但是tomcat启动的时候,还是不识别环境变量,提示是这样的:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PACSDemoController': Invocation of init method failed; nested exception is java.net.URISyntaxException: Illegal character in path at index 1: ${widgetsRestBaseUrl}
这是我的 class:
public class BaseDemoController {
protected URI restUri;
@Resource(name = "widgetsRestBaseUrl")
protected String widgetsRestBaseUrl;
@Resource(name = "widgetsServerUploaderUrl")
protected String widgetsServerUploaderUrl;
@Resource(name = "widgetsServerViewerUrl")
protected String widgetsServerViewerUrl;
@Resource(name = "widgetsServerStudySelectionUrl")
protected String widgetsServerStudySelectionUrl;
@Resource(name = "widgetsServerHieStudySelectionUrl")
protected String widgetsServerHieStudySelectionUrl;
@Resource(name = "widgetsServerPatientHistoryUrl")
protected String widgetsServerPatientHistoryUrl;
@Resource(name = "widgetsServerShareImageUrl")
protected String widgetsServerShareImageUrl;
@Resource(name = "widgetServerPacsConfigUrl")
protected String widgetServerPacsConfigUrl;
@Resource(name = "apiNominate")
protected String apiNominate;
@Resource(name = "apiPacsConfig")
protected String apiPacsConfig;
@Resource(name = "widgetsServerNominatePacsUrl")
protected String widgetsServerNominatePacsUrl;
@Resource(name = "apiBaseUrl")
protected String apiBaseUrl;
@Resource(name = "widgetsBasicPacsConfigUrl")
protected String widgetsBasicPacsConfigUrl;
@Resource(name = "auth0Domain")
protected String auth0Domain;
@Resource(name = "widgetAuth0Audience")
protected String widgetAuth0Audience;
@Resource(name = "apiAuth0Audience")
protected String apiAuth0Audience;
@Autowired
protected EpicTokenService epicTokenService;
@PostConstruct
public void initialize() throws URISyntaxException {
restUri = new URI(widgetsRestBaseUrl);
}
所有这些资源都来自jndi.xml
:
?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<bean id="apiBaseUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiBaseUrl}"/>
</bean>
<bean id="widgetsRestBaseUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsRestBaseUrl}"/>
</bean>
<bean id="widgetsServerUploaderUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerUploaderUrl}"/>
</bean>
<bean id="widgetsServerViewerUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerViewerUrl}"/>
</bean>
<bean id="widgetsServerStudySelectionUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerStudySelectionUrl}"/>
</bean>
<bean id="widgetsServerPatientHistoryUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerPatientHistoryUrl}"/>
</bean>
<bean id="widgetsServerShareImageUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerShareImageUrl}"/>
</bean>
<bean id="widgetServerPacsConfigUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetServerPacsConfigUrl}"/>
</bean>
<bean id="apiPacsConfig" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiPacsConfig}"/>
</bean>
<bean id="apiNominate" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiNominate}"/>
</bean>
<bean id="widgetsServerNominatePacsUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerNominatePacsUrl}"/>
</bean>
<bean id="widgetsBasicPacsConfigUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsBasicPacsConfigUrl}"/>
</bean>
<bean id="widgetsServerHieStudySelectionUrl" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetsServerHieStudySelectionUrl}"/>
</bean>
<bean id="apiAuth0Audience" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${apiAuth0Audience}"/>
</bean>
<bean id="widgetAuth0Audience" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${widgetAuth0Audience}"/>
</bean>
<bean id="auth0Domain" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${auth0Domain}"/>
</bean>
</beans>
我不确定为什么这不起作用。我有两个几乎完全相同的项目,一个项目没有问题。有什么建议吗?
更新:PACDemoController 扩展自 BaseDemoController:
@Controller
public class PACSDemoController extends BaseDemoController {
错误来自 BaseDemoController 中的@PostConstruct。这是日志:
Caused by: java.net.URISyntaxException: Illegal character in path at index 1: ${widgetsRestBaseUrl}
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parseHierarchical(URI.java:3105)
at java.net.URI$Parser.parse(URI.java:3063)
at java.net.URI.<init>(URI.java:588)
at com.accelarad.smr.widgets.demo.BaseDemoController.initialize(BaseDemoController.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
... 27 more
Tomcat与将${...}
替换为相应系统环境变量的值没有任何关系。它仅在自己的配置文件(server.xml
、各种 context.xml
描述符等)中替换这些值。
如果您希望 Spring 替换那些 属性 占位符 ${...}
,您需要将 PropertySourcesPlaceholderConfigurer
添加到您的应用程序上下文中。这可以通过以下方式完成:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder />
...
</beans>
然而,直接从 Spring Environment
using the @Value
注释中注入 属性 值比创建数百个 java.lang.String
.[=21 类型的 beans 更容易=]