Over a million developers have joined DZone.

Domain Object Dependency Injection with Spring

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

I have always considered domain objects as dumb because they normally don't do anything i.e. they don't have any behavior. But with the advent of Domain Driven Design, the same domain objects have got the power to do useful things. In order to do something useful, they have to collaborate with other classes i.e. they will have dependencies on other classes . The problem is that Domain objects are either created using the new operator or by an ORM framework so as to inject beans into it. For example, we have a User entity and we want to add a behavior like findAllUsersWithSameLastName into the User entity as shown below

public class User implements Serializable {

private String firstname;
private String lastname;

private MyDao myDao;

public List<User> findAllUsersWithSameLastName(){
return myDao.findAllUsersWithLastName(this.lastname);
}

// setters and getters

}

Now in order to inject myDao bean inside User entity, we have to mark the User class with @org.springframework.beans.factory.annotation.Configurable annotation which makes the User class eligible for Spring driven configuration. The core idea behind domain object DI is : An AspectJ-woven aspect selects join points corresponding to creation or deserialization of any object matching certain specification. Advice to those join points inject dependencies into the object being created or deserialized.

@Configurable
public class User implements Serializable {

private String firstname;
private String lastname;

@Autowired
private MyDao myDao;

public List<User> findAllUsersWithSameLastName(){
return myDao.findAllUsersWithLastName(this.lastname);
}

// setters and getters

}

Just mentioning @Configurable on the User entity does not make this class Spring configured. You need to have org.springframework.aspects-3.0.4.RELEASE.jar in your classpath. This jar contains a class called AnnotationBeanConfigurerAspect which acts on this annotation . AnnotationBeanConfigurerAspect needs to be configured in spring application context xml to obtain a reference to the bean factory that is to be used to configure new objects.  Also, we need to configure the auto scanning of User entity and dao class so that they can be auto-registered. You also need to activate the Spring LoadTimeWeaver which is also present in context namespace.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:spring-configured />
<context:component-scan base-package="com.shekhar.di"></context:component-scan>
<context:load-time-weaver aspectj-weaving="on" />

</beans>

Last thing that you have to configure is to add the javaagent argument to your JVM or eclipse JUnit run configurations

-javaagent:~\.m2\repository\org\springframework\org.springframework.instrument\3.0.4.RELEASE\org.springframework.instrument-3.0.4.RELEASE.jar

 Lets run the test.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UsersTest {

@Test
public void shouldConfigureDomainObject() {
User user = new User();
user.setFirstname("shekhar");
user.setLastname("gulati");
List<User> users = user.findAllUsersWithSameLastName();
assertNotNull(users);
}
}

 After you run test if you get exception like this

SEVERE: register definition failed
org.aspectj.weaver.BCException: Unable to continue, this version of AspectJ supports classes built with weaver version 3.0 but the class org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
is version 6.0

then make sure that you have correct version of aspectj jars. If not then please add these dependencies to pom.xml

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.9</version>
</dependency>

 This feature is not new in Spring; it existed from 2.0 days but I think it is less known. So next time, you think of adding some behavior to your domain object think about dependency injection in domain objects. Although there is documentation on the web about this, it took some time to make it work. I am also attaching the sample project in case you want to play with it.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}