我的项目需要从属性文件加载初始上下文工厂和提供程序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>
这是我的Spring容器初始化的方法
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setProperties(ConfigManager.getProperties());
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
context.addBeanFactoryPostProcessor(ppc);
context.refresh();
QueueConnectionFactory初始化会抛出异常
线程“main”中的异常org.springframework.beans.factory.BeanCreationException:创建名为'jmsQueueConnectio的bean时出错 nFactory'在类路径资源中定义[spring-config.xml]:init方法的调用失败;嵌套异常是javax.naming.Com municationException [根异常是java.net.ConnectException:http://maven.apache.org/ingestionservices-core:没有已知的有效por t for:'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) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:291) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075) 在com.quickplay.ingestionservices.poolmgr.PoolManager.initialize(PoolManager.java:143) 在com.quickplay.ingestionservices.poolmgr.PoolManager。(PoolManager.java:56) 在com.quickplay.ingestionservices.poolmgr.PoolManager.main(PoolManager.java:47)
似乎未正确配置provider.url属性。如果我对提供者URL进行硬编码,它就有效。有人可以指出发生了什么吗?感谢
答案 0 :(得分: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>
创建属性文件“project.properties”,放在类路径
中# jndiTemplate Info
queue.connection.factory="value..."
provider.url="value..."
initial.context.factory="value..."