Spring Boot Basics Interview Questions

What is Spring Boot and how is it different from Spring?

Spring FrameworkSpring Boot
DI container + modules (MVC, Data, Security)opinionated layer on top of Spring
you configure beans, DispatcherServlet, data source yourselfauto-configuration sets them up from the classpath
deploy a WAR to an external Tomcatembedded server, runnable fat JAR
pick compatible library versions yourselfstarters + managed versions
no production tooling built inActuator: health, metrics, info

What does @SpringBootApplication do?

It is a shortcut for three annotations:

  • @SpringBootConfiguration (a @Configuration): the class can declare @Bean methods.
  • @EnableAutoConfiguration: turns on auto-configuration.
  • @ComponentScan: scans the package of this class and its sub-packages.

Tip: Keep the main class in the root package (com.shopease); beans in sibling packages outside it are not found.

What are Spring Boot starters?

Starters are dependency bundles for one feature, with versions managed by Spring Boot. spring-boot-starter-web brings Spring MVC, Jackson, validation support hooks and embedded Tomcat; spring-boot-starter-data-jpa brings Hibernate, Spring Data JPA and HikariCP. You add one line instead of a dozen compatible versions.

What is auto-configuration?

Spring Boot ships many @AutoConfiguration classes (listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports). Each one is guarded by conditions like @ConditionalOnClass, @ConditionalOnMissingBean and @ConditionalOnProperty. Example: if HikariCP and a JDBC driver are on the classpath and you defined no DataSource, Boot creates one from spring.datasource.*. Your own bean always wins (back off).

How do you see which auto-configurations were applied?

Start with --debug (or debug=true) to print the Condition Evaluation Report (positive and negative matches), or call the Actuator /actuator/conditions endpoint.

Example

java -jar shopease.jar --debug

Example output

============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
   DataSourceAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.sql.DataSource', ... (OnClassCondition)
Negative matches:
   MongoAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'com.mongodb.client.MongoClient'

What happens when you run SpringApplication.run()?

  1. Decides the app type (servlet, reactive or none) from the classpath.
  2. Prepares the Environment (properties, YAML, env vars, command-line args, profiles).
  3. Prints the banner and creates the ApplicationContext.
  4. Loads bean definitions: component scan + auto-configurations, evaluates conditions.
  5. Refreshes the context: creates singletons, starts the embedded web server.
  6. Runs CommandLineRunner/ApplicationRunner beans and publishes ApplicationReadyEvent.

What is the difference between CommandLineRunner and ApplicationRunner?

Both run once after the context starts (seed data, warm caches). CommandLineRunner.run(String... args) gets raw arguments; ApplicationRunner.run(ApplicationArguments args) gets parsed ones (getOptionValues("mode")). Order several runners with @Order.

What is a fat (executable) JAR?

spring-boot-maven-plugin repackages the app into one JAR containing your classes (BOOT-INF/classes), all dependency JARs (BOOT-INF/lib) and a launcher (JarLauncher) that knows how to load nested JARs. You run it with java -jar app.jar; no external server is needed.