【问题标题】:JNDI properties (java.naming.factory.initial & java.naming.provider.url) not getting set through Spring's PropertyPlaceholderConfigurer未通过 Spring 的 PropertyPlaceholderConfigurer 设置 JNDI 属性(java.naming.factory.initial 和 java.naming.provider.url)
【发布时间】:2014-05-03 23:17:08
【问题描述】:

我已经配置了以下PropertyPlaceholderConfigurer

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:///opt/myproject/jndi.properties" />
</bean>

我的 JNDI 模板 bean 看起来像

<bean id="myJndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">${factory}</prop>
            <prop key="java.naming.provider.url">${url}</prop>
        <props>
    </property>
</bean>

jndi.properties 文件定义了两个键。

factory=org.webmethods.jms.naming.WmJmsNamingCtxFactory
url=wmjmsnaming://MY_BROKER@abc.com:7001

当我在 weblogic 上部署它并启动应用程序时,我看到以下跟踪

nested exception is javax.naming.NoInitialContextException: Cannot instantiate class: ${factory} [Root exception is java.lang.ClassNotFoundExcep
tion: ${factory}]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
        at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:481)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:181)
        at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1868)
        at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3154)
        at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1518)
        at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:484)

Spring 不会用属性文件中的值替换 ${factory},而是将 ${factory} 视为值,因此会显示类未找到异常。 如果我对工厂类名称和 url 进行硬编码,它可以正常工作。 我不确定这里缺少什么,因为我无法弄清楚真正的问题。

感谢任何关于此的帮助或指示。

【问题讨论】:

    标签: java spring properties jms jndi


    【解决方案1】:

    我发现了另一种更简单的方法,不需要额外的类。我使用org.springframework.beans.factory.config.PropertiesFactoryBean 加载 jndi.properties

    jndiTemplate bean 看起来像这样。

    <bean id="myJndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
                 <property name="location" value="file:///opt/myproject/jndi.properties" />
            </bean>
        </property>
    </bean>
    

    【讨论】:

      【解决方案2】:

      你也可以像这样使用springs PropertiesFactoryBean

      <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="location" value="file:///opt/myproject/jndi.properties" />
      </bean>
      

      【讨论】:

        【解决方案3】:

        我设法找到了一个对我有用的解决方案。创建我自己的 JndiTemplate 并从服务器显式读取 jndi.properties 文件。自定义类看起来像这样

        public class MyJndiTemplate extends JndiTemplate {
        
            @Override
            protected Context createInitialContext() throws NamingException {
                Properties jndiProperties =  new Properties();
                final File file = new File("/opt/myproject/jndi.properties");
                try {
                    jndiProperties.load(new FileInputStream(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new InitialContext(jndiProperties);
            }
        }
        

        然后像这样在spring上下文xml中使用这个类

        <bean id="myJndiTemplate" class="com.MyJndiTemplate">
        

        希望这可以帮助任何面临同样问题的人。

        【讨论】:

          猜你喜欢
          • 2012-02-18
          • 1970-01-01
          • 2015-03-07
          • 2011-08-23
          • 1970-01-01
          • 2021-01-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多