Hi,
I just started a new project and gave JavaConfig a try. Maybe that's just my impression, but it seems that I found helpful information on how to do the setup mostly in blogs and not in the spring documentation.
One thing I found to be particularly annoying though was setting up JNDI. In xml, the configuration looked like this:
Thus I assumed, that the corresponding JavaConfig would be this:Code:<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/appDataSource</value> </property> </bean>
Unfortunately, this did not work - I only got NPEs when the datasource was injected. After googling some more, I found the advice to call afterPropertiesSet(), so I ended up with:Code:@Bean public DataSource dataSource() { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/appDataSource"); }
So it appears to me, that even though JndiObjectFactoryBean implements InitializingBean, afterPropertiesSet() is not called automatically, but that I have to do it manually. Is this true? If so, what is the recommended approach for figuring out, when one needs to call it and when it can be omitted?Code:@Bean public DataSource dataSource() { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/appDataSource"); try { jndiObjectFactoryBean.afterPropertiesSet(); } catch (Exception ex) { throw new RuntimeException("JNDI DS lookup failed!"); } return (DataSource) jndiObjectFactoryBean.getObject(); }
One last questions about exception handling: Is there a best practice on how to handle them? Local try/catch or "throws Exception" on every @Bean method?
Regards,
Tom


Reply With Quote
).

