如何将 jndi 查找从 xml 转换为 java 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34757609/
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
how to convert jndi lookup from xml to java config
提问by FreezY
Currently I'm converting the xml to java config. But I stuck at some part that I have been research for several days. Here the problem:
目前我正在将 xml 转换为 java 配置。但是我坚持了几天我一直在研究的部分。这里的问题:
Xml config:
xml配置:
<jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}" resource-ref="true" />
<beans:bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate" >
<beans:property name="dataSource" ref="dbDataSource"></beans:property>
</beans:bean>
So far I managed to convert this code:
到目前为止,我设法转换了这段代码:
<jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}" resource-ref="true" />
<jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}" resource-ref="true" />
to this :
对此:
@Bean(name = "dbDataSource")
public JndiObjectFactoryBean dataSource() {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("${db.jndi}");
bean.setResourceRef(true);
return bean;
}
And this :
还有这个 :
<beans:bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate" >
<beans:property name="dataSource" ref="dbDataSource"></beans:property>
</beans:bean>
to this:
对此:
@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate() {
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
return jt;
}
The problem is the method setDataSource()need DataSource object but I'm not sure how to relate both bean.How to pass the JndiObjectFactoryBean to DataSource?
问题是setDataSource()方法需要 DataSource 对象,但我不确定如何关联这两个 bean。如何将 JndiObjectFactoryBean 传递给 DataSource?
Or do I need to use another method?
还是我需要使用其他方法?
Extra Question:
额外问题:
The bean.setJndiName("${db.jndi}")
, ${db.jndi}is refer to properties file but I always got NameNotFoundException, How to make it work?
的bean.setJndiName("${db.jndi}")
,$ {} db.jndi是指性文件,但我总是得到的NameNotFoundException,如何使它工作?
Thanks!!
谢谢!!
回答by M. Deinum
Instead of JndiObjectFactoryBean
use a JndiDataSourceLookup
instead. To use the ${db.jndi}
in the method declare a method argument and annotate it with @Value
.
而不是JndiObjectFactoryBean
使用 aJndiDataSourceLookup
代替。要${db.jndi}
在方法中使用 ,声明一个方法参数并用@Value
.
@Bean(name = "dbDataSource")
public DataSource dataSource(@Value("${db.jndi}" String jndiName) {
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource(jndiName);
}
Autowired methods and constructors can also use the
@Value
annotation. -- Spring Reference Guide.
自动装配的方法和构造函数也可以使用
@Value
注解。-- Spring 参考指南。
@Bean
methods are basically factory methods which are also auto wired methods and as such fall into this category.
@Bean
方法基本上是工厂方法,它们也是自动连接的方法,因此属于这一类。
In your factory method for the JdbcTemplate
you can simply use a DataSource
method argument to get a reference to the datasource (If you have multiple you can use the @Qualifier
on the method argument to specify which one you want to use).
在你的工厂方法中,JdbcTemplate
你可以简单地使用一个DataSource
方法参数来获取对数据源的引用(如果你有多个,你可以使用@Qualifier
方法参数来指定你想使用哪个)。
@Bean
public JdbcTemplate jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}