Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Explain how to write a custom Spring Boot starter for your company's audit logging library, including auto-configuration registration and conditional beans.

A custom starter has two parts: an autoconfigure module with a class annotated @AutoConfiguration, registered in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, and a starter module that is only a dependency list pulling in the autoconfigure module and the audit library. The auto-configuration uses conditions such as @ConditionalOnClass, @ConditionalOnMissingBean and @ConditionalOnProperty so services can override or disable it, binds settings with @ConfigurationProperties under a company prefix with the configuration processor generating metadata, and is tested with ApplicationContextRunner.

Why this question is asked

Every company with many Spring Boot services ends up with cross-cutting libraries, and audit logging is a typical one: every service must record who did what, in the same format, to the same place. Asking each team to wire the library by hand leads to copies that drift apart. A starter lets a team add one dependency and get a correct, configurable setup, the same way spring-boot-starter-web gives them a web server. Interviewers ask this to check that a candidate knows how auto-configuration really works and how to make it safe to override. In this answer, we will recap auto-configuration, build the module layout, write the auto-configuration with its conditions, bind properties, assemble the starter and test it.

1A Quick Recap of Auto-Configuration

Spring Boot's auto-configuration is a set of ordinary @Configuration classes that Boot loads automatically from the libraries on the class path. Each one is guarded by conditions, rules that decide whether its beans should be created, for example only if a certain class is present or only if the application has not defined its own bean of that type. That is why adding spring-boot-starter-data-jpa gives us a configured EntityManagerFactory, and why defining our own DataSource makes Boot's default step aside.

Boot finds these classes through a file in each library's jar: META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, which lists one fully qualified class name per line. Older Boot versions used spring.factories for this; since Boot 3, the imports file is the only way for auto-configurations.

2Laying Out the Modules

The convention from Spring Boot itself is two modules. The autoconfigure module contains the auto-configuration classes, the properties class and the imports file. The starter module contains no code at all; it is a pom that depends on the autoconfigure module, the audit library and anything else a typical user needs. Users depend only on the starter.

Step throughModules of the audit starter
  1. Servicedepends on acme-audit-spring-boot-starter
  2. acme-audit-spring-boot-starterpom only: pulls in the modules below
  3. acme-audit-spring-boot-autoconfigure@AutoConfiguration, properties, imports file
  4. acme-audit-corethe audit library itself, no Spring Boot dependency

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

Names start with the company, not with spring-boot, which is reserved for Spring's own starters.

Splitting them keeps the autoconfigure module's dependencies optional. A service that wants the auto-configuration but a different set of libraries can depend on the autoconfigure module directly. The naming matters too: Spring's guidance is that third-party starters should not start with spring-boot, so we use acme-audit-spring-boot-starter.

3Writing the Auto-Configuration With Conditions

Now the core class. It creates the AuditLogger bean the library needs, but only when it makes sense, and it lets the service replace or switch off anything.

Java
@AutoConfiguration
@ConditionalOnClass(AuditLogger.class) // only if the audit library is present
@ConditionalOnProperty(prefix = "acme.audit", name = "enabled",
havingValue = "true", matchIfMissing = true) // on by default, can be disabled
@EnableConfigurationProperties(AuditProperties.class)
public class AuditAutoConfiguration {
@Bean
@ConditionalOnMissingBean // a service's own AuditSink wins
AuditSink auditSink(AuditProperties props) {
return new KafkaAuditSink(props.getTopic());
}
@Bean
@ConditionalOnMissingBean
AuditLogger auditLogger(AuditSink sink, AuditProperties props) {
return new AuditLogger(sink, props.getServiceName(), props.isIncludePayload());
}
}

Each condition has a job. @ConditionalOnClass keeps the configuration from loading when the library is absent, which matters if the autoconfigure module is used without the starter. @ConditionalOnProperty with matchIfMissing = true means auditing is on unless a service sets acme.audit.enabled=false. @ConditionalOnMissingBean on each bean means a service that defines its own AuditSink, for example one that writes to a file in tests, gets its own, and our default steps aside.

Notice that @ConditionalOnMissingBean is evaluated against beans already defined when this configuration is processed. User configuration is processed before auto-configurations, so the service's own beans are seen. That is also why auto-configuration classes must never be picked up by component scanning; they are loaded only through the imports file.

4Registering It and Binding Properties

The imports file contains a single line, and the properties class binds everything under our company prefix.

Java
# src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.acme.audit.autoconfigure.AuditAutoConfiguration
@ConfigurationProperties(prefix = "acme.audit")
public class AuditProperties {
/** Whether audit logging is enabled. */
private boolean enabled = true;
/** Kafka topic that receives audit events. */
private String topic = "audit.events.v1";
/** Name of the service written into every event. */
private String serviceName;
/** Whether request payloads are included in events. */
private boolean includePayload = false;
// getters and setters
}
# application.yaml in a service
acme:
audit:
service-name: invoice-api # binds to serviceName
include-payload: true

