我配置了以下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}视为值,因此显示了一个未找到类的异常。 如果我硬编码工厂类名称和网址,它工作正常。 我不知道这里遗漏了什么,因为我无法弄清楚这是什么问题。
感谢任何帮助或指示。
答案 0 :(得分:3)
我发现了另一种更简单的方法,它不需要额外的课程。我使用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>
答案 1 :(得分:1)
您也可以像这样使用弹簧PropertiesFactoryBean
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:///opt/myproject/jndi.properties" />
</bean>
答案 2 :(得分:0)
我设法找到一个适合我的解决方案。创建了我自己的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">
希望这可以帮助任何面临同样问题的人。