JNDI 属性(java.naming.factory.initial 和 java.naming.provider.url)未通过 Spring 的 PropertyPlaceholderConfigurer 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22646004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JNDI properties (java.naming.factory.initial & java.naming.provider.url) not getting set through Spring's PropertyPlaceholderConfigurer
提问by rahul pasricha
I have configured the following PropertyPlaceholderConfigurer
我已经配置了以下内容 PropertyPlaceholderConfigurer
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:///opt/myproject/jndi.properties" />
</bean>
And my JNDI template bean looks like
我的 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 file has two keys defined.
jndi.properties 文件定义了两个键。
factory=org.webmethods.jms.naming.WmJmsNamingCtxFactory
url=wmjmsnaming://[email protected]:7001
When i deploy this on weblogic and start the application, i see the following trace
当我在 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.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 isn't replacing ${factory} witht he value from the properties file but considers ${factory} as the value and thus shows a class not found exception. If i hardcode the factory class name and the url, it works fine. I am not sure whats missing here as i am not able to figure out whats really the issue.
Spring 不会用属性文件中的值替换 ${factory},而是将 ${factory} 视为该值,因此会显示未找到类的异常。如果我对工厂类名和 url 进行硬编码,它就可以正常工作。我不确定这里缺少什么,因为我无法弄清楚真正的问题是什么。
Appreciate any help or pointers on this.
感谢您对此的任何帮助或指示。
采纳答案by rahul pasricha
You can also use springs PropertiesFactoryBean like this
你也可以像这样使用 springs PropertiesFactoryBean
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:///opt/myproject/jndi.properties" />
</bean>
回答by rahul pasricha
I managed to find a workaround solution which did work for me. Created my own JndiTemplate and read the jndi.properties file explicitly from the server. The custom class looks something like this
我设法找到了一个对我有用的解决方法。创建了我自己的 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);
}
}
and then use this class in the spring context xml like this
然后像这样在spring上下文xml中使用这个类
<bean id="myJndiTemplate" class="com.MyJndiTemplate">
Hope this helps anyone facing the same issue.
希望这可以帮助任何面临相同问题的人。
回答by rahul pasricha
I found another simpler approach that doesn't need an extra class. i used org.springframework.beans.factory.config.PropertiesFactoryBean
to load the jndi.properties
我发现了另一种更简单的方法,不需要额外的课程。我曾经org.springframework.beans.factory.config.PropertiesFactoryBean
加载 jndi.properties
The jndiTemplate bean looks something like this.
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>