Unable to combine AnnotationDrivenTx with ExternalValue. Possible bug?
Hi,
First off, to all you JavaConfig developers; thanks for providing this great alternative to the XML Spring configuration. Keep up the good work :)
I'm out of luck trying to combine AnnotationDrivenTx with ExternalValue annotations. Consider the following JavaConfig applcation config class;
(Oh, I'm unable to create a post with 'at' sign due to restrictions of the forum (this is my first post) so I've swapped them with '#' in the code.)
Code:
#Configuration
#AnnotationDrivenConfig
#AnnotationDrivenTx
#PropertiesValueSource(locations = {
"classpath:db.properties"
})
public class AppConfig extends ConfigurationSupport {
#ExternalValue("datasource.username") String username;
#ExternalValue("datasource.password") String password;
#ExternalValue("datasource.driverClassName") String driverClassName;
#ExternalValue("datasource.url") String url;
#ExternalValue("hibernate.dialect") String dialect;
#ExternalValue("hibernate.hbm2ddl.auto") String hbm2ddlAuto;
#Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
System.out.println("in dataSource(): username = " + username);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
return dataSource;
}
#Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.show_sql", true);
sessionFactoryBean.setHibernateProperties(properties);
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setMappingResources(new String[] {
"test.xml"
});
return getObject(sessionFactoryBean, SessionFactory.class);
}
#Bean
public PlatformTransactionManager transactionManager() {
return new HibernateTransactionManager(sessionFactory());
}
}
Loading this will cause 'in dataSource(): username = null' to be written to stdout. Trying to load the class with '#AnnotationDrivenTx' commented out, will however yield whatever value set in the db.properties file.
Any insights?
Thanks in advance.