• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using @Inject on static utility classes

 
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, J and Ashish.

A colleague of mine was trying to use @Inject to inject a runtime String value (the name of the host where the application is running) into a class that had no constructor and only contains static utility methods. The value came up empty, of course. That started a discussion about how one could annotate this class (such as @Component) in order to ensure that Spring instantiated it, or if it should be added to the application context. But that begs the question, why would you want to do that?

So, the question for you guys is: are there any situations you have found where it made sense to have the Spring application context manage a bean that provided only static methods?

Thank you.
 
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont see any reason why not.

You need it configurable. Having it in the Spring context allows you cleaner and clearer maintenance of that variable.

- k


--------------------------------------------------------------------------------------------------------
[SpringSource Certified Spring Professional - Practice Tests]
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally I think it's not a good idea to have utility classes that have dependencies like his, because you are practically introducing a Singleton. Spring has better ways of creating singletons. Change the utility class to one without static methods, then wire the dependent class into it and then wire the utility class into places that need it
 
Rob Ivan
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the responses. I already had the second solution in place (Spring managed Singleton), and it got refactored. I was looking for some "independent validation" that it was not very Spring-like to inject into a class with static utilities.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively leave your static utility class alone if it makes sense and pass in the hostname as a parameter to the static utility methods that need it. In this case the caller (probably a spring bean service) could @Value this value in or get it from the Spring Environment assuming it was correctly set as a @PropertySource.

 
reply
    Bookmark Topic Watch Topic
  • New Topic