Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your company has a shared aspect with the pointcut execution(* com.company..*.*(..)) for logging. Services see high CPU and odd behaviour. How would you redesign it?

The pointcut execution(* com.company..*.*(..)) matches almost every method of almost every bean, so Spring wraps nearly every bean in a proxy and runs the logging advice on every internal method call, which costs CPU and brings proxy side effects such as final-class failures, type changes and surprising self-call behaviour. I would redesign it as opt-in: an explicit annotation such as @Logged matched with @annotation or @within, adopted by teams where they want it, with its overhead measured. For timing and tracing, which is often what such aspects are really for, Micrometer observations and distributed tracing are the better tool.

Why this question is asked

A shared logging aspect with a package-wide pointcut is a common sight in older company frameworks. It was written to save effort: every method in every service logs its entry and exit without anyone writing a line. Years later, services show high CPU and odd failures, and nobody suspects the aspect, because it is invisible in their code. Interviewers ask this design question to see whether we can trace symptoms back to a proxy mechanism and replace it with something cheaper and more honest. In this answer, we will recap how the pointcut is applied, see where the CPU goes and where the odd behaviour comes from, then redesign it step by step.

1A Quick Recap: What the Pointcut Really Selects

In Spring AOP, a pointcut decides which beans get a proxy and which of their method executions run the advice. The proxy is a generated wrapper, a CGLIB subclass in Spring Boot, that runs the advice and then calls the real bean. Spring checks every bean in the application against the pointcut when it is created, and any bean with at least one matching method is proxied.

The expression execution(* com.company..*.*(..)) means any return type, any type in com.company or any sub-package, any method, any parameters. That is every method on every bean the company wrote: controllers, services, repositories, configuration classes, small helper beans, converters, even the logging utilities themselves if they are beans in com.company. Nothing has to opt in; everything is in.

2Where the CPU Goes

Every matched call now goes through the proxy and the interceptor chain before reaching the real method. On its own that dispatch is small, a fraction of a microsecond. The cost comes from what a typical logging advice does on every call: it builds a signature string, formats the arguments into the log message, often by calling toString() on large objects or collections, reads the clock, and writes one or two log lines. Inside a service, one request can make hundreds of bean-to-bean calls, each now doing this work.

Java
@Around("execution(* com.company..*.*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable {
log.debug("enter {} {}", pjp.getSignature(), Arrays.toString(pjp.getArgs())); // runs on every call
try { return pjp.proceed(); }
finally { log.debug("exit {}", pjp.getSignature()); }
}
// One request: 300 advised calls x (signature + args formatting + 2 log events)
// Profiler: about 20 to 30 percent of request CPU in the aspect (numbers from our profile; yours will differ)

Notice that even with the log level set to INFO, Arrays.toString(pjp.getArgs()) is evaluated before the logger decides to discard the message, because it is an argument to the call. That alone can turn a cheap disabled log line into real work on every method. A profile such as a JFR recording or an async-profiler flame graph makes this visible quickly.

3Where the Odd Behaviour Comes From

Proxying every bean also changes how the application behaves. A final class in com.company cannot be subclassed by CGLIB, so its bean fails at startup, or a final method silently runs on the uninitialised proxy instance and throws a NullPointerException. Code that checks bean.getClass() now sees a generated class name. Arguments logged by toString() may include personal data or secrets, and some toString() implementations trigger lazy loading in Hibernate entities, firing extra queries or throwing LazyInitializationException outside a transaction. If the aspect's own helper beans are in com.company, the aspect can end up advising itself.

There is also inconsistency. Self-calls inside a bean are not logged, so the log shows some calls and not others, which confuses anyone using it to debug. These are not bugs in Spring; they are what happens when a proxy is applied to everything without anyone choosing it.

Symptoms traced to the broad pointcut
SymptomCause
High CPU, more garbageadvice formats arguments and logs on every internal call
Startup failure for a final classCGLIB cannot subclass a final class
NullPointerException in a final methodfinal method runs on the proxy, whose fields are null
Extra SQL or LazyInitializationExceptiontoString() of logged entities touches lazy relations
Secrets or personal data in logsarguments logged without filtering
Some calls logged, others notself-calls on this skip the proxy

4The Redesign: Opt In With an Annotation

The first change is to stop matching by package and start matching by an explicit marker. We define an annotation such as @Logged and write the pointcut against it. Teams put it on the few entry points where they want detailed logs, or on a class to cover all of its methods.

