Wed. Apr 24th, 2024

Top 30 Spring Boot interview questions: A Cheat Sheet to Ace an interview

spring boot interview questions

If you are looking for Spring Boot interview questions, your wait is over here. Spring Boot is a convention over configuration solution that helps to create stand-alone. However, it is rapidly coming with new technologies to make the building applications process easier. In fact, around 822 companies, including CRED, Udemy and Deleokorea, are using Spring Boot in their tech stack nowadays.

That means Spring Boot has become one of the most used technologies with Java in creating web applications. If you’re looking for some Spring Boot questions with proper explanation to ace the interview, this article could be your avenue. Here, we’re covering the 30 most vital Spring Boot interview questions collected from experts that will for sure help you to ace the interview.

Let’s get started.

Table of Contents

Why is there a need to use Spring Boot?

The main reason for Spring Boot’s popularity is that it has a link with Java. And as we all know, Java is one of the most popular programming languages in the world.

And that is also the reason why most of the tech companies are using Spring Boot in their tech stack nowadays.

Moreover, it is rapidly coming with new ways and technologies to make building applications more efficient.

Moreover, you do not have to worry about safety and correct configuration while building any applications with Spring Boot.

Although Spring Boot is a vast community, that means there are many free courses and materials available for you on the internet for learning.

That is why we can say that it is known as a multi-threaded technology. Let’s take a look at some more benefits of Spring Boot.

  • Spring Boot offers easy access to the command-line interface.
  • It helps to minimize the time spent on building any application.
  • Spring Boot provides flexibility in arranging Java Beans and XML configurations.
  • Spring Boot offers a default setup for integration tests that will ease the creation and testing of Java-based applications.
  • It helps to prevent all the manual work like writing boilerplate code and annotation.
  • Spring Boot offers admin support. That means you can operate and manage applications through your smartphones.
  • For testing web applications, it comes with embedded HTTP servers like Tomcat and Jetty.
  • Spring Boot will give you an opportunity to have access to the database and ques services like Oracle, Solr, Rabbit MQ, ActiveMQ, MySQL, Redis and many more.
  • Spring Boot integrated with the Spring that arises a Spring Boot ecosystem. And this Spring Boot ecosystem includes Spring ORM, Spring JDBC, Spring Data and Spring Security.
  • It offers various plug-ins that will ease your working with embedded and in-memory databases.

Top 30 Spring Boot interview questions with examples and complete explanation

In this section, we will discuss some more vital Spring Boot questions with complete explanations and examples.

We’re sure that these questions will keep you high in this industry and help you, expert, the interview.

Q1. What do you mean by Spring Boot, and why do you need it?

It is the first and foremost vital question you should grasp to ace the interview.

We mentioned earlier that Spring Boot is an open-source framework that helps to build stand-alone.

However, it is also known for using the Spring framework for Java development.

After gathering all the uses of Spring Boot, we can say that it helps to remove a lot of configurations and dependencies.

One big plus is that it comes with an auto-dependency resolution, embedded HTTP servers and default setup for integration tests helping in rapid application development and testing.

Next, you asked why there is a need to use Spring Boot.

In answer, I would like to say that Spring Boot offers conveniences like admin support, the default setup for integrations for tests that will ease the development and testing of Java products.

However, the best thing is that it minimizes the time spent on building any application.

Therefore, these are reasons why there is a need to use Spring Boot.

Q2. Discuss the benefits of Spring Boot?- Spring Boot interview questions

Below, I would walk through some benefits of Spring Boot.

  • It provides easy access to the command-line interface.
  • Spring Boot minimizes the time spent on developing any application.
  • It allows easy access to database and queue services like MySQL, Oracle, Solr, ActiveMQ, RabbitMQ and many more.
  • Spring Boot offers flexibility in managing Java Beans and XML configurations.
  • It comes with admin support. That means you can operate any application through your mobile device.
  • Spring Boot comes with an embedded servlet container.
  • It offers a default setup for integration tests that facilitates the development and testing of Java-based applications.
  •  It provides plug-ins that will ease your working with embedded and in-memory databases.
  • For testing web applications, it comes with an embedded HTTP server like Tomcat and Jetty.
  • It will maximize the efficiency of the developing team.
  • One big plus is that it reduces all the manual work like writing boilerplate code and annotation.
  • Spring Boot comes with a wide range of APIs that will help in monitoring and managing applications.

Q3. What are some features of Spring Boot?

Below, I will discuss some vital features of Spring Boot.

1. Security and logging

