Results 1 to 5 of 5

Thread: static methods in spring

  1. #1

    Default static methods in spring

    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

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    (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

  3. #3

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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

  5. #5
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Replies: 5
    Last Post: Jan 23rd, 2008, 04:30 PM
  3. Accessing beans from static methods
    By Agardner in forum Container
    Replies: 5
    Last Post: Jun 23rd, 2005, 06:10 PM
  4. Replies: 3
    Last Post: May 11th, 2005, 01:38 PM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •