Spring annotations static injection tutorial

Spring’s architecture isn’t very friendly to static classes and methods.  It doesn’t have any way of injecting static properties of classes because it doesn’t have any way to discover them.  Spring’s designers have acknowledged that it’s a shortcoming of the framework and suggest the use of this solution.

  1. Create the static property of the class without any annotations
  2. Mark the class to have static properties injected with @Component so that the properties will be injected on Spring startup
  3. Create a non-static setter method that sets the static property
  4. Mark the setter method with @Autowired(required = true)
@Component
public class UserUtils
{
  private static UserAccessor userAccessor;
 
  /**
   * Sets the user DAO. This method should never be called except by Spring
   * @param userAccessor The user accessor to set
   */
  @Autowired(required = true)
  public void setUserAccessor(userAccessor UserAccessor) {
    UserUtils.userAccessor = userAccessor;
  }
}

Using this technique, you can have all of the advantages of Spring injection without the headaches of Spring injection!  Avoid using this technique whenever possible.  It should only be used to support legacy applications.  With a lot of statically stored values, your application will not scale well.

Posted in Spring by MrSqueezles at February 11th, 2009.
Tags: , , ,

4 Responses to “Spring annotations static injection tutorial”

  1. MattL says:

    I always considered Spring’s lack of static injection a feature.
    The semantics of static properties and especially singletons are often not fully understood and can result in very obscure problems and assumptions. This is especially true when serialization, persistence and remoting are involved.

    My rule is: never use static properties unless their value is guaranteed never to change ever for the life of the universe.

    Spring has a better option – define an object to be a singleton within some context e.g. the application context. Then we can tidy up the semantics for persistence and clustering and not use statics.

    Just my 2c.

  2. Andras Hatvani says:

    Nice hint, added to bookmarks ;) However, it doesn’t work with JUnit 4 test cases, where I wanted to inject a resource used for initializing another field in @BeforeClass which has to be static.

    @MattL: static definitely isn’t about changes, so I’m not sure, whether your rule makes any sense…

  3. Joksy says:

    @MattL, hi man, personally i use static constants as a key for both sessions and Context in generals, the point is accesibility, a public static variable it is accessible everywhere, whereas singleton object handle by spring appcontext means that you have a reference of that context.

    for sure there are some reason why spring arch. did not develop the support for this point anyway a workaround always exist.

  4. Tyrone Hed says:

    The problem with this approach is that the setter injection of a non-static variable can happen AFTER the static variable has already been used. So, beans that use the static variable before it has been initialized by the non-static setter will get the default value of the property while beans that use the property later will get the initialized value. So, this is not a perfect solution.

Leave a Reply