This feature will ensure that all the applications made by Spring Boot are secured.

2. Spring Initializer

It is an application that will reduce your manual work of setting up the structure of the project.

3. Spring Boot CLI

Spring CLI will reduce all your manual work writing boilerplate code and annotation.

4. Spring Boot Actuator

It is a sub-project of the Spring Boot framework that will help you monitor and manage the Spring Boot applications.

5. Starter Dependency

Starter Dependency will help in removing a lot of configurations and dependencies. As a result, it will improve your productivity.

6. Auto-configurations

If you are working on a project, this feature will help you in loading the default configurations. Although it will prevent any unnecessary WAR files interrupting your project.

Q4. Mention the difference between Spring Boot and Spring?

Spring is a Java-based web application framework. On the other hand, Spring Boot is a module of Spring that helps in creating stand-alone.

However, the Spring framework is more complex than Spring Boot. Moreover, the spring framework is helpful in creating customized web applications by providing tools and libraries.

Q5. How can we create a Spring Boot application with the help of Maven?

There are many ways like the Spring Maven project, Spring Boot CLI, Spring Initializer and Spring starter project wizard to create a Spring Boot application using Maven.

Q6.Mention some sources of external configuration?

There is no doubt that you can run the same application in different environments with the help of Spring Boot.

However, it is possible because it provides you with support for external configuration.

For mentioning the configuration properties, Spring Boot uses property files, command-line arguments, YAML files, environment variables and system properties.

1.Profile-Specific properties

These properties loaded from the YAML files in a similar location.

2.Application properties

For loading the properties, Spring Boot searches for the application properties like YAML files.

3.Command-line properties

Spring Boot comes with command-line properties that will help you to convert arguments into the properties.

Q7. What do you mean by the Spring Boot starters?

Spring Boot starters are a collection of convenience dependencies that can help in reducing all the configurations and dependencies.

That means removing all the dependencies and configurations will make development efficient.

However, some popular Spring Boot starters are Spring-boot-starter-test, spring-boot-starter-security, spring-boot-starter-jdbc, spring-boot-starter-web, spring-boot-starter-aop and spring-boot-starter-data-jpa.

Q8. Discuss what happens when a Spring Boot application is ‘Run as Java Application’?

When a Spring Boot application is ‘Run as Java application,’ it will automatically launch an HTTP server Tomcat.

Q9. Can you use Jetty instead of Tomcat in Spring-Boot-Starter-Web?

Yes, it can be possible due to removing the existing dependency and adding:-

<dependency>

        <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-web</artifactId>

       <exclusions>

            <exclusion>

                 <groupId>org.springframework.boot</groupId>

              <artifactId>spring-boot-starter-tomcat</artifactId>

             </exclusion>

      </exclusions>

       </dependency>

        <dependency>

             <groupId>org.springframework.boot</groupId>

             <artifactId>spring-boot-starter-jetty</artifactId>

  </dependency>

Q10. What do you mean by Spring Actuator and mention its advantages?

Spring Actuator is one of the features of Spring Boot that will help you see what is actually happening inside the running application.

Understanding your application will let you debug your application and analyze the logs.

However, one big plus is that it will give you access to many features like CPU usages and identifying beans.

Better yet, it will also give you access to the production-ready REST points.

Q11. What are the steps for connecting the Spring Boot application to a database using JDBC?

You can connect the Spring Boot application to a database using JDBC with the help of Spring Boot Starter projects.

Yeah, it is possible by creating an application and connecting it with MYSQL.

If you want to know how to do so, you can follow the steps given below.

Step1:-  Firstly, you need to create a database in MYSQL.

  CREATE DATABASE example;

Step2:-  Now, you need to create a table.

CREATE TABLE customers(customerid INT PRIMARY KEY NOT NULL AUTO_INCREMENT, customername VARCHAR(255)); 

Step3:-  You have to create a Spring Boot project and provide the details.

Step4:-  You need to include MySQL, dependencies and JDBC.

Step5:-  Arrange the database into application properties after creating the spring boot project.

spring.datasource.url=jdbc:mysql://localhost:3306/example spring.datasource.username=root spring.datasource.password=edureka 

spring.jpa.hibernate.ddl-auto=create-drop

Step6:-  Now, your application Java class must have codes:-

package com.edureka; 

 import org.springframework.boot.SpringApplication; 

 import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication 

 public class SampleApplication { 

         public static void main(String[] args) { 

                SpringApplication.run(SampleApplication.class, args); 

         } 

  } 

