Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

You are defining a set of composed annotations for your organisation's Spring starter, for example @InternalApi combining security, validation and transaction settings. What rules do you set so they stay understandable and safe?

Keep each composed annotation focused on one concern, document exactly which meta-annotations it carries and with which defaults, and expose only the attributes teams genuinely need through @AliasFor, so nothing important is hidden or impossible to override. Forbid stacking composed annotations that set the same underlying attribute, or define precedence and enforce it, and treat the annotations as public API whose meta-annotations are versioned, because removing one changes behaviour without any compile error. Finally, ship tests in the starter that assert the merged behaviour on sample classes, plus a check that detects conflicting combinations.

Why this question is asked

A composed annotation like @InternalApi is attractive: one line replaces a security rule, a validation setting and a transaction configuration, and every team gets the same defaults. The danger is the same as its appeal. One line now hides three behaviours, readers cannot see them, and a change in the starter can alter security or transactions in 50 services without a single compile error. Interviewers ask this design question to see whether we can give teams the convenience without the surprises. In this answer, we will look at what can go wrong, then set six rules covering scope, documentation, attributes, conflicts, versioning and testing, with an example annotation and a conflict check.

1What Can Go Wrong

Suppose the starter defines @InternalApi as a class-level annotation carrying @PreAuthorize("hasRole('INTERNAL')"), @Validated and @Transactional(readOnly = true). A team puts it on a controller that also writes data. Their writes start failing or behaving oddly under a read-only transaction, and nobody suspects the annotation, because readOnly appears nowhere in their code. Another team stacks @InternalApi with @PartnerApi, which carries a different @PreAuthorize, and Spring picks one of them by its search order, so one security rule silently disappears.

Both problems come from the same place: composed annotations move behaviour out of sight. Our rules exist to bring it back into sight and keep it predictable.

2Rule 1 and 2: One Concern Each, Fully Documented

A composed annotation should answer one question that a reader can guess from its name. @InternalEndpoint for security, @ReadOnlyService for transactions, @ValidatedApi for validation. Combining unrelated concerns into one annotation saves a line but makes it impossible to adopt one without the others. If we really want a bundle, it should be a thin combination of focused annotations, each of which also exists on its own.

Each annotation's Javadoc lists every meta-annotation it carries and every default value it sets, and is itself @Documented so it shows in the docs of annotated classes. A reader in an IDE should be one hover away from knowing exactly what the annotation turns on.

Java
/**
* Marks an endpoint as reachable only by internal callers.
* Carries: @PreAuthorize("hasAuthority('SCOPE_internal')").
* Sets nothing else. Combine with @ReadOnlyService for read-only transactions.
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@PreAuthorize("hasAuthority('SCOPE_internal')")
public @interface InternalEndpoint { }
/** Carries: @Transactional(readOnly = true, timeout = 5 seconds by default). */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Transactional(readOnly = true)
public @interface ReadOnlyService {
@AliasFor(annotation = Transactional.class, attribute = "timeout")
int timeout() default 5;
}

Notice that security and transactions are now separate annotations. A team that needs internal-only writes uses @InternalEndpoint alone and is not dragged into a read-only transaction.

3Rule 3: Expose Only the Attributes Teams Need

Through @AliasFor we decide which attributes of the meta-annotations teams may set. Exposing timeout on @ReadOnlyService is reasonable; exposing every attribute of @Transactional would turn the composed annotation into a renamed copy with no value. But anything a team might legitimately need to change must either be exposed or documented as fixed with a clear alternative, such as using the plain Spring annotation instead. What we avoid is behaviour that cannot be overridden or even seen: a hidden rollbackFor rule, a propagation setting, or a pointcut that matches the annotation and does extra work nobody documented.

4Rule 4: Forbid or Define Stacking Conflicts

When two composed annotations on the same element both carry @Transactional or @PreAuthorize with different values, Spring's merged-annotation search finds both, and which one wins depends on declaration order and search distance. That is too subtle to rely on. Our rule is that composed annotations touching the same underlying annotation may not be combined, and the starter enforces it. A startup check in the starter can scan beans and fail fast, and an ArchUnit test in each service can fail the build.

Java
// In the starter: fail at startup if two of our annotations set @Transactional on one class
@Bean
static BeanPostProcessor composedAnnotationGuard() {
return new BeanPostProcessor() {
public Object postProcessBeforeInitialization(Object bean, String name) {
Class<?> type = AopUtils.getTargetClass(bean);
long sources = MergedAnnotations.from(type).stream(Transactional.class)
.filter(a -> a.getDistance() > 0) // reached through a composed annotation
.map(a -> a.getRoot().getType()) // which of our annotations carried it
.distinct().count();
if (sources > 1) throw new IllegalStateException(name + " stacks conflicting transactional annotations");
return bean;
}
};
}

