So today I was happy coding a Spring boot project of mine and wanted to use a file based session using H2 for a development environment. Since my application used another type of primary datasource (postgresql) I went on to figure out how to do it.
After setting my own beans, the application failed because the initializer was not run on the proper DataSource, it used the primary one. After some code digging I found out that the bean in spring boot is configured like this:
@Bean
@ConditionalOnMissingBean
JdbcSessionDataSourceInitializer jdbcSessionDataSourceInitializer(DataSource dataSource,
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
return new JdbcSessionDataSourceInitializer(dataSource, resourceLoader, properties);
}
After finding this out, I happily defined my own JdbcSessionDataSourceInitializer
bean annotating dataSource
with @SpringSessionDataSource
and went on coding. After some thinking I believe this makes sense to be changed in the library.
The end result would be
@Bean
@ConditionalOnMissingBean
JdbcSessionDataSourceInitializer jdbcSessionDataSourceInitializer(@SpringSessionDataSource DataSource dataSource,
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
return new JdbcSessionDataSourceInitializer(dataSource, resourceLoader, properties);
}
Thoughts?
Comment From: wilkinsona
Thanks for the suggestion. I think that makes sense and would align the DataSource
that's injected into the JdbcSessionDataSourceInitializer
with the DataSource
that's injected into Boot's JdbcHttpSessionConfiguration
subclass.