Observe extra behaviour around an existing product service without mixing it into the business operation.
Open this stage in the online learning path
Use JDK 21 or newer and Maven 3.9 or newer. Verify with java -version and mvn -version. Extract this project into its own folder; do not merge its files with another stage.
This lesson reuses the product catalog and Todo responsibilities from Stages 8–14. Only the feature described below is new. Read the ordered explanation before opening the complete files.
ProductService.count returns the product count. The aspect contains the additional logging concern; the service does not call the aspect itself.
@EnableAspectJAutoProxy installs Spring’s annotation-driven AOP support. We request class-based proxies to keep the concrete service lookup simple. AspectJ annotations and pointcut expressions are used with Spring runtime proxies, not AspectJ bytecode weaving.
The service method execution is the join point. The pointcut selects executions. The advice is the extra method that runs. The aspect groups these rules. In Spring AOP, interception applies to method execution through eligible managed proxies, not arbitrary source-code lines.
Our @LogExecutionTime annotation is retained at runtime so the pointcut can inspect it. Only methods carrying that annotation are selected.
Around advice calls proceed exactly once, returns its result and allows exceptions to propagate. finally records elapsed time even if the method fails. Timing output varies; this is an observation example rather than a performance benchmark.
TimingAspect.invocations exists only to verify these sequential console calls. This mutable counter is not a production metric for concurrent requests.
The injected service reference is a proxy. service.count passes through it. countInternally enters the target and this.count calls the target directly, bypassing the proxy. The internal call still returns 2 but adds no timed invocation.
Move the separately advised operation into another injected service rather than relying on this calls, private methods or self-injection tricks. The transaction lessons reuse this boundary. Caching and method security also have proxy-related boundaries; servlet security filters are a different mechanism.
Open a terminal in the extracted folder containing pom.xml. Run one command at a time.
mvn test
mvn spring-boot:runThis is a console lesson. It does not open a web port. Application.main starts a Spring context, calls the demonstration and closes the context. Read the terminal, not a browser. Spring Boot’s Maven plugin is used as a convenient launcher; these examples show Spring Framework directly.
Timed count: <varying number> ns
External result: 2
Internal result: 2
Timed invocations: 1Object identifiers and elapsed times can vary. Compare the relationship between values rather than copying an identifier.
Create the following files at exactly these paths, or use the ZIP. Source files include their imports. The test checks the behaviour described in the lesson.
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>4.1.1</version><relativePath/></parent>
<groupId>com.edupoly</groupId><artifactId>spring-stage-33-proxy-boundary</artifactId><version>1.0.0</version>
<properties><java.version>21</java.version></properties>
<dependencies>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId></dependency>
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency>
</dependencies>
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
package com.edupoly.lesson;
import org.springframework.context.annotation.*;
@Configuration @ComponentScan("com.edupoly.lesson") @EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppConfig { }
package com.edupoly.lesson;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
public @interface LogExecutionTime { }
package com.edupoly.lesson;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect @Component public class TimingAspect {
private int invocations;
public int invocations() { return invocations; }
@Around("@annotation(com.edupoly.lesson.LogExecutionTime)")
public Object time(ProceedingJoinPoint point) throws Throwable {
invocations++;
long start = System.nanoTime();
try { return point.proceed(); }
finally { System.out.println("Timed " + point.getSignature().getName() + ": " + (System.nanoTime() - start) + " ns"); }
}
}
package com.edupoly.lesson;
import org.springframework.stereotype.Service;
@Service public class ProductService {
@LogExecutionTime public int count() { return 2; }
public int countInternally() { return this.count(); }
}
package com.edupoly.lesson;
import org.springframework.stereotype.Component;
@Component public class Lesson {
private final ProductService service; private final TimingAspect aspect;
public Lesson(ProductService service, TimingAspect aspect) { this.service = service; this.aspect = aspect; }
public void run() {
System.out.println("External result: " + service.count());
System.out.println("Internal result: " + service.countInternally());
System.out.println("Timed invocations: " + aspect.invocations());
}
}
package com.edupoly.lesson;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) throws Exception {
try (var context = new AnnotationConfigApplicationContext(AppConfig.class)) {
context.getBean(Lesson.class).run();
}
}
}
package com.edupoly.lesson;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
class LessonTest {
@Test void advicePreservesReturnValueAndUsesProxy() {
try (var context = new AnnotationConfigApplicationContext(AppConfig.class)) {
var service = context.getBean(ProductService.class);
assertTrue(org.springframework.aop.support.AopUtils.isAopProxy(service));
assertEquals(2, service.count());
var aspect = context.getBean(TimingAspect.class);
assertEquals(1, aspect.invocations());
assertEquals(2, service.countInternally());
assertEquals(1, aspect.invocations());
}
}
}
Call countInternally and expect a second timing line: it does not appear. Move the count operation into a separately injected service if that call needs advice.
| Word | Meaning in this example |
|---|---|
| Proxy | The injected intermediary applying advice before reaching the target. |
| Self-invocation | A this.method() call within the target bypasses its proxy. |
Add another service method. Predict whether the current pointcut matches it, then run the example to check. Keep the service return value unchanged.