BAEL-4844: Code for the AspectJ class annotation article.
This commit is contained in:
parent
e0b4e6bcd6
commit
cfee931194
|
@ -14,6 +14,15 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjrt</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
@ -23,7 +32,41 @@
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</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>
|
</project>
|
||||||
|
|
||||||
|
|
|
@ -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...");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
<logger name="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" level="TRACE" />
|
<logger name="org.springframework.aop.interceptor.PerformanceMonitorInterceptor" level="TRACE" />
|
||||||
|
|
||||||
|
<logger name="com.baeldung.aspectj.classmethodadvice" level="TRACE" />
|
||||||
|
|
||||||
<root level="TRACE">
|
<root level="TRACE">
|
||||||
<appender-ref ref="STDOUT" />
|
<appender-ref ref="STDOUT" />
|
||||||
</root>
|
</root>
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue