Spring Boot services · Chapter 34 of 41
Aspect-oriented programming
By the end of this chapter you can write code that runs around, after or on the failure of many methods at once, choose those methods with pointcuts, and explain why a call inside a class is never advised. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 5 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
Meera wants two things before the auditors visit: every refund in an audit trail, and a timing for every service call, so Ravi can see what slows the 6 am rush. The first attempt pasted System.nanoTime() and an audit line into fourteen methods. Two were missed, and each new method must remember them too.
Timing and auditing are not what these methods do: they cut across all of them. With what you know so far, the only place for that code is inside each method, copied.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
TapriAspects.java: time every OrderService call with an around advice
Make TapriAspects an aspect, and make time(call) an around advice for every method of OrderService. It runs the real method, returns exactly what that method returned, and records the call with trail.timed(name(call), nanos), the nanoseconds it took, also when the method throws.
place("SBC", [40, 25]) still returns 65 and leaves the trail's timings at ["OrderService.place"]; a second place adds a second "OrderService.place".
- Task 2
TapriAspects.java: record every @Audited method in the audit trail
Make audit(call, audited) run after every method annotated with @Audited returns, and record trail.audit(entry), where entry is the annotation's value followed by the call's arguments, separated by ", ", in brackets: refunds.refund("A1", 40) still answers "Refunded 40 for A1" and adds "refund(A1, 40)". place() is not @Audited, so it adds no entry.
@Audited must still be there at run time: give it runtime retention (the check reads it).
- Task 3
TapriAspects.java: record failures and let the exception through
Make failed(call, error) run whenever an OrderService method throws (task 5 widens this to every service), and record trail.failed(name(call) + " failed: " + the exception's message).
place("SBC", []) still throws IllegalArgumentException "An order needs at least one item" to the caller, and the trail's failures are then exactly ["OrderService.place failed: An order needs at least one item"]. A call that succeeds records no failure.
- Task 4 · interview question
Interview: cancel an order so that its refund is audited
Write OrderService.cancel(orderId, amount) so it refunds the order through the RefundService it was given: cancel("A7", 65) answers "Refunded 65 for A7", and the audit trail then holds exactly "refund(A7, 65)". Needs task 2.
Be ready to explain why the same refund code written inside OrderService, or a refund method called on this, would never be audited.
- Task 5
TapriAspects.java: time every @Service through a named pointcut
Declare a named pointcut in TapriAspects (an empty method with the pointcut annotation) that matches every method of every class annotated @Service, and use it for the timing advice and the failure advice. OrderService and RefundService are then proxies and timed: refunds.refund("B2", 25) then orders.place("CSMT", [25]) leave the timings at ["RefundService.refund", "OrderService.place"].
AuditTrail is a plain @Component and must stay a plain object, not a proxy.
What you will learn
- Aspects and advice: code that runs before, after or around many methods, written once in one class.
- Pointcuts: expressions that choose those methods by name, type or annotation.
- Your own annotation as a marker, and the retention it needs.
- The proxy: why a call from inside the class is not advised (the interview question).
More in Production
Build "Aspect-oriented programming" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.