Spring Boot Interview Questions

Spring Boot is a spring module that provides the Rapid Application Development features for spring framework. It is used in creating the standard alone spring-based application which you can run with little spring configuration.

In this article, you can go through the set of spring boot interview questions most frequently asked in the interview panel. This will help you crack the interview as the topmost industry experts curate these at HKR training.

Let us have a quick review of the spring boot interview questions.

Most Frequently Asked Spring Boot Interview Questions

1. What are the advantages of Spring Boot?

 The advantages are.

  • Creating the standard alone applications which can be started using java -jar.
  • The ability to embed Tomcat, Jetty or Undertow directly without any need for deployment of WAR files.
  • It provides opinionated 'starter' POMs to simplify your Maven configuration.
  • It configures the spring automatically whenever it is possible.

2. Name the Spring Boot features.

The features of Spring Boot are.

  • Web development.
  • Spring Application.
  • Application events and listeners.
  • Admin features.

3. What is Spring Boot dependency management?

The dependency management of Spring Boot automatically manages the configurations and dependencies. It is not required to specify a version for any of these dependencies. By upgrading the Spring Boot the dependencies of Spring Boot also upgrades automatically.

Become a Spring Boot Certified professional by learning this HKR Spring Boot Training

4. What is the difference between Spring vs Spring Boot?

  • Spring is a web application framework based on java.
  • The customized web applications are created using the tools and libraries that are provided by the Spring framework.
  • Spring is more complex than Spring Boot.
  • It takes opinionated view.

Spring Boot:

  • Spring Boot is a module of a spring.
  • Spring Boot is used in creating a Spring application project which can just run/execute.
  • Spring Boot is less complex than the Spring framework.
  • It takes an opinionated view of a platform.

Become a Expert in Spring Boot with HKR. Join now Spring Boot Microservices Training!

5. What are the Spring Boot starters and what are available starters?

The Spring Boot Starters are the set of convenient dependency management providers which is used in the application for enabling the dependencies. These starters make the easy and rapid deployment. All the available starters will come under the “org.springframework.boot” group. Few of the popular starters are as follows:

  • spring-boot-starter:  It is the core starter which includes logging, auto-configuration support, and YAML.
  • spring-boot-starter-jdbc: This starter is used for HikariCP connection pool with JDBC.
  • spring-boot-starter-web: This starter is used for building web applications. It also includes RESTful and applications that use Spring MVC.
  • spring-boot-starter-data-jpa: It Is the starter that is used for Spring Data JPA with Hibernate
  • spring-boot-starter-security: It is the starter used for Spring Security.
  • spring-boot-starter-aop: This starter is used for aspect-oriented programming with AspectJ and  Spring AOP.
  • spring-boot-starter-test: It is the starter used in testing Spring Boot applications.

6. Explain how to create a Spring Boot application using Maven.

There are various approaches to create a Spring Boot project using Maven. These are the few approaches that are followed in creating a Spring Boot application using Maven.

  • Spring Boot CLI.
  • Spring Starter Project Wizard.
  • Spring Initializr.
  • Spring Maven Project.

7. Can you explain what happens in the background when a spring boot application is “Run as Java Application”?

The tomcat server is launched automatically when the spring boot is executed as “Run as Java Application” while developing a web application.

8. Mention the steps to create a Spring Boot project using Spring Initializr.

Spring Initializr is a web tool of Spring which helps in creating Spring Boot projects by providing the project details. The creating steps are as follows.

  1. Choose the maven project and its dependency requirements. Then, fill the details like Group, Artifact and then click on Generate Project.
  2. Extract the project once it is downloaded in your system.
  3. With the “import” option try importing the project under “Spring Tool Suite IDE”. Then choose the project type as “Maven” and source project file that contains pom.xml file.

With these steps, you can see the Spring Boot project is created with all required dependencies.

9. What is thymeleaf and how is it used?

Thymeleaf is a server-side java template engine used for web applications. The main purpose of thymeleaf is to bring natural templates for web applications which integrate well with Spring Framework and HTML5 java web applications. For using thymeleaf the following code must be added in the pom.xml file.

org.springframework.boot
spring-boot-starter-thymeleaf

10. What is the need for Spring Boot Dev Tools?

Spring Boot Dev Tools are the set of tools which make the application development easier. When the application runs in the production than this module is disabled automatically, repacking of archives are also excluded by default. The Spring Bot developer tools apply the properties to the specific development environments. Add the following dependency into the pom.xml file for including DevTools.



    org.springframework.boot

    spring-boot-devtools

