Spring,JndiTemplate外化提供程序URL

gol*_*dio 5 spring jms

我的项目需要从属性文件加载初始上下文工厂和提供程序URL.这是我的Spring配置

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

<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true" depends-on="jndiTemplate">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>${queue.connection.factory}</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

这是我的Spring容器初始化的方法

    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    ppc.setProperties(ConfigManager.getProperties());
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    context.addBeanFactoryPostProcessor(ppc);
    context.refresh();
Run Code Online (Sandbox Code Playgroud)

QueueConnectionFactory初始化会引发异常

例外在线程"主" org.springframework.beans.factory.BeanCreationException:错误创建具有名称豆"jmsQueueConnectio nFactory"类路径资源定义[弹簧-config.xml中]:init方法的调用失败; 嵌套异常是javax.naming.Com municationException [根异常是java.net.ConnectException:http://maven.apache.org/ingestionservices-core:没有已知的有效值:'Default [http]:http(http):null:-1:192.168.50.160:-1'; 无可用路由到目的]在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFa ctory.java:1412)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFact ory.java:519) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactor y.java:456)atg.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:291)org.springframework.beans组织中的.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222).

似乎没有正确配置provider.url属性.如果我对提供者URL进行硬编码,它就有效.有人可以指出发生了什么吗?谢谢

Aar*_*ers 5

修改现有的config.xml文件

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:project.properties</value>
    </property>
</bean>


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

<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true" depends-on="jndiTemplate">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>${queue.connection.factory}</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

创建属性文件"project.properties",放在类路径中

# jndiTemplate Info
queue.connection.factory="value..."
provider.url="value..."
initial.context.factory="value..."
Run Code Online (Sandbox Code Playgroud)