We add spring-boot-configuration-processor as an optional annotation processor dependency of the autoconfigure module. At compile time it reads the properties class and its Javadoc comments and generates META-INF/spring-configuration-metadata.json. IDEs use that file to offer auto-completion and descriptions when a developer types acme.audit. in application.yaml, which makes the starter far easier to use correctly.

5Assembling the Starter Module

The starter is just a pom with dependencies. It has no Java code, so it never needs to change when the auto-configuration changes, only when the set of dependencies does.

XML
<artifactId>acme-audit-spring-boot-starter</artifactId>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>acme-audit-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.acme</groupId>
<artifactId>acme-audit-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId> <!-- needed by the default Kafka sink -->
</dependency>
</dependencies>

Versions come from a company BOM, so the starter, the core library and Spring Boot always fit together.

6Testing It With ApplicationContextRunner

ApplicationContextRunner is a test utility from Spring Boot that starts a small application context with chosen auto-configurations, properties and user beans, runs assertions against it and closes it. It is fast and lets us test each condition separately, without a full @SpringBootTest.

Java
class AuditAutoConfigurationTest {
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AuditAutoConfiguration.class))
.withPropertyValues("acme.audit.service-name=test");
@Test void createsDefaults() {
runner.run(ctx -> assertThat(ctx).hasSingleBean(AuditLogger.class)
.hasSingleBean(KafkaAuditSink.class)); // passes
}
@Test void backsOffWhenUserDefinesSink() {
runner.withBean(AuditSink.class, InMemoryAuditSink::new)
.run(ctx -> assertThat(ctx).hasSingleBean(AuditSink.class)
.doesNotHaveBean(KafkaAuditSink.class)); // user sink wins
}
@Test void canBeDisabled() {
runner.withPropertyValues("acme.audit.enabled=false")
.run(ctx -> assertThat(ctx).doesNotHaveBean(AuditLogger.class)); // nothing created
}
@Test void skipsWithoutLibrary() {
runner.withClassLoader(new FilteredClassLoader(AuditLogger.class))
.run(ctx -> assertThat(ctx).doesNotHaveBean(AuditLogger.class)); // class missing
}
}

FilteredClassLoader hides a class from the context, which lets us test @ConditionalOnClass without building a separate project. Together these four tests cover the defaults, overriding, disabling and the class condition, which are exactly the promises the starter makes to its users. Check your Boot version's documentation for the test packages, since Spring Boot 4 reorganised some modules.

7Follow-up questions

Why use @AutoConfiguration instead of @Configuration?

@AutoConfiguration is a meta-annotation for auto-configuration classes. It marks the class as a configuration with proxyBeanMethods = false, and it accepts before and after attributes for ordering relative to other auto-configurations, such as after = KafkaAutoConfiguration.class so Boot's Kafka beans exist first. It also documents intent: this class is loaded through the imports file, not component scanning.

How does a service see which auto-configurations were applied?

By starting the application with debug=true, which prints the conditions evaluation report listing each auto-configuration and why it matched or did not, or by calling the conditions endpoint of Actuator. That is the first place to look when the starter's beans are missing or unexpectedly present.

Should the starter enable itself by default?

For something every service must have, like audit logging, yes, with a property to turn it off for special cases such as tests. For optional features, it is better to require an explicit property, so adding the dependency for one reason does not switch on unrelated behaviour.

8Conclusion

In this answer, we built a custom Spring Boot starter for an audit logging library. We placed the code in an autoconfigure module with an @AutoConfiguration class registered in the AutoConfiguration.imports file, guarded it with @ConditionalOnClass, @ConditionalOnProperty and @ConditionalOnMissingBean so services can override or disable it, bound settings with @ConfigurationProperties under acme.audit with the configuration processor for IDE metadata, assembled a code-free starter module that pulls in the pieces, and tested every condition with ApplicationContextRunner.

What to say in the interviewAbout 80 seconds aloud

Open with

I would follow the same structure Spring Boot uses for its own starters. There is an autoconfigure module and a starter module. The autoconfigure module has a class annotated @AutoConfiguration, and it is registered by listing its name in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, which is how Boot 3 and later discover auto-configurations.

Then, if they want more

The class uses conditions so it is safe: @ConditionalOnClass on the audit library, @ConditionalOnProperty on acme.audit.enabled with matchIfMissing true so it is on by default but can be switched off, and @ConditionalOnMissingBean on every bean so a service that defines its own sink or logger wins and ours steps aside.

Settings are bound with a @ConfigurationProperties class under the acme.audit prefix, and I add spring-boot-configuration-processor so the build generates metadata and developers get auto-completion in their IDE. The starter module has no code.

It is just a pom that depends on the autoconfigure module, the audit library and things like spring-kafka that the default sink needs, with versions from our BOM, and it is named acme-audit-spring-boot-starter because names starting with spring-boot are reserved.

Finally I test it with ApplicationContextRunner: defaults created, user bean wins, property disables it, and FilteredClassLoader to check the class condition.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "Explain how to write a custom Spring Boot starter for your company's audit logging...": https://scrivio-interview-library.netlify.app/architecture/platform-and-governance/67d5c4004a/