11. What is the Spring Boot Actuator?

Spring Boot provides the actuator to monitor and manage our application. Actuator contains the HTTP endpoints. When the application is pushed to production then by using HTTP endpoints you can choose to manage and monitor your application.

12. How do you create a custom endpoint in Spring Boot Actuator?

In Spring Boot 2.x, the custom endpoint is created using “@Endpoint” annotation. The Spring Boot also exposes the endpoints using @WebEndpointor, @WebEndpointExtension annotations over HTTP with the help of Spring MVC, Jersey, etc.

13. Can you change the port of the embedded Tomcat server in Spring Boot?

 Yes, the port of the embedded Tomcat server can be changed by using the application properties file. You have to add the property “server.port” in the application properties file and assign it to any port which you want. After assigning the port number, the Spring Boot automatically loads the application properties file and the required configurations will be applied on the application.

14. Explain how to deploy a different server with a Spring Boot?

 Apply the below steps to deploy a different server with Spring Boot.

  1. Generate a WAR file from the project.
  2. Then, deploy the WAR file onto your favourite server.

The steps of deploying the WAR file onto a server is dependant on the server which you choose.

15. What are the steps to connect an external database like MySQL or Oracle?

 Apply the below steps for connecting an external database.

  1. Start adding a dependency for MySQL Connector to pom.xml.
  2. Then, in the pom.xml file remove the H2 dependency.
  3. Now set up your MYSQL and configure your connection to MySQL database.
  4. Restart your project.

16. What is the difference between RequestMapping and GetMapping?

