BAEL-4068 Add AspectJ DI to module spring-di-2

This commit is contained in:
Nguyen Nam Thai 2020-06-23 17:58:44 +07:00
parent 2c007f42bd
commit 580a3bb920
6 changed files with 175 additions and 0 deletions

65
spring-di-2/pom.xml Normal file
View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-di-2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-di-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-5</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.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</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<properties>
<spring-boot.version>2.3.1.RELEASE</spring-boot.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());
}
}