如何在PropertyPlaceholderConfigurer中获取tomcat环境变量(How to get a tomcat Environment variable in a Property

编程入门 行业动态 更新时间:2024-10-15 18:22:58
如何在PropertyPlaceholderConfigurer中获取tomcat环境变量(How to get a tomcat Environment variable in a PropertyPlaceholderConfigurer)

我尝试在我的jpa-spring.xml文件中的'PropertyPlaceholderConfigurer'中获取Tomcat的server.xml中指定的Environment变量。

到目前为止,设置如下:

Tomcat server.xml

<Environment description="Identifies the server environement" name="server-env" type="java.lang.String" value="dev" />

在WebContent/META-INF/context.xml :

<Context> <ResourceLink name="server-env" global="server-env" type="java.lang.String"/> </Context>

在WebContent/WEB-INF/web.xml引用的内容如下:

<resource-env-ref> <description>Identifies server environement</description> <resource-env-ref-name>server-env</resource-env-ref-name> <resource-env-ref-type>java.lang.String</resource-env-ref-type> </resource-env-ref> <!-- Spring Integration --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/jpa-spring.xml </param-value> </context-param>

在/WEB-INF/config/jpa-spring.xml我尝试将该变量作为替换:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>WEB-INF/config/db.${server-env}.properties</value> </list> </property> </bean>

这是我使用网上发现的几篇文章中的信息整理的设置。

但是,我得到一个像......的错误

Could not resolve placeholder 'server-env' in [WEB-INF/config/db.${server-env}.properties] as system property: neither system property nor environment variable found 05 Nov 2011 14:45:13,385 org.springframework.web.context.ContextLoader ERROR Context initialization failed org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/config/db.${server-env}.properties]

...当启动tomcat时。

什么是实现我正在寻找的正确方法?

我知道这个问题和这个问题类似。 但是,我甚至无法用这些答案中的信息弄明白。

I try to get an Environment variable specified in Tomcat's server.xml in a 'PropertyPlaceholderConfigurer' located in my jpa-spring.xml file.

So far, the setup looks as follows:

Tomcat server.xml

<Environment description="Identifies the server environement" name="server-env" type="java.lang.String" value="dev" />

The in WebContent/META-INF/context.xml:

<Context> <ResourceLink name="server-env" global="server-env" type="java.lang.String"/> </Context>

Which is referenced in WebContent/WEB-INF/web.xml like:

<resource-env-ref> <description>Identifies server environement</description> <resource-env-ref-name>server-env</resource-env-ref-name> <resource-env-ref-type>java.lang.String</resource-env-ref-type> </resource-env-ref> <!-- Spring Integration --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/jpa-spring.xml </param-value> </context-param>

And in /WEB-INF/config/jpa-spring.xml I try to get that variable as a replacement:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>WEB-INF/config/db.${server-env}.properties</value> </list> </property> </bean>

This is a setup I put together using information from several articles found on the web.

However, I get an error like ...

Could not resolve placeholder 'server-env' in [WEB-INF/config/db.${server-env}.properties] as system property: neither system property nor environment variable found 05 Nov 2011 14:45:13,385 org.springframework.web.context.ContextLoader ERROR Context initialization failed org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/config/db.${server-env}.properties]

... when starting tomcat.

What is the right approach to achieve what I am looking for?

I know that this question is similar to this, and this question. However, I even couldn't figure it out with the information from these answers.

最满意答案

这是我的建议

使用当前设置,读取JNDI属性server-env并使用相同的方法来加载属性文件真的很复杂。 组装spring应用程序(和PropertyPlaceholderConfigurer )的方式,spring将尝试首先在OS环境中查找属性server-env ,然后在java系统属性中查找(使用-D选项从命令传递)。 它在这两个地方都找不到它,因此失败了。 因此,目前最简单的方法是传递应用程序服务器的server-env form命令提示符的值(调用java的地方;典型的语法是-Dserver-env = dev)。 我把它留给你弄清楚。 如果上面的选项看起来有点复杂,另一个更简单的方法是将名称为server-env的环境变量设置为适当的值(在Windows上set server-env=dev dev.Plz检查尊重操作系统文档)。

Here are my suggestion

With the current set up, its really going to be complicated to read JNDI property server-env and use the same to load the property file. The way you have assembled the spring application (and PropertyPlaceholderConfigurer), spring will try to look for the property server-env first in OS environment then in java system properties (passed from command using -D option). It finds it at neither of these places and hence fails. So currently the easiest way out right now is to pass the value of server-env form command prompt of your application server (where you invoke java ; typical syntax would be -Dserver-env=dev). I leave this to you to figure out. if above option appears a bit complicated, another easier way out is set an environment variable with name server-env to its appropriate values (on Windows its set server-env=dev. Plz check respect OS documentations for this).

更多推荐

本文发布于:2023-08-05 22:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境变量   如何在   PropertyPlaceholderConfigurer   tomcat   Environment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!