diff --git a/spring-di/pom.xml b/spring-di/pom.xml
index 48cdf60673..d556bcd9a4 100644
--- a/spring-di/pom.xml
+++ b/spring-di/pom.xml
@@ -68,6 +68,11 @@
spring-boot-starter
${spring-boot.version}
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+ ${spring-boot.version}
+
org.springframework.boot
spring-boot-test
@@ -90,7 +95,11 @@
aspectjweaver
${aspectjweaver.version}
-
+
+ org.springframework
+ spring-aspects
+ ${spring.version}
+
@@ -129,6 +138,27 @@
false
+
+ org.codehaus.mojo
+ aspectj-maven-plugin
+ ${aspectj-plugin.version}
+
+ ${java.version}
+
+
+ org.springframework
+ spring-aspects
+
+
+
+
+
+
+ compile
+
+
+
+
@@ -164,6 +194,7 @@
1.10.19
3.12.2
1.9.5
+ 1.11
\ No newline at end of file
diff --git a/spring-di/src/main/java/com/baeldung/di/aspectj/AspectJConfig.java b/spring-di/src/main/java/com/baeldung/di/aspectj/AspectJConfig.java
new file mode 100644
index 0000000000..41d43483ac
--- /dev/null
+++ b/spring-di/src/main/java/com/baeldung/di/aspectj/AspectJConfig.java
@@ -0,0 +1,9 @@
+package com.baeldung.di.aspectj;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
+
+@ComponentScan
+@EnableSpringConfigured
+public class AspectJConfig {
+}
diff --git a/spring-di/src/main/java/com/baeldung/di/aspectj/IdService.java b/spring-di/src/main/java/com/baeldung/di/aspectj/IdService.java
new file mode 100644
index 0000000000..4589f28309
--- /dev/null
+++ b/spring-di/src/main/java/com/baeldung/di/aspectj/IdService.java
@@ -0,0 +1,12 @@
+package com.baeldung.di.aspectj;
+
+import org.springframework.stereotype.Service;
+
+@Service
+public class IdService {
+ private static int count;
+
+ int generateId() {
+ return ++count;
+ }
+}
diff --git a/spring-di/src/main/java/com/baeldung/di/aspectj/PersonEntity.java b/spring-di/src/main/java/com/baeldung/di/aspectj/PersonEntity.java
new file mode 100644
index 0000000000..f087a97c7e
--- /dev/null
+++ b/spring-di/src/main/java/com/baeldung/di/aspectj/PersonEntity.java
@@ -0,0 +1,36 @@
+package com.baeldung.di.aspectj;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Configurable;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Transient;
+
+@Entity
+@Configurable(preConstruction = true)
+public class PersonEntity {
+ @Autowired
+ @Transient
+ private IdService idService;
+
+ @Id
+ private int id;
+ private String name;
+
+ public PersonEntity() {
+ }
+
+ public PersonEntity(String name) {
+ id = idService.generateId();
+ this.name = name;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
diff --git a/spring-di/src/main/java/com/baeldung/di/aspectj/PersonObject.java b/spring-di/src/main/java/com/baeldung/di/aspectj/PersonObject.java
new file mode 100644
index 0000000000..e63e43ce3d
--- /dev/null
+++ b/spring-di/src/main/java/com/baeldung/di/aspectj/PersonObject.java
@@ -0,0 +1,29 @@
+package com.baeldung.di.aspectj;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Configurable;
+
+@Configurable
+public class PersonObject {
+ @Autowired
+ private IdService idService;
+
+ private int id;
+ private String name;
+
+ public PersonObject(String name) {
+ this.name = name;
+ }
+
+ void generateId() {
+ this.id = idService.generateId();
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+}
\ No newline at end of file
diff --git a/spring-di/src/test/java/com/baeldung/di/aspectj/PersonUnitTest.java b/spring-di/src/test/java/com/baeldung/di/aspectj/PersonUnitTest.java
new file mode 100644
index 0000000000..72ccfbadf3
--- /dev/null
+++ b/spring-di/src/test/java/com/baeldung/di/aspectj/PersonUnitTest.java
@@ -0,0 +1,24 @@
+package com.baeldung.di.aspectj;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(SpringRunner.class)
+@ContextConfiguration(classes = AspectJConfig.class)
+public class PersonUnitTest {
+ @Test
+ public void givenUnmanagedObjects_whenInjectingIdService_thenIdValueIsCorrectlySet() {
+ PersonObject personObject = new PersonObject("Baeldung");
+ personObject.generateId();
+ assertEquals(1, personObject.getId());
+ assertEquals("Baeldung", personObject.getName());
+
+ PersonEntity personEntity = new PersonEntity("Baeldung");
+ assertEquals(2, personEntity.getId());
+ assertEquals("Baeldung", personEntity.getName());
+ }
+}
\ No newline at end of file