🔍 10 Tricky Spring Boot Internal working Interview Questions You Must Know (with Answers)
Spring Boot makes life easier for developers, but interviewers love to dig into what happens under the hood. If you're preparing for a Spring Boot interview, these tricky questions and answers will give you an edge!
✅ 1. What exactly happens when you call SpringApplication.run()
?
When you invoke SpringApplication.run()
:
-
It creates and configures the ApplicationContext
-
Loads
application.properties
or.yml
configuration files -
Registers ApplicationListeners and ApplicationInitializers
-
Performs component scanning
-
Triggers auto-configuration via
@EnableAutoConfiguration
Pro Tip: Mention that it uses SpringFactoriesLoader
to load configuration classes from META-INF/spring.factories
.
✅ 2. How does Spring Boot decide which configuration class to load automatically?
Spring Boot uses:
It loads class names from:
Only those relevant to the current classpath dependencies are activated.
✅ 3. What is the difference between @SpringBootApplication
and @EnableAutoConfiguration
?
-
@SpringBootApplication
is a meta-annotation that includes:-
@Configuration
-
@ComponentScan
-
@EnableAutoConfiguration
-
-
Using only
@EnableAutoConfiguration
will not scan for beans.
📌 Tip: Interviewers often test if you understand this convenience wrapper.
✅ 4. If two auto-configuration classes try to create the same bean, who wins?
Spring Boot uses conditional annotations like:
This means:
-
If you define the bean manually → your bean wins
-
Otherwise, Spring uses the auto-configured one
This is how Spring Boot gives you full control to override.
✅ 5. How is DispatcherServlet
registered without a web.xml
?
Spring Boot uses auto-configuration:
Internally, it uses a ServletRegistrationBean
to register the DispatcherServlet
at runtime — no need for web.xml
.
✅ 6. How can you disable a specific auto-configuration?
There are two ways:
a) Using exclude
in @SpringBootApplication
:
b) In application.properties
:
✅ 7. What if multiple application.properties
files are found in the classpath?
Spring Boot loads properties in the following priority order:
-
/config/application.properties
-
/application.properties
in root classpath -
External files via
-Dspring.config.location=...
-
Command-line arguments override all
This enables flexible configuration across environments.
✅ 8. Can you run a Spring Boot app without @SpringBootApplication
?
Yes. Just use the equivalent annotations manually:
But @SpringBootApplication
is the cleaner, preferred approach.
✅ 9. How does Spring Boot determine if it's a web application?
Spring Boot checks the classpath:
-
If it finds
spring-web
,spring-webmvc
, orServletContext
→ it's a web app -
Then it loads web-specific configurations like:
-
DispatcherServletAutoConfiguration
-
WebMvcAutoConfiguration
-
✅ 10. What happens if there is no application.properties
or .yml
?
Spring Boot still starts using default values, such as:
-
Port:
8080
-
Context path:
/
-
Embedded H2 database (if JPA is on classpath)
-
Default error page at
/error
But defining properties is essential for production-ready apps
No comments:
Post a Comment