Check the MergedAnnotation methods against your Spring version; the idea is to count how many different composed annotations contribute the same underlying annotation. If we prefer to allow some combinations, we document an explicit precedence and test it, but forbidding is simpler for everyone.

5Rule 5 and 6: Version Them Like Public API and Test the Merged Result

A composed annotation is a public API whose behaviour lives in its meta-annotations. Removing @Validated from @ValidatedApi, or changing a default timeout from 5 to 30 seconds, compiles cleanly in every service and changes what they do. So such changes follow semantic versioning: behaviour-changing edits go into a major version with release notes, and additions that tighten security are announced like any breaking change.

Because the compiler cannot protect us, tests in the starter do. For each composed annotation, a test annotates a sample class and asserts the merged result: which security expression applies, whether the transaction is read-only, what the timeout is, and that the real Spring infrastructure honours it, for example by calling the sample through a Spring context and checking that a write fails under @ReadOnlyService. When someone changes an annotation, a failing test forces the version and release-note discussion.

Step throughReviewing a new composed annotation for the starter
  1. Does it cover one concern?
    YesContinueNoSplit it into focused annotations
  2. Document every meta-annotation and default
  3. Expose needed attributes with @AliasFor
  4. Could it stack with another that sets the same thing?
    YesAdd it to the conflict guardNoContinue
  5. Add merged-behaviour tests on sample classes
  6. Release under semantic versioning

6 steps in order. Play them, or step through them one at a time.

6Follow-up questions

How do you detect that a team stacked two conflicting composed annotations?

Two layers work well together. A startup guard in the starter uses MergedAnnotations to find underlying annotations such as @Transactional or @PreAuthorize that are contributed by more than one composed annotation on the same class or method, and fails the application with a clear message. An ArchUnit rule, which the starter can ship for services to include in their tests, catches the same combinations at build time, before deployment.

Should composed annotations ever carry @Component?

Only when the annotation is meant to declare a bean, like a custom stereotype such as @UseCase. Mixing bean declaration with cross-cutting behaviour in one annotation makes it hard to use the behaviour on classes that are not beans, so we keep stereotypes and behaviour annotations separate.

Is it better to use plain Spring annotations instead?

Often, yes. A composed annotation earns its place when the combination is used in many services and the defaults are an organisational decision, such as a standard internal-scope security rule. For a combination used in two places, the plain annotations are clearer.

7Conclusion

In this answer, we set rules that keep an organisation's composed annotations understandable and safe. Each annotation covers one concern and documents every meta-annotation and default it carries. Only the attributes teams need are exposed through @AliasFor, with no hidden behaviour that cannot be overridden. Stacking annotations that set the same underlying attribute is forbidden and enforced by a startup guard or ArchUnit rule. The annotations are versioned like public API, since removing a meta-annotation changes behaviour without a compile error, and starter tests assert the merged behaviour on sample classes.

What to say in the interviewAbout 80 seconds aloud

Open with

My first rule is one concern per annotation. Instead of one @InternalApi that does security, validation and transactions, I would have @InternalEndpoint for the security rule and @ReadOnlyService for the transaction, so a team that needs internal writes is not pulled into a read-only transaction.

Then, if they want more

Second, each annotation documents in its Javadoc exactly which meta-annotations it carries and the defaults it sets, and it is @Documented so readers can see it. Third, I expose only the attributes teams really need through @AliasFor, like a timeout, and avoid hidden behaviour that cannot be overridden, such as an aspect that quietly matches the annotation.

Fourth, stacking: if two composed annotations both carry @Transactional or @PreAuthorize with different values, which one wins depends on Spring's search order, so I forbid those combinations and enforce it with a startup check using MergedAnnotations and an ArchUnit rule. Fifth, I version them like public API.

Removing a meta-annotation or changing a default compiles everywhere and silently changes behaviour, so it goes into a major version with release notes. And sixth, the starter has tests that put each annotation on a sample class, load a Spring context, and assert the merged behaviour, for example that a write fails under the read-only annotation.

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 "You are defining a set of composed annotations for your organisation's Spring starter...": https://scrivio-interview-library.netlify.app/java/annotations/01e27d56b1/