Spring JndiTemplate并从上下文中参数化JNDI查找

时间:2010-05-27 14:01:43

标签: java spring jndi

如何在Spring应用程序上下文文件中表示new JndiTemplate(properties).lookup(name),其中name是字符串变量?我能否以类似于以下的方式表达它,其中应用程序在检索bean ID时提供name

<util:properties id="applicationProperties"
    location="classpath:application.properties"/>

<jee:jndi-lookup id="connectionFactory"
    jndi-name="${name}"
    environment-ref="applicationProperties"
    resource-ref="false" />

1 个答案:

答案 0 :(得分:2)

据我了解,你需要这样的东西:

<bean id = "jndiTemplate" class = "org.springframework.jndi.JndiTemplate">
    <property name = "environment" ref = "applicationProperties" />
</bean>

<bean id = "objectFromJndi" factory-bean = "jndiTemplate" factory-method = "lookup"
    scope = "prototype" />

-

ApplicationContext ctx = ...;
Object o = ctx.getBean("objectFromJndi", name);

这样可行,因为getBean可以将参数传递给factory-method