繁体   English   中英

无法使用PropertyPlaceholderConfigurer在JNDI上下文中找到属性

[英]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上下文”。

您引用的示例不这样做。 只是在做以下事情

  1. 如果上下文文件中引用了一个属性占位符(例如${my.name} ),那么它将从JNDI解析它(假设它已经存在)
  2. 如果无法从JNDI获得,则将其解析为从属性文件读取的一些默认值。
  3. 没有任何有关将变量绑定到JNDI的详细信息。 没有对bind()方法的引用。

现在, 要解决您的问题,即获得某种读取属性文件并将其绑定到JNDI树的方法,一种方法是

  1. 您可以创建一个类JndiPropertyBinder并将jndiTemplate注入其中。
  2. 将您的属性文件插入此类
  3. 现在在bean上写一个init钩子 ,它将在其中读取文件中的所有属性并将它们绑定到jndi树。
  4. 使此Bean尽早加载,以便使其在使用它的所有其他Bean之前加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM