I am currently converting xml to java config. But I was stuck in some part that I spent for several days. Here's the problem:
Xml config:
<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 have managed to convert this code:
<jee:jndi-lookup id="dbDataSource" jndi-name="${db.jndi}" resource-ref="true" />
:
@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>
:
@Bean(name = "jdbcTemplate") public JdbcTemplate jdbcTemplate() { JdbcTemplate jt = new JdbcTemplate(); jt.setDataSource(dataSource); return jt; }
The problem is that the setDataSource () method needs a DataSource object, but I'm not sure how to bind as a bean. How to pass JndiObjectFactoryBean to DataSource?
Or do I need to use a different method?
Additional question:
bean.setJndiName("${db.jndi}")
, $ {db.jndi} refers to the properties file, but I always got a NameNotFoundException, how to make it work?
Thanks!!
source share