Step7:-  Create a controller to handle the HTTP requests by adding codes:-

package com.edureka;

 import org.springframework.web.bind.annotation.RequestMapping;

 import org.springframework.web.bind.annotation.RestController;

 @RestController

 public class JdbcController {

 @Autowired

 JdbcTemplate jdbc;

 @RequestMapping(“/insert”)

 public String index(){

 jdbc.execute(“insert into customers(name)values(‘Aryya’)”);

 return “Data Entry Successful”;

 }

 }

Step8:-  You have to perform this project as a Java application.

Step9:-  Open the URL (localhost:8080/insert).

Therefore, after opening the URL, output as Data Entry Successful will appear on your screen. Go forward to make sure that the data written in the table.

Q12. Is this change the port of the embedded Tomcat server in Spring Boot?

Yes, it can be possible by using the application properties files. The only thing you have to do is add a property of server.port and assign it to any port.

Let me explain with you an example.

Suppose you want to assign it to the 8081. You only have to mention server.port=8081.

Therefore, the application file will automatically loaded by the Spring Boot after mentioning the port number.

Q13. How can you disable a specific auto-configuration?

We can use the exclude attribute of the @EnableAutoConfiguration annotation to disable a specific auto-configuration.

For example, you can neutralize DataSourceAutoConfiguration with this code:-

// other annotations 

@EnableAutoConfiguration(exclude =DataSourceAutoConfiguration.class) public class MyConfiguration { } 

If we allow auto-configuration using @SpringBootApplication annotation having meta-annotation @EnableAutoConfiguration, you can disable it using an attribute:-

// other annotations 

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class MyConfiguration { }

Using Spring.autoconfigure.exclude environment property is another way to disable auto-configuration.

Q14. Can we register a custom auto-configuration?

Yes, it can be possible if we have a fully qualified name listed under the EnableAutoConfiguration key in the META-INF/spring.factories file:-

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.baeldung.autoconfigure.CustomAutoConfiguration

Q15. Can we tell an auto-configuration to back away if a bean exists?

Yes, it can be possible to tell an auto-configuration with the help of annotation @ConditionalOnMissingBean.

However, the attributes of these annotations are:-

  • name: The names of beans.
  • value: The types of beans.

After placing on a method with @Bean, we can target the type defaults to:-

@Configuration 

public class CustomConfiguration { 

      @Bean 

      @ConditionalOnMissingBean 

       public CustomService service() { … } 

}

Q16. What are the steps for creating a Spring Boot project using Spring Initializer?

Spring Initializer is one of the best ways to create a Spring Boot project. You only have to provide your project details.

We can follow these steps to create a Spring Boot project with the help of Spring Initializer.

Step1:-  Firstly, we need to select the required dependencies and the Maven project.

Moreover, we have to fill up some other details such as Artifact and Group.

Step2:-  Now, click on the Generate project.

Step3:-  After generating the project, extract it from your system.

Step4:-  Select the import option on the Spring tool suite IDE to import this project.

Note:-  Make sure that your chosen project is the type of Maven and source project containing the pom.xml file while importing your project.

Therefore, after following all these steps, the Spring Boot will get created with all necessary dependencies.

Q17. Can we enable HTTP/2 support in Spring Boot?

Yes, we can enable HTTP/2 support in Spring Boot using server.http2.enabled=true.

Q18. What do you mean by Spring Boot CLI, and can we execute the Spring Boot project using CLI?

Spring Boot CLI is a command-line tool to develop a Spring application.

Next, you asked if we can execute the Spring Boot project using CLI.

Yes, we can execute the Spring Boot project using CLI by following these simple steps.

Step1:-  Firstly, we need to download the CLI tool and extract the zip file.

We have to use the bin folder present in the Spring setup to execute the Spring Boot application.

Step2:-  Now, create a groovy file for the Spring Boot application by opening the current directory to the bin folder.

Step3:-  Then we have to open that groovy file and create a controller like this:-

@RestController public class Sample {   

 @RequestMapping(“/example”)   

 String index(){   

<h1>”Welcome To Edureka”</h1>;  

} }

Step4:-  Now, we have to complete the groovy file by adding:- ./spring run Sample.groovy;

After completing your project, we have to open the URL (localhost:8080:/example). 

Q19. Mention an example for ReadOnly as TRUE in transaction management?

Here is an example:-

Suppose you have a customer base and want to read the customer details such as customer name and customer ID.

If you want to do that, you can not check for any changes in the entities, and you have to set the red-only on the transaction.

Q20. Is this possible to expose custom application configuration using Spring Boot?

