![](/img/trans.png)
[英]Combinating context:property-override and PropertyPlaceholderConfigurer
[英]Unable to find property in JNDI context using PropertyPlaceholderConfigurer
我想从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>
所以我创建了一个具有(c:/my.properties)的属性文件
default_mask=9999
所以我尝试使用现有的解决方案,例如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"/>
启动Tomcat服务器读取属性文件,例如
.......com.test.webappl.JndiPropertyPlaceholderConfigurer] Loading properties file from URL [file:c:/my.properties]
现在在Java中,当我阅读
Context context = new InitialContext();
String resource = context.lookup("java:comp/env/default_mask");
应用程序引发以下错误
**javax.naming.NameNotFoundException: Name default_mask is not bound in this Context**
我在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>
有人知道我是否使用正确的方法吗? 我知道在Spring Jndi Context和PropertyPlaceholderConfigurer中已经回答了这个问题,但是在我的情况下不起作用
提前致谢
如果要将任何内容部署到任何应用程序服务器,则最好将所有相关资源打包到部署单元中(以您的情况为准)。
要回答您的问题-如果您正在使用spring将任何东西注入JNDI容器,则还应该让spring为您找到所有东西。
所以你不能用
new InitialContext(); // this has nothing to do with spring.
希望这可以帮助 :)
您正在尝试(或期望)做的是“将名称值对从my.properties
到JNDI上下文”。
但
您引用的示例不这样做。 只是在做以下事情
${my.name}
),那么它将从JNDI解析它(假设它已经存在) bind()
方法的引用。 现在, 要解决您的问题,即获得某种读取属性文件并将其绑定到JNDI树的方法,一种方法是
JndiPropertyBinder
并将jndiTemplate
注入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.