Depending on your need rewrite your configuration a little. If you don't really need the datasource injected you can do something like this.
@Bean(name = DEMO_DS)
public JndiObjectFactoryBean demoDataSource() {
JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
factory.setJndiName(JDBC_DEMO_DS);
factory.setProxyInterface(DataSource.class);
return factory;
}
@Bean(name = DEMO_SESSION_FACTORY)
public SqlSessionFactoryBean demoSqlSessionFactory() {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(demoDataSource().getObject());
sessionFactory.setConfigLocation(new ClassPathResource("demo/config.xml"));
return sessionFactory;
}
If you need to have a datasource injected you might want to switch to using a JndiLocatorDelegate
to do the lookup instead of a JndiObjectFactoryBean
.
@Bean(name = DEMO_DS)
public DataSource demoDataSource() throws NamingException {
return JndiLocatorDelegate.createDefaultResourceRefLocator().lookup(JDBC_DEMO_DS, DataSource.class);
}
This gives you a DataSource
directly instead of a FactoryBean<Object>
(which is what the JndiObjctFactoryBean
is) what probably is the source of the problem.
Or (in theory) you should also be able to use a @Value
annotation on a property in your config class. Instead of a @Value
a normal @Resource
should also do the trick (that can also delegate a call to JNDI for a lookup).
public class MyConfig {
@Value("${" + JDBC_DEMO_DS + "}")
private DataSource demoDs;
}
With @Resource
public class MyConfig {
@Resource(mappedName=JDBC_DEMO_DS)
private DataSource demoDs;
}
And you can then simply reference it in your configuration method.
@Bean(name = DEMO_SESSION_FACTORY)
public SqlSessionFactoryBean demoSqlSessionFactory() {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(demoDs);
sessionFactory.setConfigLocation(new ClassPathResource("demo/config.xml"));
return sessionFactory;
}