Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert jndi lookup from xml to java config

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 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 managed to convert this code:

<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?

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?

Thanks!!

like image 948
FreezY Avatar asked Jan 13 '16 02:01

FreezY


People also ask

Does tomcat support JNDI?

Tomcat provides a JNDI InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the /WEB-INF/web.

How JNDI works in tomcat?

Actual benefit of DataSource comes when we use it with a JNDI Context. For example, connection pool in a web application deployed in a servlet container. Most of the popular servlet containers provide built-in support for DataSource through Resource configuration and JNDI context.

What is JNDI in Java with examples?

2. JNDI Description. Any work with JNDI requires an understanding of the underlying service as well as an accessible implementation. For example, a database connection service calls for specific properties and exception handling. However, JNDI's abstraction decouples the connection configuration from the application.


1 Answers

Instead of JndiObjectFactoryBean use a JndiDataSourceLookup instead. To use the ${db.jndi} in the method declare a method argument and annotate it with @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.

@Bean methods are basically factory methods which are also auto wired methods and as such fall into this category.

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).

@Bean
public JdbcTemplate jdbcTemplate(DataSource ds) { 
    return new JdbcTemplate(ds);
}
like image 67
M. Deinum Avatar answered Oct 11 '22 15:10

M. Deinum