The @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET). The @RequestMapping and @GetMapping annotations both of them will support the consume options which are.
consumes = “text/plain”
consumes = {“text/plain”, “application/*”}

17. What are the steps to add a custom JS code with Spring Boot?

The following are the steps for adding a custom JS code using Spring Boot.

  1. In the resources folder, create a folder and name it as static.
  2. Now keep all the static content in this folder.

If in case a browser throws an unauthorised error, then disable the security or search for a password in the log file and eventually it will be passed in the request header.

18. What are the differences between @SpringBootApplication and @EnableAutoConfiguration annotation?

@SpringBootApplication:

  • The @SpringBootApplication annotation is used in the main class or bootstrap class.
  • It is a combination of @Configuration, @ComponentScan and @EnableAutoConfiguration annotations.

@EnableAutoConfiguration:

  • The @EnableAutoConfiguration annotation is used to enable auto-configuration and component scanning in your project.
  • It is a combination of @Configuration and @ComponentScan annotations

19. Can you create a non-web application in SpringBoot?

A non-web application can be created by removing the web dependencies from the classpath along with changing the way Spring Boot creates the application context.

20. How is Hibernate chosen as the default implementation for JPA without any configuration?

By using the Spring Boot Auto Configuration the spring-boot-starter-data-jpa dependency is automatically added to pom.xml file. As this dependency has a transitive dependency on JPA and Hibernate, whenever spring boot sees Hibernate in the classpath, it auto-configures Hibernate as the default implementation for JPA.

Spring Boot Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

 

21. What do you think of using jetty instead of tomcat in spring-boot-starter-web?

You can use jetty instead of tomcat in spring-boot-starter-web, by removing the existing dependency and including the following:


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-tomcat




org.springframework.boot
spring-boot-starter-jetty

22. Mention the dependencies needed to start up a JPA Application and connect to in-memory database H2 with Spring Boot?

The following are the dependencies required to start up a JPA Application and connecting in-memory database H2 with Spring Boot.

  • web starter
  • h2
  • data JPA starter

You must refer to the following code for including the dependencies.


org.springframework.boot
spring-boot-starter-web


com.h2database
h2
runtime


org.springframework.boot
spring-boot-starter-data-jpa

23. Why is Spring Data REST not recommended in real-world applications?

As the database entities are exposed directly as REST Services, it is not recommended in real-world applications. The domain model and the consumers are the two main things which are considered while designing the RESTful services. But, none of these parameters are considered while using Spring Data REST. The entities will be exposed directly. The Spring Data REST can be used for the initial evolution of the project.

24. How to instruct an auto-configuration to back off when a bean exists?

The @ConditionalOnMissingBean annotation is used for instructing an auto-configuration class to back off when a bean exists. The annotation attributes are.

value: It stores the type of beans to be checked.

name: It stores the name of beans to be checked.

25. What is the difference between WAR and embedded containers?

WAR is benefited for a considerable measure from Spring Boot while the embedded containers use only one component of Spring Boot for utilization during the improvements.

Explore Spring Boot Sample Resumes Download & Edit, Get Noticed by Top Employers!

26. What are the steps to deploy Spring Boot web applications as JAR and WAR files?

1.Add the following plugin in the pom.xml for deploying the Spring Boot application.

org.springframework.boot
spring-boot-maven-plugin

2.With the use of the above plugin, execute its package which generates a JAR file. This JAR contains all the required libraries and dependencies. This also contains an embedded server which can run the application as an ordinary JAR file.

To build a jar file, the packaging element in the pom.xml file must be set to jar as below.
jar
To build a war file, you must mention
war

27. What is the best way to expose the custom application configuration with Spring Boot?

By using the “@value” annotation you are able to expose the custom application configuration in Spring Boot but the configuration values are distributed through the application. If this is one way than another way is to use a centralized approach instead of using @value annotation.
With a centralized approach, the configuration component is defined using the “@ConfigurationProperties” as follows:



@Component
@ConfigurationProperties("example")
public class SampleConfiguration {
private int number;
private boolean value;
private String message;

With the above snippet, the configured values of application properties will be as follows.
example.number: 100
example.value: true
example.message: Dynamic Message

28. What is the difference between JPA and Hibernate?

JPA is a Data Access Abstraction used in reducing the amount of boilerplate code while Hibernate is an implementation of the Java Persistence API that offers the loose coupling benefits.

29. What are the @RequestMapping and @RestController annotation in Spring Boot used for?

@RequestMapping:

  • This annotation provides the routing information and tells to Spring that any HTTP request must be mapped to the respective method
  • The “@RequestMapping” annotation is used by importing.
    org.springframework.web.
    bind.annotation.RequestMapping;

@RestController:

  • This annotation adds the “@ResponseBody” and “@Controller” annotation to the class.
  • The “@RestController” annotation is used by importing.
    org.springframework.web.
    bind.annotation.RestController;

Example: Consider there is a method example() which should map with /example URL.

package com.hkrtrainings;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@RequestMapping("/example")
public String example(){
return"Welcome To HKRTrainings";

30. Mention the advantages of the YAML file than Properties file and the different ways to load YAML file in Spring boot.

  • The advantage of the YAML file than Properties file is that the data is stored in a hierarchical format which makes it easy for developers to debug the issues. Whenever you use the SnakeYAML library on your classpath, the YAML file is supported as an alternative to properties by the SpringApplication class.
  • The different ways to load a YAML file in Spring Boot is as follows:
    For loading YAML as a Map, the “YamlMapFactoryBean” is used.
    For loading YAML as Properties, the  “YamlPropertiesFactoryBean” is used.

31. What is meant by Spring Boot?

Ans. Spring Boot is a Java-based open-source spring module useful for Rapid Application Development (RAD). It helps to develop many stand-alone spring-based apps which we can run with a little configuration. Also, it simplifies the usage of the Spring framework for the development of Java.

 Want to know more about Spring Boot ,visit here Spring Boot Tutorial !

Subscribe to our youtube channel to get new updates..!

 

32. Define Spring Boot Annotations?

Ans. The Spring Boot Annotations represent metadata that offers data regarding a program. It provides additional data about a program that doesn’t include the part of an application we develop. Moreover, it doesn’t directly impact the operation of the code they explain.

33. What do you mean by Spring Initializr?

Ans. In Spring Boot, Spring Initailizr is a web tool provided by Spring that helps produce the Spring Boot project structure. It supports us while we start a new project from scratch by resolving issues of framework setting. Thus, there is no need to set up the project structure manually.

34. Define Spring Boot CLI?

Ans. Spring Boot CLI refers to the command-line interface which Spring Boot Framework supports. It enables you to build Spring-based Java apps and also helps to execute and test Spring Boot apps using the command prompt.

35. Explain Spring Boot Properties?

Ans. Spring Boot offers multiple properties where the file application.properties helps write the application-based property into that file. Moreover, these property files hold various configurations that require the app to run in a distinct environment. The significant Spring Boot application properties' types include command line properties, YAML files, properties files, and externalized properties.

36. Define Spring Data in Spring Boot?

Ans. Spring Data makes it easier for developers to use different types of databases like relational and non-relational, cloud data services, etc. Thus, it makes data access more accessible and still retains the underlying data.

37. What is meant by Dependency Injection?

Ans. In Spring Boot, dependency injection is a design pattern that Spring container uses to insert objects to other dependencies. Also, it helps to eliminate the dependency on the various programs.

38. Why use Spring Boot rather than Spring?

Ans. Spring Boot has vital features that Spring doesn’t have. Key features like auto-configuration save much time for developers from drafting long-form codes. It also makes the application development process much more manageable. Further, some other vital features that Spring Boot offers include Starter POM, Actuators, Scanning Components, Version Management, InMemory database, etc.

39. What is Relaxed Binding in Spring Boot, and how does it support it?

Ans. Spring Boot uses some relaxed binding rules for binding various Environment Properties to map with the bean property name. It maps with the property name even if there is no exact match. Relaxed binding is highly relevant for the type-safe binding of configuration properties within Spring Boot.

40. Define IOC Container?

Ans. IOC container is a robust Spring framework useful for applying automatic dependency injection. It helps in managing the creation of objects and their lifetime. Also, it helps to insert dependencies into the class. The IoC container inserts the dependency objects using a constructor, a method, or a property at the runtime and ejects the same at a relevant time.

41. How database connection information automatically connects to H2 in Spring Boot?

Ans. The auto-configuration property in Spring Boot specifies the database connection information and connects the database to H2 automatically. It does so because Spring Boot auto-configuration configures all the application dependencies.

42. What is the default H2 database that Spring Boot configures?

Ans. The default H2 database that Spring Boot configures is called “testdb.”

Related Article : Java Spring Vs Node Js

43. Why are Profiles used in Spring Boot?

Ans. Application development has to deal with multiple environments like Dev, QA, Production, etc. Every environment needs a different configuration. So, Profiles help here to separate the various portions of application configuration. Also, they make it handy for different environments. Thus, Spring Boot offers some features using which we can mention the active profiles for a particular environment. Also, we can specify the configuration of multiple environments for different profiles.

44. How can we disable the default web server within the Spring Boot application?

Ans. Spring Boot mostly prefers to use the tomcat server, but we can prevent Spring Boot from starting a server automatically. For this, we can avoid including the web server starter within our dependencies.

45. How to validate an Actuator within the Spring Boot application?

Ans. To validate or authorize an Actuator in the Spring Boot app, we have to insert the spring-boot-starter-actuator dependency within pom.xml.

46. Define the way to check the environment properties within the Spring Boot Application?

Ans. To check all the environment properties in the Spring Boot application, we need to enable the Actuator endpoint “/env.” It returns all the properties that run the Spring Boot app.

Spring Boot Training

Weekday / Weekend Batches

 

47. What are the sources of External Configuration in Spring Boot?

Ans. Spring Boot enables developers to execute similar applications in multiple environments. Here, the external configuration supports them to do so. The below sources are the possible ones for external config in Spring Boot.-

  • Command-line Properties
  • Application Properties
  • Profile-specific Property files

48. Define the process of custom auto-configuration registration in Spring Boot?

Ans. To register a custom auto-configuration class in Spring Boot, we must state the annotation @EnableAutoConfiguration with the key META-INF/Spring.factory file. You can also opt for the annotation @SpringBootApplication to one of the configuration classes.

49. Name the most useful Spring Boot CLI commands?

Ans. The following are the most commonly used Spring Boot CLI Commands.:-

-run, -jar, -install, --init, -shell, -help, -test, -grap, -uninstall, -war, etc.

Related Article : Spring Data ElasticSearch

50. Name the layer at which the Transaction boundary begins in Spring Boot?

Ans. The boundary or border of the Transaction begins from the Service Layer as it defines the transaction boundaries. This layer also holds the logic of business transactions.

51. Define the way of configuring Logging using Log4j?

Ans. For logging a configuration in Spring Boot, we can use Log4j2 as Spring Boot supports it. Thus, we need to include “Log4j2” for logging a configuration.

Find our upcoming Spring Boot Training Online Classes

  • Batch starts on 5th Feb 2023, Weekend batch

  • Batch starts on 9th Feb 2023, Weekday batch

  • Batch starts on 13th Feb 2023, Weekday batch

 
Global Promotional Image
 

Categories

Request for more information

Manikanth
Manikanth
Research Analyst
As a Senior Writer for HKR Trainings, Sai Manikanth has a great understanding of today’s data-driven environment, which includes key aspects such as Business Intelligence and data management. He manages the task of creating great content in the areas of Digital Marketing, Content Management, Project Management & Methodologies, Product Lifecycle Management Tools. Connect with him on LinkedIn and Twitter.