【问题标题】:Unable to find property in JNDI context using PropertyPlaceholderConfigurer无法使用 PropertyPlaceholderConfigurer 在 JNDI 上下文中找到属性
【发布时间】:2012-05-06 04:38:39
【问题描述】:

我想从 WEb.XML 中删除 env-entry

<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 and 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 中的 spring 设置也是

<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 and PropertyPlaceholderConfigurer 中得到了回答,但不知何故不适用于我的情况

提前致谢

【问题讨论】:

    标签: java spring properties jndi


    【解决方案1】:

    如果您要将任何东西部署到任何应用程序服务器,最好将所有相关资源打包到部署单元(在您的情况下是战争)。

    回答您的问题 - 如果您使用 spring 向 JNDI 容器注入任何内容,您还应该让 spring 为您定位所有内容。

    所以你不能使用

    new InitialContext(); // this has nothing to do with spring.
    

    希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      您正在尝试(或期望)做的是“将名称值对从您的 my.properties 绑定到 JNDI 上下文”。

      但是

      您提到的示例没有这样做。只是做以下事情

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

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

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

      【讨论】:

      • 仍然,这个解决方案是否适用于普通的 new InitialContext() ?您不必从 Spring 本身查找属性吗?
      • plain new InitialContext() 仍然可以使用。 JNDI 不是 spring 的一部分。它是一个不同的实体。InitialContext 允许您与 JNDI 树进行交互。事实上,jndiTemplate 内部使用了“InitialContext”。见this链接
      • Tomcat 在尝试绑定键/值 @java:comp/env 时抛出错误......上下文是只读的
      • 哦,是的。本地 JNDI 上下文是只读的。同时我明白了这一点,我想了解为什么您需要 JNDI 中的这些变量,您可以通过标准 PropertyPlaceholderConfigurer 从春季的普通属性文件中选择它们?
      • 实际上,我们的应用程序使用 JNDI java:comp/env/xxxxx 从初始上下文中读取 env 变量,现在我们只想将它们移动/覆盖到外部属性文件,而无需更改应用程序内部检索它们
      猜你喜欢
      • 2012-02-18
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多