Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

An internal starter must work whether or not the application uses Micrometer or a tracing library. How would you design its beans?

I would build the starter as a core that depends on nothing optional, plus one separate configuration class per integration, each activated with @ConditionalOnClass only when Micrometer or the tracing library is on the classpath. The core bean receives its optional collaborators through ObjectProvider, so it works with or without them. Keeping each integration in its own class prevents class loading errors when a library is missing, and ApplicationContextRunner with FilteredClassLoader tests the starter with and without each library.

Why this question is asked

Internal starters are used by services with very different dependencies. One service has Micrometer and OpenTelemetry tracing, another has only Micrometer, a batch job has neither. A starter that assumes everything is present breaks the smallest services with NoClassDefFoundError, and a starter that ignores the libraries misses metrics and traces where they exist. Interviewers ask this design question because getting it right needs three things together: conditions, optional injection and careful class layout. In this answer, we will recap the problem, design the core bean, add each integration in its own configuration, see why the layout prevents class loading errors, and test every combination.

1A Quick Recap of the Problem

Suppose the starter provides a RateLimiter bean. When Micrometer is present, it should record a counter for rejected requests; when a tracing library such as Micrometer Tracing is present, it should tag the current span. A dependency marked optional in the starter's build file, Maven's <optional>true</optional> or Gradle's compileOnly, lets the starter compile against those libraries without forcing them on every user. At runtime, though, the classes may simply not exist.

If any class the JVM loads mentions MeterRegistry in a method signature or field, and Micrometer is not on the classpath, the JVM fails with NoClassDefFoundError when loading or linking that class. So the design must make sure that code mentioning optional types is only loaded when those types exist.

2The Core Bean Knows Only Its Own Hooks

The core RateLimiter must not mention Micrometer or tracing types at all. Instead it defines its own small listener interface, and integrations implement it.

Java
public interface RateLimitListener { void rejected(String key); } // starter's own type, always present
public class RateLimiter {
private final List<RateLimitListener> listeners;
public RateLimiter(List<RateLimitListener> listeners) { this.listeners = listeners; }
public boolean tryAcquire(String key) {
boolean ok = /* token bucket */ true;
if (!ok) listeners.forEach(l -> l.rejected(key));
return ok;
}
}
@AutoConfiguration
public class RateLimitAutoConfiguration {
@Bean @ConditionalOnMissingBean
RateLimiter rateLimiter(ObjectProvider<RateLimitListener> listeners) {
return new RateLimiter(listeners.orderedStream().toList()); // empty list if no integration is active
}
}

Notice that the core configuration takes an ObjectProvider<RateLimitListener>. If no integration is active, orderedStream() is simply empty and the RateLimiter works without metrics or traces. A plain List<RateLimitListener> parameter would fail at startup when there are no listeners, which is exactly the case we must support.

3One Configuration Class per Integration

Each optional library gets its own configuration class, guarded by @ConditionalOnClass. The condition is checked before Spring loads the class's bean methods, using the class name, so the configuration is skipped cleanly when the library is missing.

Java
@AutoConfiguration(after = RateLimitAutoConfiguration.class)
@ConditionalOnClass(name = "io.micrometer.core.instrument.MeterRegistry")
class RateLimitMetricsAutoConfiguration {
@Bean @ConditionalOnBean(MeterRegistry.class)
RateLimitListener metricsListener(MeterRegistry registry) {
Counter c = registry.counter("ratelimit.rejected");
return key -> c.increment();
}
}
@AutoConfiguration(after = RateLimitAutoConfiguration.class)
@ConditionalOnClass(name = "io.micrometer.tracing.Tracer")
class RateLimitTracingAutoConfiguration {
@Bean @ConditionalOnBean(Tracer.class)
RateLimitListener tracingListener(Tracer tracer) {
return key -> { Span s = tracer.currentSpan(); if (s != null) s.tag("ratelimit.rejected", key); };
}
}
// Micrometer only: RateLimiter gets [metricsListener]
// Both: RateLimiter gets [metricsListener, tracingListener]
// Neither: RateLimiter gets [] and works
Step throughStarter layout
  1. RateLimitTracingAutoConfiguration@ConditionalOnClass Tracer: only loaded with tracing
  2. RateLimitMetricsAutoConfiguration@ConditionalOnClass MeterRegistry: only loaded with Micrometer
  3. RateLimitAutoConfigurationcore: RateLimiter with ObjectProvider<RateLimitListener>
  4. Starter API typesRateLimiter, RateLimitListener: no optional imports

4 layers, from the top down. Step through them one at a time.

Optional types appear only in classes that are skipped when the library is missing.

Both classes are listed in the starter's META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file. @ConditionalOnClass answers "is the library on the classpath?", and @ConditionalOnBean answers "has the application actually configured a MeterRegistry or Tracer?", since a library can be present without being set up. Note that @ConditionalOnBean is only reliable in auto-configuration ordered after the configuration that defines the bean, so in a real starter we also order these after Boot's metrics and tracing auto-configurations (check your Boot version for their class names).

