How to Inject Dependencies Into Domain Objects When Using the Spring Framework 1.x and Hibernate

Yesterday I read an interesting post on Chris Richardson’s blog about how to inject dependencies into domain objects when using the Spring Framework and Hibernate. This is a great feature because it allows you the option to create richer domain models and avoid the anemic domain model anti-pattern, which was coined by Martin Fowler and discussed at great lengths in Eric Evans’ book Domain Driven Design.

Unfortunately, this AspectJ-based solution discussed by Chris Richardson will not be officially supported until Spring Framework 2 is released (which should be soon). There are some other solutions (e.g. DependencyInjectionInterceptorFactoryBean, ServiceLocator lookups, passing dependencies as as parameters to service methods, etc) but, as Chris points out, these all have some undesirable drawbacks.

Today I thought of a another solution, which might be sufficient (if a bit kludgy) until Spring Framework 2 is released. The genesis for this solution was my realization that the service dependencies for a given domain object class could probably use the same reference for all instances of that class. In other words, it seemed to me that it might make sense to store the dependency in a static variable of the class instead of an instance variable. If I use a static variable, I can set the dependency before Hibernate starts instantiating any instances of the domain object class.

Fortunately, Spring provides the MethodInvokingFactoryBean, which can be used to call any arbitrary static method upon starting up the application context. Using this mechanism, I can configure the service in the application context and transparently set the dependency for this in the domain object class. Furthermore, if I type the static variable with the service’s interface instead of the implementation, I can easily replace the implementation with a stub or mock during unit testing.

Using Chris Richardson’s “PendingOrder” example, here is how my cheesy solution might work. First the code:

public interface RestaurantRepository
{
  //...
}

public class RestaurantRepositoryImpl implements RestaurantRepository
{
  //...
}

public class PendingOrder
{
  private static RestaurantRepository restaurantRepository;

  public static void setRestaurantRepository(RestaurantRepository rr)
  {
    restaurantRepository = rr;
  }

  public int updateDeliveryInfo (
    Address deliveryAddress,
    Date deliveryTime,
    boolean force)
  {
    // the RestaurantRepository dependency is used here
  }
...
}

Now, the configuration:

<bean id="restaurantRepository" class="somepackage.RestaurantRepositoryImpl">
...
</bean>

<bean id="pendingOrderRestaurantRepositoryInjector"
  class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="staticMethod">
    <value>somepackage.PendingOrder.setRestaurantRepository</value>
  </property>
  <property name="arguments">
    <list>
      <ref bean="restaurantRepository"/>
    </list>
  </property>
</bean>

Yes, it’s cheesy but I think it will work fine until I switch to using the AspectJ-based solution when Spring Framework 2 is released.

Be Sociable, Share!

    4 comments to How to Inject Dependencies Into Domain Objects When Using the Spring Framework 1.x and Hibernate

    • schmoely

      We started doing things exactly like this last year.

      The idea has been really well adopted inside our organisation and we are yet to work out why this approach has not been pimped elsewhere.

      AOP is nasty.

    • James

      This is a great idea… especially since the code required to integrate Spring Framework 2.0/Hibernate/AspectJ for domain object injection is still in the sandbox apparently.

    • memeplex

      Well, at least there are other people that have come to the same (imo obvious) solution. I was scared about adopting it, I thought that there must be something terribly wrong with the approach because nobody was even mentioning it, despite its simplicity. Glad to hear you have (successfully?) done this. I’ve recently posted about the idea at the spring forum http://forum.springframework.org/showthread.php?t=48311,
      before knowing about this blog. Maybe a custom tag like /staticdependency class=”X” name=”X” ref=”X”/ will be valuable in order to reduce the xml noise.

    • Joe

      Thanks Memeplex!

    Leave a Reply

     

     

     

    You can use these HTML tags

    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>