Hi,
Why is Spring code using static methods instead of injected dependencies :
Examples
* DatabaseUtils.getConnection
* TransactionSynchronizationManager.isSynchronizatio nAlive
* and so on...
Thanks for your answers,
Christophe
Hi,
Why is Spring code using static methods instead of injected dependencies :
Examples
* DatabaseUtils.getConnection
* TransactionSynchronizationManager.isSynchronizatio nAlive
* and so on...
Thanks for your answers,
Christophe
(you mistyped DataSourceUtils as DataBaseUtils).
Spring actually drinks it's own coolaid. Almost all code is just JavaBeans based, pluggable, and meant to be wired together.
In the few instances like these where there are static methods, it's because they are accessing contextual (thread-based in this case) information...
Regards,
Colin Sampaleanu
SpringSource - http://www.springsource.com
Thank you for your quick answer.
If I look at TransactionSynchronization, I understand what you mean. This is just a thread-base information store. Ok.
But in DataSourceUtils, this is not the case. It holds no state and has just "doing-stuff" methods.
Christophe
DataSourceUtils.getConnection will end up getting a preexisting connection if there is already one associated with any current thread-bound transaction, forcing one to be created otherwise (and bound to any encompassing transaction).
It absolutely does belong as a static method...
Colin Sampaleanu
SpringSource - http://www.springsource.com
To add to Colin's point, some of the code in Spring accesses thread-bound state meaning that it really belongs in a static. In addition, some code is simple 'utils' style code for which no other implementation exists and which does not need to be switched out at runtime. An example of this usage style can be found in the org.springframework.util package.
Rob