简体   繁体   English

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

[英]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 只是在做以下事情

  1. If there is a property placeholder referred in the context file (like ${my.name} ), then it will resolve it from JNDI (assuming its already there) 如果上下文文件中引用了一个属性占位符(例如${my.name} ),那么它将从JNDI解析它(假设它已经存在)
  2. If its not available from JNDI then resolve it to some default read from a property file. 如果无法从JNDI获得,则将其解析为从属性文件读取的一些默认值。
  3. Nowhere its details about Binding variables to JNDI. 没有任何有关将变量绑定到JNDI的详细信息。 There is no reference to 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树的方法,一种方法是

  1. You can create a class JndiPropertyBinder and inject jndiTemplate in it. 您可以创建一个类JndiPropertyBinder并将jndiTemplate注入其中。
  2. Inject your property file in this class 将您的属性文件插入此类
  3. Now write a init hook on the bean where in it will read all the property in the file and bind them to jndi tree. 现在在bean上写一个init钩子 ,它将在其中读取文件中的所有属性并将它们绑定到jndi树。
  4. Make this bean to load very early in order so that its loaded before all the other beans which are using it. 使此Bean尽早加载,以便使其在使用它的所有其他Bean之前加载。

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

相关问题 结合使用context:property-override和PropertyPlaceholderConfigurer - Combinating context:property-override and PropertyPlaceholderConfigurer 春季:在应用程序上下文之外使用PropertyPlaceholderConfigurer支持 - Spring: Using PropertyPlaceholderConfigurer support outside of application context Maven项目:Spring无法使用PropertyPlaceholderConfigurer在src / main / resources中找到属性文件 - Maven Project: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurer 不使用PropertyPlaceholderConfigurer加载属性文件 - Load a property file without using PropertyPlaceholderConfigurer Websphere MQ无法创建初始JNDI上下文 - Websphere MQ unable to create initial JNDI context 在多个项目/模块中使用多个属性文件(通过PropertyPlaceholderConfigurer) - Using multiple property files (via PropertyPlaceholderConfigurer) in multiple projects/modules PropertyPlaceholderConfigurer类中的locations属性 - locations property in PropertyPlaceholderConfigurer class 修改 PropertyPlaceholderConfigurer 的属性 - Modify property of PropertyPlaceholderConfigurer Spring 3.5 设置 xml 属性,使用 PropertyPlaceholderConfigurer 处理默认值 - Spring 3.5 Setting an xml property, handling the default value using PropertyPlaceholderConfigurer 无法找到JMS聊天应用程序的JNDI资源 - Unable to find JNDI resource for JMS Chat Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM