BAEL-4844: Code for the AspectJ class annotation article.

This commit is contained in:
bhandy 2021-03-23 21:52:58 -04:00
parent e0b4e6bcd6
commit cfee931194
8 changed files with 177 additions and 0 deletions

View File

@ -14,6 +14,15 @@
</parent>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
@ -23,7 +32,41 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</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>
<source>${java.version}</source>
<target>${java.version}</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<aspectj-plugin.version>1.11</aspectj-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.aspectj.classmethodadvice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
@Trace
@Component
public class MyTracedService {
private static final Log LOG = LogFactory.getLog(MyTracedService.class);
public void performSomeLogic() {
LOG.info("Inside performSomeLogic...");
}
public void performSomeAdditionalLogic() {
LOG.info("Inside performSomeAdditionalLogic...");
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.aspectj.classmethodadvice;
import org.springframework.stereotype.Component;
@Component
public class MyTracedServiceConsumer {
public MyTracedServiceConsumer(MyTracedService myTracedService) {
myTracedService.performSomeLogic();
myTracedService.performSomeAdditionalLogic();
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.aspectj.classmethodadvice;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Trace {
}

View File

@ -0,0 +1,19 @@
package com.baeldung.aspectj.classmethodadvice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public aspect TracingAspect {
private static final Log LOG = LogFactory.getLog(TracingAspect.class);
pointcut traceAnnotatedClasses(): within(@Trace *) && execution(* *(..));
Object around() : traceAnnotatedClasses() {
String signature = thisJoinPoint.getSignature().toShortString();
LOG.trace("Entering " + signature);
Object returnValue = proceed();
LOG.trace("Exiting " + signature);
return returnValue;
}
}

View File

@ -17,6 +17,8 @@
<logger name="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" level="TRACE" />
<logger name="com.baeldung.aspectj.classmethodadvice" level="TRACE" />
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>

View File

@ -0,0 +1,30 @@
package com.baeldung.aspectj.classmethodadvice;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
public class MyTracedServiceConsumerUnitTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Spy
private MyTracedService myTracedService;
@Test
public void whenCallingConsumer_thenServiceIsCalled() {
doNothing().when(myTracedService).performSomeLogic();
doNothing().when(myTracedService).performSomeAdditionalLogic();
new MyTracedServiceConsumer(myTracedService);
verify(myTracedService).performSomeLogic();
verify(myTracedService).performSomeAdditionalLogic();
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.aspectj.classmethodadvice;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.system.OutputCaptureRule;
import static org.junit.Assert.assertTrue;
/*
* When running this test class, the tests may fail unless you build the code with Maven first. You
* must ensure the AspectJ compiler executes to weave in the Aspect's logic. Without the Aspect
* weaved into the class under test, the trace logging will not be written to stdout.
*/
public class MyTracedServiceUnitTest {
@Rule
public OutputCaptureRule outputCaptureRule = new OutputCaptureRule();
@Test
public void whenPerformingSomeLogic_thenTraceAndInfoOutputIsWritten() {
MyTracedService myTracedService = new MyTracedService();
myTracedService.performSomeLogic();
String output = outputCaptureRule.getOut();
assertTrue(output.contains("TracingAspect - Entering MyTracedService.performSomeLogic"));
assertTrue(output.contains("MyTracedService - Inside performSomeLogic"));
assertTrue(output.contains("TracingAspect - Exiting MyTracedService.performSomeLogic"));
}
@Test
public void whenPerformingSomeAdditionalLogic_thenTraceAndInfoOutputIsWritten() {
MyTracedService myTracedService = new MyTracedService();
myTracedService.performSomeAdditionalLogic();
String output = outputCaptureRule.getOut();
assertTrue(output.contains("TracingAspect - Entering MyTracedService.performSomeAdditionalLogic"));
assertTrue(output.contains("MyTracedService - Inside performSomeAdditionalLogic"));
assertTrue(output.contains("TracingAspect - Exiting MyTracedService.performSomeAdditionalLogic"));
}
}