Spring Boot Interview Questions

Get Technical Answers for Spring Boot Interview Questions by Spring Boot Developer experts, Advanced Technical Questions for Freshers and Experience Persons. Get Free practical tests.

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.

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.

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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

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.

<dependency>

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

    <artifactId>spring-boot-devtools</artifactId>

</dependency>

Spring Boot Training

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

 

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.

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

 

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.

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:

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

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.

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

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.

Spring Boot Training

Weekday / Weekend Batches

 

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.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</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.
<packaging>jar</packaging>
To build a war file, you must mention
<packaging>war</packaging>

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.

Submit an interview question

Categories

Request for more information

Sachin
Sachin
Networking, OS, Salesforce & Sap
Am sachin I have worked with hkr trainings for more than 4 years .and our methodologies had been a practical suit for student culture and professionals. And I have trained more than 30 batches over the last 8 months. And I have great experience working with hkr trainings. Contact me with my LinkedIn and Twitter.

WhatsApp
To Top