【问题标题】:Spring Jndi Context and PropertyPlaceholderConfigurerSpring Jndi 上下文和 PropertyPlaceholderConfigurer
【发布时间】:2012-02-18 09:36:38
【问题描述】:

使用 Spring,我想在 Webspehere 的上下文中读取一个变量。

Read a Environment Variable in Java with Websphere

在 web.xml 中定义数据....

<env-entry>
   <env-entry-name>varName</env-entry-name>
   <env-entry-value>56</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

用java看

Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup(“varName”);

但我想在我的 common.xml 中获取数据,例如

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/context/servweb.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders">
        <value>true</value>
    </property>
</bean>

也许有类似的东西

 <constructor-arg>
     <jee:jndi-lookup jndi-name="java:comp/env" default-value="data" /> 
  </constructor-arg>

但在上下文中做同样的事情

Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup(“varName”);

也许是这样的:

 <constructor-arg>
    <jee:jndi-lookup  jndi-name="java:comp/env">
      <jee:environment>
          varName=default
     </jee:environment>
  </jee:jndi-lookup>

有人知道正确的方法吗?

提前致谢

【问题讨论】:

    标签: spring websphere environment-variables jndi


    【解决方案1】:

    您可以创建自己的 PropertyPlaceholderConfigurer

    public class JndiPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    
        private String jndiPrefix = "java:comp/env/";
        private JndiTemplate jndiTemplate = new JndiTemplate();
    
        @Override
        protected String resolvePlaceholder(String placeholder, Properties props) {
            String value = null;
            value = resolveJndiPlaceholder(placeholder);
            if (value == null) {
                value = super.resolvePlaceholder(placeholder, props);
            }
            return value;
        }
    
        private String resolveJndiPlaceholder(String placeholder) {
            try {
                String value = (String)jndiTemplate.lookup(jndiPrefix + placeholder, String.class);
                return value;
            } catch (NamingException e) {
                // ignore
            } 
            return null;
        }
    
        public void setJndiPrefix(String jndiPrefix) {
            this.jndiPrefix = jndiPrefix;
        }
    
        public void setJndiTemplate(JndiTemplate jndiTemplate) {
            this.jndiTemplate = jndiTemplate;
        }
    }
    

    然后在你的applicationContext.xml中使用它

    <bean id="propertyPlaceholderConfigurer"
          class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
        <property name="properties">
            <props>
                <prop key="varName">default</prop>
            </props>
        </property>
    </bean>
    

    或用于在属性文件中定义默认值

    <bean id="propertyPlaceholderConfigurer"
          class="mypkg.helper.JndiPropertyPlaceholderConfigurer">
        <property name="location" value="classpath:/defaults.properties"/>
    </bean>
    

    【讨论】:

    • 工作,没有问题.. 非常感谢
    【解决方案2】:

    我在我的网络应用程序中做同样的事情,但无法从 Initialcontext 读取

    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"/> 
    </bean> 
    

    my.properties 有

    default_mask=9999  
    

    尝试阅读

    Context context = new InitialContext();
    String resource = context.lookup("java:comp/env/default_mask");
    

    但上下文的绑定只有来自 web.xml 的 env-entry,而不来自属性文件

    【讨论】:

      【解决方案3】:

      如果您只想获取在容器上下文中定义的变量的值并将其用作字符串而不创建占位符对象,您可以执行以下操作(这在 Tomcat 中进行了测试,但很可能工作方式相同在其他容器/JEE 服务器(如 WebSphere)中):

      在Tomcat的context.xml中定义环境变量(或者使用自己服务器的语法):

      <Environment type="java.lang.String" name="myString" value="hello"/>
      

      在 Spring XML 上下文文件中:

      将 jee 命名空间添加到根元素:

      xmlns:jee="http://www.springframework.org/schema/jee"
      

      xsi:schemaLocation

      http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
      

      现在您可以轻松地查找一个值(请注意,您不必指定 java:/comp/env 的东西,Spring 会为您做到这一点):

      <jee:jndi-lookup id="myStringValue"
                           jndi-name="myStringValue"
                           expected-type="java.lang.String" />
      

      然后你可以使用它,例如将它作为参考传递给一个bean的构造函数:

      <bean id="observationFileManager" class="my.service.Bean">
          <constructor-arg name="myString" ref="myStringValue" />
      </bean>
      

      bean 将接收 "hello" 作为其构造参数。

      编辑:

      如果您在容器(Tomcat、Websphere...)之外运行 Spring 上下文以进行集成测试,则查找将不起作用。因此,如果您有特殊的测试上下文,只需添加以下 String 定义,该定义将覆盖 jee:lookup 并设置您要用于测试的值:

      <!-- This overrides the jndi jee:lookup used in the real context -->
      <bean id="mediaFilesBaseDirPath" class="java.lang.String" >
          <constructor-arg value="Z:" />
      </bean>
      

      【讨论】:

        【解决方案4】:
        <bean id="propertyConfigurer" 
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location">  
        <value>classpath:resources/my-jndi.properties</value>       
        </property>  
        </bean>
        

        在 my-jndi.properties 中: jndi-qconnfactory=jms/QConnFactory

        <jee:jndi-lookup id="connectionFactory" jndi-name="${jndi-qconnfactory}"/>
        

        【讨论】:

          猜你喜欢
          • 2012-05-06
          • 2014-05-03
          • 2011-11-24
          • 2014-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-20
          • 1970-01-01
          相关资源
          最近更新 更多