Yes, using @Value annotation is one of the best ways to do so. But it will cause a problem.

So, we have to choose another path.

We can take the help of Configuration Properties with a snippet code:-

@Component

@ConfigurationProperties(“example”)

public class SampleConfiguration {

private int number;

private boolean value; 

However, the value configured in application properties according to the above snippet will be:-

example.number: 100

example.value: true

example.message: Dynamic Message

Q21. Discuss Spring Data REST?

Displaying the restful sources around Spring Data repositories is known as the Spring Data REST.

For example:-

@RepositoryRestResource(collectionResourceRel = “sample”, path = “sample”)

public interface SampleRepository

        extends CustomerRepository<sample, Long> {

Now, we can use the POST method to expose the REST services.

{

“customername”: “Rohit”

}

Then the response content will be like this:-

{

“customername”: “Rohit”

“_links”: {

“self”: {

“href”: “http://localhost:8080/sample/1”

},

“sample”: {

“href”: “http://localhost:8080/sample/1”

}

}

At this point, we can see that the response content is containing the href of the newly created resource.

Q22. Discuss the working of the path=”sample”, Collection Resource Rel=”sample” using Spring Data REST?

Generating links to the collection resource is done by a value CollectionResourceRel.

On the other hand, the path is the section that helps mention the segments under the resource to get exported.

Q23. Mention the error that arises when H2 is not present in the classpath?  – Spring Boot interview questions

We will see the following error:-

Cannot determine embedded database driver class 

Now, you can add H2 to the pom.xml file to resolve this error.

Q24. How is this possible to use profiles to configure the environment-specific configuration with Spring Boot?

Yes, it can be possible to use profiles to configure the environment-specific configuration with Spring Boot.

Let me explain to you with an example. Consider two profiles, prod and dev.

Now, in the application properties file, consider the following properties:-

example.number: 100

example.value: true

example.message: Dynamic Message

We need to create a file with the name application-dev.properties.

For example, you can mention:-

example.message: Dynamic Message in Dev

Similarly, we can also customize the application properties file for the prod file by using the following code snippet:-

example.message: Dynamic Message in Prod

Therefore, after completing the specific profile configuration, we need to set the active profile in an environment.

However, to set the active profile in an environment, we can follow these ways:-

We have to add Spring.profiles.active=prod in the application properties file and -Dspring.profiles.active=prod in arguments to set the active profile in an environment.

Q25. What are the dependencies needed to start up a JPA application with Spring Boot?

The dependencies like h2, web starter and data JPA starter are helpful to start up a JPA application and connect to the in-memory database H2 with Spring Boot.

We need to refer to the following codes to include dependencies:-

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

    <groupId>com.h2database</groupId>

    <artifactId>h2</artifactId>

    <scope>runtime</scope>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

Q26. Discuss the steps to add a custom JS code with Spring Boot?

 

Here are the steps you can follow to add a custom JS code with Spring Boot.

Step1:-  Firstly, we have to create an account.

Step2:-  Under the resources folder, name the folder static.

Step3:-  Put all the static content in this folder.

If the browser shows an unauthorized error, we can disable the security or search for the password in the log file.

Q27. Mention the steps to deploy Spring Boot web applications as WAR and JAR files?

We can add a plug-in shown below in the pom.xml file to deploy the Spring Boot web applications.

<plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

We have to get a JAR file containing all the necessary dependencies and libraries to use the above plug-in.

The packaging element in the pom.xml file set to the jar will be like this:-

<packaging>jar</packaging>

Similarly, with war:-

<packaging>war</packaging>

Q28. How is this possible to create a custom endpoint in Spring Boot Actuator?

Yes, it can be possible to create an endpoint with the help of @Endpoint annotation. We can also expose endpoints with the help of @WebEndpointExtension, @WebEndpointor over the HTTP server.

Q29. How can we deploy to a different server with Spring Boot?

We can follow the steps to deploy to a different server with the help of Spring Boot.

  1. Firstly, you have to generate a WAR from the project.
  2. Now, you can deploy the WAR file onto your server.

Q30. Is it possible to create a non-web application in Spring Boot?

Yes, it can be possible by removing the web dependencies from the classpath. You also have to change the way Spring Boot creates the application context.

Wrapping up

It seems that Spring Boot is rapidly growing day by day. However, Spring Boot has made the process of developing web-applications more efficiently. Therefore, if you are trying to ace an interview, we are sure that the Spring Boot interview questions shown in this article will help you.

By Sambit

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *