Announcement Announcement Module
Collapse
No announcement yet.
dependency inject static fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • dependency inject static fields

    Hello there,

    I would like to achieve this:
    Code:
    public class SomeClass{
      @Autowired
      private static OtherClass otherClass;
    
      public static void doDo{
         otherClass.anything();
      }
    }
    I know, this doesn't work, as Spring doesn't autowire static fields.
    Yet I would like to do it this way, what would be the cleanest way of injecting that static field?
    • Code:
      public class SomeClass{
        @Autowired
        private OtherClass otherClass;
        
        public static void doDo{
           new SomeClass().otherClass.anything();
        }
      }
      Doesn't look good to me, as I have to create a new object every time.

    • Code:
      public class SomeClass{
        @Autowired
        private OtherClass otherClass;
        private static OtherClass otherClassStatic;
      
        public static void doDo{
           if(otherClassStatic == null){
              otherClassStatic = new SomeClass().otherClass;
           }
           otherClass.anything();
        }
      }
      Okay, only 1 time I create a new object, but this just looks really weird.
      + If I have multiple static methods, I'm gonna have that lazy initlialization code everywhere.
    Does anyone know better workarounds to dependency inject a static field?

  • #2
    Maybe...

    I am a newbie, but I think that dependency injection only works for Spring managed beans.
    So your 'new SomeClass()' would never have the injection.
    In general, what you are trying to do is trying to force the lifecycle of your bean (just imagine if your bean has a request scope).
    I don't know what you are trying to do, but alternatively you might refer statically to your context and use it to manually get the bean you wanted to inject in a static way.
    It is a poor design, though...
    Maybe, if you tell us which problem you are trying to solve, we might find another solution.

    Comment


    • #3
      Oh ye, I'm using the
      @Configurable annotation on top of the class. So stuff actually does get injected when a new instace is created.

      What I'm trying to achieve is a rich domain model approach, where the model accesses its DAO directly, using no service- as middle layer.

      The model should have static methods to do the basic CRUD stuff, and possibly other more specific things, dependant on the needs.

      Person example
      Code:
      @Configurable
      public class Person{
        @Autowired
        private static personDao;
      
        public static void delete(Person person){
          personDao.delete(person);
        }
      }
      Like this, it would be ideal.

      Comment


      • #4
        What about Spring roo? It seems to be able to create your a rich domain model even without daos.

        Comment


        • #5
          You can achieve this in a couple of ways:

          1. Inject the static dependency via constructor:
          Code:
          private static YourDao yourStaticDao;
          
          @Autowired
          public YourClass( YourDao yourStaticDao ) {
              YourClass.yourStaticDao = yourStaticDao;
          }
          2. Inject the static dependency via setter:
          Code:
          private static YourDao yourStaticDao;
          .....
          @Autowired
          public void setYourStaticDao( YourDao yourStaticDao ) {
              YourClass.yourStaticDao = yourStaticDao;
          }
          NOTE: You can achieve the same via XML configuration instead of annotations.

          I personally favor option 1 because:
          a. If your class which has the static dependency needs to ever be created outside of Spring you can't ensure the static dependency will be set before trying to use it.
          b. By setting the dependency via the constructor you ensure whoever creates the class will need to provide the dependency before trying to use it, even when acting outside of Spring. I think it is more robust.

          I hope it helps!


          nicolas.loriente
          Last edited by nicolas.loriente; May 18th, 2011, 03:15 PM.

          Comment

          Working...
          X