BAEL-4068 Inject Spring beans into unmanaged objects

This commit is contained in:
Nguyen Nam Thai 2020-06-09 22:38:43 +07:00
parent 5e67d361cf
commit 8ce5703a5c
6 changed files with 142 additions and 1 deletions

View File

@ -68,6 +68,11 @@
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
@ -90,7 +95,11 @@
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring -->
<!-- <dependency>-->
<!-- <groupId>org.springframework</groupId>-->
@ -129,6 +138,27 @@
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-plugin.version}</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
@ -164,6 +194,7 @@
<mockito.version>1.10.19</mockito.version>
<assertj.version>3.12.2</assertj.version>
<aspectjweaver.version>1.9.5</aspectjweaver.version>
<aspectj-plugin.version>1.11</aspectj-plugin.version>
</properties>
</project>

View File

@ -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 {
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}