Saturday, 5 April 2025

 

🔍 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:


SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class)

It loads class names from:


META-INF/spring.factories

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:


@ConditionalOnMissingBean

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:


DispatcherServletAutoConfiguration

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:


@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })

b) In application.properties:


spring.autoconfigure.exclude=
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

✅ 7. What if multiple application.properties files are found in the classpath?

Spring Boot loads properties in the following priority order:

  1. /config/application.properties

  2. /application.properties in root classpath

  3. External files via -Dspring.config.location=...

  4. 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:


@Configuration @EnableAutoConfiguration @ComponentScan

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, or ServletContext → 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