This content originally appeared on DEV Community and was authored by hrishikesh1990
Hello, fellow developers, I have compiled a list of essential Spring Boot interview questions that I felt every developer should know.
Do drop your thoughts in the comments section below. Also, feel free to comment in case you find any content to be incorrect.
1. What are Spring Boot starters, and what are the different starters available?
Spring Boot starters were built to address the various dependency management issues that arise while working on a complex project.
They are a set of convenient dependency descriptors that you could easily include in your application. They automatically manage dependencies, allowing you to focus on other important parts of your projects.
There are numerous starters available that can help you get your project running. All starters in the Spring Boot framework follow a similar naming scheme: spring-boot-starter-*, where * refers to a particular application.
The following are a few commonly used Spring Boot starters, however, you can find the entire list of starters here.
- The Web Starter (spring-boot-starter-web)
- The Test Starter (spring-boot-starter-test)
- The Data JPA Starter (spring-boot-starter-data-jpa)
- The Mail Starter (spring-boot-starter-mail)
2. What do you mean by Auto-Configuration in Spring Boot?
Auto-configuration in Spring Boot automatically attempts to configure your Spring application based on your jar dependencies.
The most commonly used example is the HSQLDB classpath. Let’s say HSQLDB is on your classpath but you are yet to configure any database connection beans, then Spring Boot attempts to auto-configure an in-memory database.
3. What is CLI in Spring Boot? Also, mention some of its features?
The Spring Boot CLI is a command-line tool that allows you to quickly start developing a Spring application. This CLI allows you to run Groovy scripts. These scripts have a Java-like syntax with minimal overlap.
Some of the main features that Spring Boot CLI provides are:
- Auto configurations
- Dependency management
- Application servers
- Endpoint management
4. What are the various methods used to create a Spring Boot application?
Spring Boot comes inbuilt with multiple quick and handy methods that allow you to create an application. Listed below are a few methods.
- Spring Initializer
- Boot CLI
- Using Maven
- IDE project wizard
5. What is Spring Boot Initializr? and what are the advantages of using it?
Spring Boot Initializr is a web-based tool that makes bootstrapping Spring Boot or Spring applications easier. Spring Boot Initializr provides a simple interface and is integrated with major Java IDEs.
The major advantages of Spring Boot Initializr are:
- Reduced time in creating a Spring or Spring Boot application
- Extensible API support to quickly generate and start projects
- A configured structure that helps define all the aspects related to the projects
6. What are the steps required to override default properties in Spring Boot?
Spring Boot applications have their configuration externalized through the application.properties file. Although these properties work as default values, they can be overridden.
The steps to do this are as follows:
- Start by creating an application.properties file in the classpath to override specific properties. For Spring Boot, the file to be overridden in the classpath is application.yml.
- For Maven projects, the file will be under /src/main/resource
- Change the Server HTTP port, the default port would be 8080. Add the following code to the application.properties file to change the port to 9090
server.port=9090
7. What are the various embedded containers that Spring Boot supports?
Spring Boot supports the following embedded containers
- Tomcat
- Undertow
- Jetty
8. Why are Actuators used in Spring Boot?
Spring Boot Actuators allow developers to include production-ready features into their applications. Actuators are mainly used to display operational information about the application.
Some examples of this type of information are the health metrics, info, dump, etc and this data can be used to monitor our application, gather metrics, understand traffic, etc.
The following code can be used to enable a Spring Boot Actuator.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
9. How are custom endpoints created in Spring Boot?
In order to create a custom endpoint in Spring, the user must expose the instance of the custom endpoint class as a bean.
In the code below, we have implemented the Endpoint interface.
@Component
public class CustomEndpoint implements Endpoint {
//method implementation
}
10. How is security implemented for Spring boot applications?
While using Spring Boot, security can be easily implemented by using the Spring Boot security starter (spring-boot-starter-security).
This code is used to add the security starter to your application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
11. How are databases configured using Spring Boot?
Spring Boot provides comprehensive support while working with databases. SQL databases can be easily configured.
In order to configure your database and your application, you could either use the spring-boot-starter-jbdc or spring-boot-starter-data-jpa starter. Furthermore, to configure the data source, the application.properties file can be used.
The following code can be used to configure a MySQL database and is the answer to the aforementioned Spring Boot interview question:
spring.datasource.url=jdbc:mysql://localhost/flexiple
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
12. Why is spring-boot-maven-plugin used?
This Maven plugin provides Spring Boot support to Apache Maven. This allows users to create and package an executable jar or war archives. It also allows you to start your application before running integration tests.
To add the plugin to your project, add this XML code in the plugin section in your pom.xml as shown below.
The following code can be used to configure a MySQL database and is the answer to the aforementioned Spring Boot interview question:
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>getting-started</artifactId>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
13. What is YAML? Why is it used?
Spring boot YAML can also be used to define properties and is an alternative to the application.properties file. Whenever the SnakeYAML library is in your classpath, the SpringApplication class automatically supports YAML as an alternative to properties.
14. How do you set an active profile in Spring Boot?
Active profiles can be set using the following methods:
Passing the active profile as an argument during launch
Setting the active profile using the application.property file
Code to set a profile as active in Spring Boot:
java -jar -Dspring.profiles.active=production application-1.0.0-RELEASE.jar
spring.profiles.active=production
15. How are web server configurations disabled in a Spring Boot application?
Applications in Spring boot automatically start in the webserver mode if the web module is present in the classpath. Subsequently setting the webApplciationType to none in the application.properties file will disable it.
Web server disabling code for the aforementioned Spring Boot interview question:
spring.main.web-application-type=none
16. How is ApplicationContext created by Spring Boot?
Once the SpringApplication.run() is executed, Spring Boot creates the ApplicationContext. Spring Boot then returns the ConfigurableApplicationContext which extends the ApplicationContext.
ConfigurableApplicationContext code for the aforementioned Spring Boot interview question:
public ConfigurableAppContext run(String...args) {
//preparing the AppContext
ConfigurableAppContext context = null;
//To create and return the app context
context = createAppContext();
}
protected ConfigurableAppContext createAppContext() {
Class << ? > contextClass = this.appContextClass;
if (contextClass == null) {
try {
switch (this.webAppType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
} catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Could not create a default AppContext, " +
"specify an AppContextClass",
ex);
}
}
return (ConfigurableAppContext) BeanUtils.instantiateClass(contextClass);
}
17. How are HTTP Response Compressions enabled in Spring Boot?
Insert this code in the application.properties file
server.compression.enabled=true
Then use the server.compression.min-response-size to set compression length.
18. Write a code snippet to implement bubble sort?
Spring Boot binds environment properties to beans using relaxed binding. What this essentially means is that Spring Boot does not force an exact match between the environment property and the bean names. These names could be written in different cases or separated by a dash but Spring Boot would still bind the following values.
The below code snippet is the answer to the above-mentioned Spring Boot interview question.
@ConfigurationProperties(prefix="flexiple.demoapplication-project.person")
public class CustomerProperties {
private String Name;
public String getName() {
return this.Name;
}
public void setName(String Name) {
this.Name = Name;
}
}
19. What are the uses of the application.properties file in Spring Boot?
The application.properties file is a single file that contains all the properties of your application allowing it to run in different environments. In essence, this file is the control system for your Spring Boot application.
20. What are dependency injections in Spring Boot?
Injecting dependent beans into target bean objects is called dependency injection.
The three types of Dependency Injections are:
- Setter Injection
- Constructor Injection
- Field Injection
This content originally appeared on DEV Community and was authored by hrishikesh1990
hrishikesh1990 | Sciencx (2021-09-14T12:08:17+00:00) 20 Essential Spring Boot Interview Questions. Retrieved from https://www.scien.cx/2021/09/14/20-essential-spring-boot-interview-questions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.