Scrivio Interview library
CodingIntermediate5 min read · updated 12 November 2026

How do you customise startup without changing application.yml, for example setting a default profile, adding a listener and disabling the banner? Show code using SpringApplication or SpringApplicationBuilder.

We create new SpringApplication(App.class) and, before calling run(args), call setAdditionalProfiles("dev"), addListeners(...) and setBannerMode(Banner.Mode.OFF). SpringApplicationBuilder offers the same in a fluent form and also supports parent-child contexts, and setDefaultProperties supplies properties with the lowest precedence. Listeners added this way also receive the early events, such as ApplicationEnvironmentPreparedEvent, that bean listeners never see.

Why this question is asked

application.yml is the usual place for configuration, but some settings belong in code: defaults that every environment should get, behaviour that depends on how the program was launched, or listeners that must run before beans exist. Interviewers ask this to see whether we know the SpringApplication API, and whether we understand the difference between configuring startup and configuring beans. In this answer, we will customize startup with SpringApplication, do the same with SpringApplicationBuilder, look at default properties and the difference between additional and default profiles, and see why listeners added here can see early events.

1Customizing With SpringApplication

The static SpringApplication.run is a shortcut. If we create the SpringApplication ourselves, we can call its setters before run(args). Here is a main method that adds a profile, registers a listener and turns off the banner.

Java
public static void main(String[] args) {
SpringApplication app = new SpringApplication(BillingApplication.class);
app.setAdditionalProfiles("local"); // active profiles: local (+ any from config)
app.addListeners(new StartupTimingListener()); // receives early events too
app.setBannerMode(Banner.Mode.OFF); // no banner printed
app.run(args);
}

setAdditionalProfiles adds profiles to whatever is active from properties, so starting with --spring.profiles.active=prod would give the active profiles prod and local together. That is often not what we mean by a default profile, which brings us to the next point.

2Additional Profiles Versus a Default Profile

The word default can mean two things. An additional profile is always active, whatever else is set. A default profile is active only when no profile has been chosen. Spring has a property for the second meaning, spring.profiles.default, and we can set it from code with setDefaultProperties.

Java
SpringApplication app = new SpringApplication(BillingApplication.class);
app.setDefaultProperties(Map.of(
"spring.profiles.default", "local", // used only if no profile is active
"server.port", "8081")); // lowest precedence of all sources
app.run(args);
// java -jar billing.jar -> active profiles: [local], port 8081
// java -jar billing.jar --spring.profiles.active=prod -> active profiles: [prod]
// SERVER_PORT=9000 java -jar billing.jar -> port 9000

Default properties sit at the very bottom of Boot's property order, below application.yml, environment variables and command-line arguments. That is exactly what we want for code-level defaults: they apply when nobody configured anything, and any real configuration wins. So for a developer-friendly default profile that disappears in production, the default property is the right tool, and setAdditionalProfiles is for profiles that must always be on.

3The Same With SpringApplicationBuilder

SpringApplicationBuilder wraps SpringApplication in a fluent API. It reads well when there are several settings, and it adds one feature SpringApplication does not have directly: building a hierarchy of contexts.

Java
public static void main(String[] args) {
new SpringApplicationBuilder(BillingApplication.class)
.profiles("local") // like setAdditionalProfiles
.listeners(new StartupTimingListener())
.bannerMode(Banner.Mode.OFF)
.properties("spring.profiles.default=local") // default properties
.run(args);
}
// a parent context with shared beans and a child web context
new SpringApplicationBuilder(SharedConfig.class)
.web(WebApplicationType.NONE)
.child(WebConfig.class)
.web(WebApplicationType.SERVLET)
.run(args); // child can see parent beans, not the other way

Parent-child contexts are rare in modern services, but they explain some surprises, for example a listener in the parent receiving events from the child, because events published in a child context are also published to its parent.

4Why Listeners Added Here See Early Events

A listener annotated with @EventListener or registered as a bean only exists after the context has started creating beans. By then several events have already been published. A listener passed to addListeners is a plain object that SpringApplication holds from the start, so it receives everything.

Java
class StartupTimingListener implements ApplicationListener<SpringApplicationEvent> {
private long start;
@Override public void onApplicationEvent(SpringApplicationEvent event) {
if (event instanceof ApplicationStartingEvent) start = System.nanoTime();
if (event instanceof ApplicationEnvironmentPreparedEvent e)
System.out.println("profiles: " + Arrays.toString(e.getEnvironment().getActiveProfiles())); // profiles: [local]
if (event instanceof ApplicationReadyEvent)
System.out.println("ready in " + (System.nanoTime() - start) / 1_000_000 + " ms"); // ready in 2410 ms
}
}
Which listeners see which events
EventaddListeners / spring.factories@EventListener bean
ApplicationStartingEventYesNo
ApplicationEnvironmentPreparedEventYesNo
ApplicationContextInitializedEventYesNo
ApplicationPreparedEventYesNo
ContextRefreshedEventYesYes
ApplicationReadyEventYesYes

The same listener registered as a @Component would never see ApplicationStartingEvent or ApplicationEnvironmentPreparedEvent, because they happen before beans exist. For a listener that should apply to every application without touching main, the alternative is to list it in META-INF/spring.factories under org.springframework.context.ApplicationListener.

5Follow-up questions

Do these code settings override application.yml?

It depends on the setting. setDefaultProperties is the lowest precedence, so application.yml wins over it. setAdditionalProfiles always adds the profiles. setBannerMode in code takes effect unless spring.main.banner-mode is set in configuration, which is applied to the SpringApplication when the environment is bound. When in doubt, check your version's documentation for the exact order.

When would you use setDefaultProperties instead of application.yml?

For defaults that belong to the program itself, for example in a library-style launcher shared by many apps, or when the value is computed at launch time. For ordinary settings of one service, application.yml is clearer.

Can I disable the banner without code?

Yes, with spring.main.banner-mode=off in properties or as SPRING_MAIN_BANNER_MODE=off. The code form is useful when the decision depends on how the program was started.

6Conclusion

In this answer, we customized Spring Boot startup without application.yml. We built a SpringApplication and called setAdditionalProfiles, addListeners and setBannerMode before run, saw that setDefaultProperties supplies the lowest-precedence properties and is the right way to set a true default profile, used SpringApplicationBuilder for a fluent form and parent-child contexts, and saw that listeners added here receive early events that bean listeners cannot.

What to say in the interviewAbout 70 seconds aloud

Open with

Instead of calling the static run, I would create new SpringApplication with the main class, and before calling run with the args I would call setAdditionalProfiles for the profile, addListeners for my listener and setBannerMode with Banner.Mode.OFF.

Then, if they want more

One subtlety is what default profile means. setAdditionalProfiles makes a profile always active, on top of whatever is configured. If I want a profile only when nobody chose one, I would use setDefaultProperties with spring.profiles.default, because default properties are the lowest precedence of all, so any real configuration wins.

The same can be written fluently with SpringApplicationBuilder: profiles, listeners, bannerMode, properties and then run, and the builder can also create parent-child contexts with child, where the child sees the parent's beans.

Listeners registered with addListeners are important because they exist from the very start, so they receive ApplicationStartingEvent and ApplicationEnvironmentPreparedEvent, which an @EventListener bean never sees because beans do not exist yet. If I need such a listener in every app without touching main, I would register it in spring.factories instead.

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
Spring Boot reference
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 "How do you customise startup without changing application.yml, for example setting a...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/f609210fdb/