DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Dependency Injection in Spring
  • Combining gRPC With Guice
  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Smart Dependency Injection With Spring: Overview (Part 1 of 3)

Trending

  • Monkey-Patching in Java
  • Securing Your Applications With Spring Security
  • How To Optimize Feature Sets With Genetic Algorithms
  • API Design
  1. DZone
  2. Coding
  3. Frameworks
  4. Domain Object Dependency Injection with Spring

Domain Object Dependency Injection with Spring

Shekhar Gulati user avatar by
Shekhar Gulati
·
Nov. 01, 10 · Interview
Like (0)
Save
Tweet
Share
29.68K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Object (computer science) Spring Framework Dependency injection

Opinions expressed by DZone contributors are their own.

Related

  • Dependency Injection in Spring
  • Combining gRPC With Guice
  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Smart Dependency Injection With Spring: Overview (Part 1 of 3)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: