Issue
My Spring configuration looks like
...
<bean id="myWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
...
<property name="sql" value="%[insert.sql.command]" />
==> <property name="dataSource" ref="outDataSource1" />
</bean>
<bean id="myWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
...
<property name="sql" value="%[insert.sql.command]" />
==> <property name="dataSource" ref="outDataSource2" />
</bean>
...
<bean id="outDataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="%[outds1.driverClassName]" />
<property name="url" value="%[outds1.url]" />
<property name="username" value="%[outds1.username]" />
<property name="password" value="%[outds1.password]" />
</bean>
<bean id="outDataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="%[outds2.driverClassName]" />
<property name="url" value="%[outds2.url]" />
<property name="username" value="%[outds2.username]" />
<property name="password" value="%[outds2.password]" />
</bean>
...
I would like to display information for the DataSource
referenced in each writer (JdbcBatchItemWriter
class). DataSource
is implemented by a Spring DriverManagerDataSource
.
For example, display infos :
(DriverManagerDataSource).getUrl()
(DriverManagerDataSource).getUsername()
So I wrapped JdbcBatchItemWriter
in a, say JdbcBatchItemWriterWrapper
, based on an ApplicationContextAware
.
Since JdbcBatchItemWriter
DOES NOT HAVE any getDataSource()
getter, I need to resolve/instantiate the ref
of the property dataSource
programatically. How can I do this?
Solution
The only place you could intercept the DataSource
is before the setDataSource
call implied by the
<property name="dataSource" ref="outDataSource2" />
One solution is to extend the JdbcBatchItemWriter
and override its setDataSource
, log the value that is passed to it, then delegate to the super
implementation.
Instead of providing JdbcBatchItemWriter
as the bean class, you provide your subclass.
Answered By - Sotirios Delimanolis
Answer Checked By - Willingham (JavaFixing Volunteer)