Quick revision
The short answer
I would set a few clear rules. Business logic stays in explicit code or configuration, and run-time annotations are reserved for genuinely cross-cutting concerns such as security, transactions, metrics or auditing. Every custom annotation must document the processor that handles it and what it changes, must come with tests that prove the behaviour, and must be discoverable through a central library rather than copied per team. New run-time annotations go through architecture review, because they affect debugging, onboarding and startup cost for everyone.
What to remember
In this answer, we set guidelines for custom run-time annotations. Business logic stays in explicit code, and annotations are reserved for cross-cutting concerns. Every annotation documents its processor and effects and comes with tests through the real mechanism, processors are findable by convention, shared annotations live in one central library, and new run-time annotations pass a short architecture review because they affect debugging and startup cost. We introduced the rules by cataloguing existing annotations with the teams and applying the rules to new ones first.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would set a few clear rules. Business logic stays in explicit code or configuration, and run-time annotations are reserved for genuinely cross-cutting concerns such as security, transactions, metrics or auditing. Every custom annotation must document the processor that handles it and what it changes, must come with tests that prove the behaviour, and must be discoverable through a central library rather than copied per team. New run-time annotations go through architecture review, because they affect debugging, onboarding and startup cost for everyone.
Why this question is asked
Hidden behaviour is one of the most expensive kinds of complexity in a large codebase. When a method annotated @Enrich or @AutoNotify silently calls two other services, every new developer has to discover that by accident, often during an incident. Interviewers ask this leadership question because the fix is not banning annotations, which are central to Spring, but setting rules that keep their power and remove the surprise. In this answer, we will look at why teams reach for annotations and where it goes wrong, then go through the guidelines one by one with the reasoning behind each, and finish with how to introduce them without stalling teams.
1Why Teams Reach for Annotations, and Where It Goes Wrong
Annotations feel productive. One line, @SendWelcomeEmail on a method, replaces a call to a notification service, and the method looks clean. Frameworks taught us this style: @Transactional and @Cacheable work beautifully. So teams copy the pattern for their own needs.
The trouble is that the framework annotations are documented, widely understood and stable, while a team's own annotations are none of those. Consider an example situation. An on-call engineer is debugging why an order was emailed twice. The code says repository.save(order) and nothing else. Two hours later they discover an @AfterSave annotation on the entity, processed by an aspect in another module, which sends the email, and which a recent refactoring made run twice. Nothing in the code they were reading pointed there. That is the cost we are trying to remove: behaviour that cannot be found by reading the code.
2Guideline 1: Business Logic Is Explicit, Annotations Are for Cross-Cutting Concerns
The core rule separates two kinds of behaviour. Cross-cutting concerns apply the same way across many unrelated methods and are not part of the business story: security checks, transactions, retries, metrics, tracing, auditing. Annotations suit them well, because repeating the code everywhere would be worse, and readers already expect them to exist.
Business logic, such as sending a welcome email, enriching an order or recalculating a price, is part of what the method does and belongs in the code, where a reader sees it. A one-line explicit call, notifications.welcome(customer), costs almost nothing and is discoverable with a click. A useful test in review: if removing the annotation would change the business outcome in a way a product manager would notice, it should be explicit code.
3Guideline 2 and 3: Document the Processor, Prove the Behaviour
Every custom annotation's Javadoc names the class that processes it, whether an aspect, a bean post-processor or an annotation processor, and states exactly what it changes: which calls it adds, which exceptions it may throw, what it costs. A developer hovering over the annotation in the IDE should learn in ten seconds what it does and where to look.
Each annotation also ships with tests that prove its behaviour through the real mechanism, for example a Spring context test showing that the audit record is written, and that it is not written for a self-call if that is a known limit. To make processors findable, we ask that they follow a naming convention and live in a known package, so a search for implementations, or a simple build report, lists every processor in the codebase with the annotation it handles.
4Guideline 4: One Central Library, Not Copies Per Team
When three teams each write their own @Audited, the organisation ends up with three slightly different behaviours under the same name, and developers moving between teams carry the wrong assumptions. Shared annotations belong in a central library owned by a platform or architecture group, versioned like public API, with one implementation and one set of docs. Teams that need something new propose it there. Team-local annotations remain possible, but they follow the same documentation and test rules and are expected to stay local.
5Guideline 5: Review New Run-Time Annotations in Architecture Review
A new run-time annotation is not a local decision. It adds a proxy or a scan that affects startup time, it changes how stack traces look, it may not work in GraalVM native images without extra hints, and every future reader has to learn it. So new run-time annotations get a short architecture review: what problem they solve, why explicit code or an existing framework annotation is not enough, how they are processed, and what their limits are. The review should be quick, a page and a short discussion, so it does not become a reason to avoid the process.
Over time, the review also gives us a catalogue of approved annotations, which is the best documentation a new developer can get.
- Is it business logic?YesWrite explicit code insteadNoContinue
- Does Spring or the central library already cover it?YesUse the existing annotationNoContinue
- Document processor and effectsJavadoc names the class and what it changes
- Add tests through the real mechanism
- Architecture reviewstartup cost, debugging, native image
- Publish in the central library
6 steps in order. Play them, or step through them one at a time.
6Introducing the Guidelines Without Stalling Teams
Rules that arrive as a ban generate workarounds. I would start by listing the existing custom annotations, which a simple scan for @interface declarations with RUNTIME retention produces, and classifying them together with the teams: keep and document, move to the central library, or replace with explicit code. Then the guidelines apply to new annotations immediately and to existing ones over a few quarters, starting with those that caused incidents.
It also helps to measure the effect: time to understand unfamiliar code in onboarding feedback, and incidents traced to hidden behaviour. When developers see that the rules shorten debugging, they enforce them in code review themselves, which is the only enforcement that really scales.
7Follow-up questions
Is a Spring event listener better than an annotation for side effects like the welcome email?
It is more explicit, because the code publishes an event with a visible call, and listeners can be found by searching for the event type. It still separates the trigger from the reaction, so it suits cases where several independent reactions are expected. For a single, essential step, a direct call is clearer still.
What if a team argues that explicit calls clutter their methods?
Clutter is usually a sign that the method does too much, not that the calls should be hidden. Extracting a well-named method or a small application service keeps it readable and explicit. Where the concern really is cross-cutting, the guideline already allows an annotation.
How do you find all run-time annotation processors in a large codebase?
Combine a convention with a check. Processors live in a known package and follow a naming pattern, and an ArchUnit test or build report lists classes annotated with @Aspect or implementing BeanPostProcessor together with the annotations they reference. The report becomes part of the central library's documentation.
8Conclusion
In this answer, we set guidelines for custom run-time annotations. Business logic stays in explicit code, and annotations are reserved for cross-cutting concerns. Every annotation documents its processor and effects and comes with tests through the real mechanism, processors are findable by convention, shared annotations live in one central library, and new run-time annotations pass a short architecture review because they affect debugging and startup cost. We introduced the rules by cataloguing existing annotations with the teams and applying the rules to new ones first.
Open with
I would not ban annotations, because Spring's own ones like @Transactional work well. The problem is hidden business behaviour, so my first guideline is that business logic stays in explicit code or configuration, and annotations are for cross-cutting concerns like security, transactions, retries, metrics and auditing.
Then, if they want more
A quick test is: if removing the annotation would change a business outcome someone would notice, like an email being sent, it should be an explicit call. Second, every custom annotation must document in its Javadoc which processor handles it, the aspect or post-processor class, and exactly what it changes, so a developer can find it in seconds.
Third, it needs tests that prove the behaviour through the real mechanism, including known limits like self-calls, and processors follow a naming and package convention so we can list all of them. Fourth, shared annotations live in one central library with one implementation, instead of each team writing its own @Audited with slightly different behaviour.
Fifth, any new run-time annotation gets a short architecture review, because it affects startup cost, stack traces, native images and everyone's ability to debug. To roll this out, I would first catalogue the existing annotations with the teams, decide which to keep, centralise or replace, and apply the rules to new ones straight away.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Teams in your organisation keep inventing custom annotations that trigger hidden...": https://scrivio-interview-library.netlify.app/java/annotations/cf413ce7ca/