Java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Logged {}
@Around("@annotation(com.company.logging.Logged) || @within(com.company.logging.Logged)")
public Object log(ProceedingJoinPoint pjp) throws Throwable {
if (!log.isDebugEnabled()) return pjp.proceed(); // nothing to do: skip all formatting
String name = pjp.getSignature().toShortString();
log.debug("enter {}", name); // no arguments by default
try { return pjp.proceed(); }
finally { log.debug("exit {}", name); }
}

Notice three changes besides the pointcut. The advice checks the log level first, so it costs almost nothing when debugging is off. It no longer logs arguments by default, which removes both the toString() cost and the risk of leaking data. And because only marked beans are proxied, final classes and helper beans elsewhere are left alone.

5Roll It Out and Measure

Switching a shared aspect from everything to almost nothing is a behaviour change, so we roll it out deliberately. We publish the new version of the shared library with the annotation-based pointcut, and a migration note explaining that teams who relied on the logs should add @Logged where it matters. For a transition period we can keep the old pointcut behind a configuration property that defaults to off, so a team can turn it back on if they discover a hidden dependency.

We then measure. Comparing CPU per request and p99 latency before and after on a representative service shows the gain, and profiling a service with @Logged on its entry points confirms the remaining overhead is small. Publishing those numbers makes the case to the remaining teams better than any rule.

6Use Observability Tools for Timing and Tracing

Many package-wide logging aspects really exist to answer which methods are slow and what a request did. Logs are a poor tool for that. Micrometer's Observation API, with @Observed or the observations Spring already creates for HTTP requests, database calls and messaging, records timings as metrics and as spans in a distributed trace, with sampling to control the cost. A trace shows the whole path of a request across services, which a wall of enter and exit log lines never could. So the redesigned aspect keeps a small job, opt-in debug logging, and timing and tracing move to the tools built for them.

7Follow-up questions

Why not just narrow the package, for example to com.company..service..*?

It reduces the damage but keeps the same design problem: matching depends on where code lives, it breaks silently when packages are renamed, and teams cannot see from the code that a method is logged. An annotation is explicit and survives refactoring.

Is the proxy dispatch itself expensive?

Usually not. On a modern JVM, the proxy and interceptor chain add well under a microsecond per call. The expensive part is the work the advice does, especially formatting arguments and writing logs. That is why the redesign checks the log level first and stops logging arguments by default.

How would you log arguments safely when a team really needs them?

Make it explicit, for example an attribute on @Logged that turns on argument logging for that method, and pass the arguments through a masking step that removes known sensitive fields. Never call toString() on entities from an aspect.

8Conclusion

In this answer, we saw that execution(* com.company..*.*(..)) proxies nearly every bean and runs logging advice on every internal call, which explains the high CPU, and that proxying everything brings final-class failures, null fields in final methods, lazy-loading surprises, leaked data and inconsistent logs. The redesign is an opt-in @Logged annotation matched with @annotation or @within, an advice that checks the log level and skips arguments, a measured rollout, and Micrometer observations and tracing for timing.

What to say in the interviewAbout 90 seconds aloud

Open with

The problem is that execution(* com.company..*.*(..)) matches essentially every method of every bean the company wrote. So Spring creates a proxy for almost every bean, and the advice runs on every internal bean-to-bean call, not just on requests.

Then, if they want more

The proxy dispatch itself is cheap; the cost is what the advice does each time, formatting signatures and arguments, often calling toString on big objects, and writing log lines, and the argument formatting even happens when the log level discards the message.

A request with a few hundred internal calls pays that hundreds of times, which shows up clearly in a profile. The odd behaviour comes from proxying things nobody chose to proxy: final classes failing at startup, final methods running on the proxy with null fields, entity toString triggering lazy loading, sensitive data in logs, and self-calls not logged.

I would redesign it as opt-in. Define an annotation like @Logged and match it with @annotation or @within, so only marked beans are proxied. Make the advice check isDebugEnabled first and not log arguments by default. Roll it out with a migration note and a temporary property to restore the old behaviour, and measure CPU and latency before and after.

And for timing and tracing, which is usually the real need, I would point teams to Micrometer observations and distributed tracing rather than logs.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your company has a shared aspect with the pointcut execution(* com.company..*.*(..)) for...": https://scrivio-interview-library.netlify.app/spring-boot/aop/c189810b66/