4Why the Separation Prevents Class Loading Errors

Imagine putting the metrics @Bean method inside the core RateLimitAutoConfiguration, guarded by @ConditionalOnClass on the method. The method signature mentions MeterRegistry. When Spring introspects the configuration class with reflection, to find its @Bean methods, the JVM must resolve the types in every method signature, and without Micrometer it throws NoClassDefFoundError for the whole class, taking the core RateLimiter down with it.

Spring Boot avoids this by reading class-level conditions from the bytecode, using ASM, before the class is ever loaded, which is why @ConditionalOnClass is safe on a configuration class, and even safe with a class literal there. Putting each integration in its own class, with the condition on the class, means a missing library makes Spring skip a class it never loads. That is the same pattern Spring Boot's own auto-configurations follow, often as nested static configuration classes.

5Testing Every Combination

ApplicationContextRunner starts a small context with just our auto-configurations, and FilteredClassLoader hides chosen classes, so we can simulate a classpath without Micrometer or tracing in an ordinary unit test.

Java
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RateLimitAutoConfiguration.class,
RateLimitMetricsAutoConfiguration.class, RateLimitTracingAutoConfiguration.class));
@Test void worksWithoutAnyOptionalLibrary() {
runner.withClassLoader(new FilteredClassLoader("io.micrometer"))
.run(ctx -> {
assertThat(ctx).hasNotFailed().hasSingleBean(RateLimiter.class); // passes
assertThat(ctx).doesNotHaveBean("metricsListener"); // passes
});
}
@Test void recordsMetricsWhenRegistryPresent() {
runner.withBean(MeterRegistry.class, SimpleMeterRegistry::new)
.withClassLoader(new FilteredClassLoader("io.micrometer.tracing"))
.run(ctx -> assertThat(ctx).hasBean("metricsListener").doesNotHaveBean("tracingListener")); // passes
}

We write one test per combination we promise to support: neither library, Micrometer only, tracing only if that makes sense, and both. FilteredClassLoader hides classes from the context's class loader, which catches most mistakes, but the test JVM still has the libraries on its classpath. So I would also add one small sample application in CI built without the optional dependencies at all, which proves there is no stray reference to them.

6Follow-up questions

Why not inject MeterRegistry directly with ObjectProvider<MeterRegistry> in the core?

Because then the core class mentions MeterRegistry in a generic type and in the code that uses it, so loading the core class can fail without Micrometer, and the core becomes coupled to every optional library. The starter's own listener interface keeps the core free of optional types and lets new integrations be added without touching it.

What is the difference between @ConditionalOnClass and @ConditionalOnBean here?

@ConditionalOnClass checks the classpath: is the library's code present at all? @ConditionalOnBean checks the context: has someone created a bean of that type? We usually need both, because a library can be on the classpath through a transitive dependency without being configured. @ConditionalOnBean depends on ordering, so it belongs in auto-configuration classes ordered after the ones that create the bean.

How should the starter declare the optional libraries in its build?

As optional or compileOnly dependencies, so the starter compiles against them but does not pull them into every application. Applications that want metrics or tracing add the libraries themselves, usually through Spring Boot's own starters such as spring-boot-starter-actuator, and the integration switches on automatically.

7Conclusion

In this answer, we designed a starter that works with or without Micrometer and tracing. The core RateLimiter knows only the starter's own listener interface and receives listeners through ObjectProvider, so it works when none exist. Each integration lives in its own auto-configuration class guarded by @ConditionalOnClass and @ConditionalOnBean, so a missing library means a class that is skipped, never loaded, and never throws NoClassDefFoundError. ApplicationContextRunner with FilteredClassLoader tests each combination, and a sample build without the libraries proves there are no stray references.

What to say in the interviewAbout 90 seconds aloud

Open with

I would split the starter into a core and separate integrations. The core bean, say a RateLimiter, must not mention Micrometer or tracing types at all. Instead the starter defines its own small hook, a RateLimitListener interface, and the core auto-configuration builds the RateLimiter from an ObjectProvider of listeners, using orderedStream, so with no integrations it just gets an empty list and still works.

Then, if they want more

A plain List parameter would fail at startup when empty. Then each optional library gets its own auto-configuration class, guarded at class level by @ConditionalOnClass with the library's class name, plus @ConditionalOnBean for the registry or tracer, because a library can be on the classpath without being configured.

Those classes contribute listener beans that record a counter or tag the current span. The separation is the important part: if I put a @Bean method mentioning MeterRegistry in the core class, reflecting over that class without Micrometer throws NoClassDefFoundError and takes down the core too.

Class-level conditions are read from bytecode before the class is loaded, so a missing library simply means a skipped class. Optional libraries are declared optional or compileOnly in the build.

For tests, I use ApplicationContextRunner with FilteredClassLoader to hide io.micrometer or just the tracing packages and assert which beans exist in each combination, and I add one sample application in CI built without the optional dependencies at all.

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 "An internal starter must work whether or not the application uses Micrometer or a...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/56e9ccd58b/