[英]Unable to find property in JNDI context using PropertyPlaceholderConfigurer
I want to remove env-entry from WEb.XML 我想从WEb.XML删除环境
<env-entry>
<description>String used in masking process</description>
<env-entry-name>default_mask</env-entry-name>
<env-entry-value>*</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
so I created a properties file having (c:/my.properties) 所以我创建了一个具有(c:/my.properties)的属性文件
default_mask=9999
So I try to use the existing solution like JndiPropertyPlaceholderConfigurer (from Spring Jndi Context and PropertyPlaceholderConfigurer ) and configured following in spring's applicationcontext.xml as 所以我尝试使用现有的解决方案,例如JndiPropertyPlaceholderConfigurer(来自Spring Jndi Context和PropertyPlaceholderConfigurer ),并在spring的applicationcontext.xml中将其配置为
<bean
class="com.test.webappl.JndiPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="location" value="file:c:\my.properties"/>
Starting Tomcat server reads the property file like 启动Tomcat服务器读取属性文件,例如
.......com.test.webappl.JndiPropertyPlaceholderConfigurer] Loading properties file from URL [file:c:/my.properties]
Now in java when I read 现在在Java中,当我阅读
Context context = new InitialContext();
String resource = context.lookup("java:comp/env/default_mask");
application throws following error 应用程序引发以下错误
**javax.naming.NameNotFoundException: Name default_mask is not bound in this Context**
also my spring setting in web.xml are 我在web.xml中的春季设置也是
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
Anybody knows if I'm using the correct way? 有人知道我是否使用正确的方法吗? I know this has been answered in Spring Jndi Context and PropertyPlaceholderConfigurer but somehow not working in my case 我知道在Spring Jndi Context和PropertyPlaceholderConfigurer中已经回答了这个问题,但是在我的情况下不起作用
Thanks in Advance 提前致谢
If you're deploying anything to any application server it's a good idea to package all relevant resources to the deployment unit (war in your case). 如果要将任何内容部署到任何应用程序服务器,则最好将所有相关资源打包到部署单元中(以您的情况为准)。
To answer your question - if you're using spring to inject anything to the JNDI container, you should also let spring locate everything for you. 要回答您的问题-如果您正在使用spring将任何东西注入JNDI容器,则还应该让spring为您找到所有东西。
So you can't use 所以你不能用
new InitialContext(); // this has nothing to do with spring.
Hope this helps :) 希望这可以帮助 :)
What you are trying (or expecting) to do is "Bind the name value pairs from your my.properties
to JNDI context". 您正在尝试(或期望)做的是“将名称值对从my.properties
到JNDI上下文”。
BUT 但
The example that you referred does not DO that. 您引用的示例不这样做。 Is simply does following things 只是在做以下事情
${my.name}
), then it will resolve it from JNDI (assuming its already there) 如果上下文文件中引用了一个属性占位符(例如${my.name}
),那么它将从JNDI解析它(假设它已经存在) bind()
method. 没有对bind()
方法的引用。 Now, To solve your problem ie get some way of reading the properties file and bind that to JNDI tree, one way would be following 现在, 要解决您的问题,即获得某种读取属性文件并将其绑定到JNDI树的方法,一种方法是
JndiPropertyBinder
and inject jndiTemplate
in it. 您可以创建一个类JndiPropertyBinder
并将jndiTemplate
注入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.