Check out Spring Boot Interview Questions for 2 Years Experience
If you want to learn and master in Spring Boot then check out Beginners to Master Spring Boot Tutorial.
YouTube Video - Spring Boot Interview Questions
1. What is the Spring Boot?
Spring Boot is basically an extension of the Spring framework which eliminated the boilerplate configurations required for setting up a Spring application.Spring Boot is an opinionated framework that helps developers build Spring-based applications quickly and easily. The main goal of Spring Boot is to quickly create Spring-based applications without requiring developers to write the same boilerplate configuration again and again.
Read more about Spring Boot at Getting Started with Spring Boot
2. Explain a few important Spring Boot Key features?
- Spring Boot starters
- Spring Boot autoconfiguration
- Elegant configuration management
- Spring Boot Actuator
- Easy-to-use embedded servlet container support
1. Spring Boot Starters
One more example, when we add the spring-boot-starter-web dependency, it will by default pull all the commonly used libraries while developing Spring MVC applications, such as spring-webmvc, jackson-json, validation-api, and tomcat. Not only does the spring-boot-starter-web add all these libraries but it also configures the commonly registered beans like DispatcherServlet, ResourceHandlers, MessageSource, etc. with sensible defaults.
Read more about starters on Important Spring Boot Starters with Examples
2. Spring Boot Autoconfiguration
If you have any embedded database drivers in the classpath, such as H2 or HSQL, and if you haven’t configured a DataSource bean explicitly, then Spring Boot will automatically register a DataSource bean using in-memory database settings.
You will learn more about autoconfiguration at What is Spring Boot Auto Configuration?
3. Elegant Configuration Management
4. Spring Boot Actuator
Read more about Spring Boot Actuator on Spring Boot Actuator
5. Easy-to-Use Embedded Servlet Container Support
Traditionally, while building web applications, you need to create a WAR file of your spring project and then deploy them on external servers like Tomcat, WildFly, etc. Spring Boot allows you to package your application as a standalone JAR file. It includes an embedded servlet container (Tomcat, Jetty, or Undertow) by default, making it easy to deploy and run applications without requiring an external server.3. What is the Difference Between Spring and Spring Boot
4. Explain Spring vs Spring MVC vs Spring Boot
Learn more here: Spring vs Spring MVC vs Spring Boot5. What is Spring Boot Auto-configuration?
Why do we need Spring Boot Auto Configuration?
The Spring Boot auto-configuration feature tries to automatically configure your Spring application based upon the JAR dependency you have added in the classpath.
Learn more about Spring Boot Auto Configuration at Spring Boot Auto Configuration | Example
6. What are the different ways to create a Spring Boot application?
7. Explain @SpringBootApplication, @Configuration and @ComponentScan annotations
The @SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring below three annotations:@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan@Configuration: The @Configuration annotation indicates that the class contains bean configurations and should be processed by the Spring IoC container. It allows you to define beans and their dependencies using annotations like @Bean.
@EnableAutoConfiguration: The @EnableAutoConfiguration annotation enables Spring Boot's auto-configuration mechanism. It automatically configures the Spring application based on the classpath dependencies, project settings, and environment. Auto-configuration eliminates the need for manual configuration and reduces boilerplate code.
@ComponentScan: The @ComponentScan annotation tells Spring where to look for components (such as controllers, services, and repositories) to be managed by the Spring IoC container. It scans the specified packages and registers the annotated classes as beans.
8. What are Spring boot starters and name a few important Spring boot starter dependencies?
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technology that you need, without having to hunt through sample code and copy-paste loads of dependency descriptors.For example, while developing the REST service or web application; we can use libraries like Spring MVC, Tomcat, and Jackson – a lot of dependencies for a single application. The spring-boot-starter-web starter can help to reduce the number of manually added dependencies just by adding a spring-boot-starter-web dependency.
So instead of manually specifying the dependencies just add one spring-boot-starter-web starter to your spring boot application:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
9. How does Spring Enable Creating Production-Ready Applications in a Quick Time?
- spring-boot-starter-actuator - To use advanced features like monitoring & tracing to your application out of the box
- spring-boot-starter-undertow, spring-boot-starter-jetty, spring-boot-starter-tomcat - To pick your specific choice of Embedded Servlet Container
- spring-boot-starter-logging - For Logging using log back
- spring-boot-starter-cache - Enabling Spring Framework’s caching support
10. What Is the Minimum Baseline Java Version for Spring Boot 3?
Spring Boot 3.0 requires Java 17 or later. It also requires Spring Framework 6.0.11. What are Different Ways of Running Spring Boot Application?
Spring Boot offers several ways of running Spring Boot applications. I would like to suggest five ways we can run the Spring Boot Application- Running from an IDE
- Running as a Packaged Application
- Using the Maven Plugin
- Using External Tomcat
- Using the Gradle Plugin
12. Name all Spring Boot Annotations?
Read more about each Spring Boot annotation at Spring Boot Annotations with Examples13. @SpringBootApplication vs @EnableAutoConfiguration Annotation?
@EnableAutoConfiguration is to enable the automatic configuration feature of the Spring Boot application which automatically configures things if certain classes are present in Classpath.@EnableAutoConfiguration also combines @Configuration and @ComponentScan annotations to enable Java-based configuration and component scanning in your project
On the other hand, @SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Read more: Spring Boot @SpringBootApplication Annotation with Example
14. Why do we need a spring-boot-maven plugin?
- It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
- It searches for the public static void main() method to flag as a runnable class.
- It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
- spring-boot:run runs your Spring Boot application.
- spring-boot:repackage repackages your jar/war to be executable.
- spring-boot:start and spring-boot:stop to manage the lifecycle of your Spring Boot application (i.e. for integration tests).
- spring-boot:build-info generates build information that can be used by the Actuator.
15. What is the Spring Boot Actuator and its Features?
Spring Boot Actuator provides production-ready features for monitoring and managing Spring Boot applications. It offers a set of built-in endpoints and metrics that allow you to gather valuable insights into the health, performance, and management of your application.Here are some key features provided by Spring Boot Actuator:
Health Monitoring: The actuator exposes a /health endpoint that provides information about the health status of your application. It can indicate whether your application is up and running, any potential issues, and detailed health checks for different components, such as the database, cache, and message brokers.
Metrics Collection: The actuator collects various metrics about your application's performance and resource utilization. It exposes endpoints like /metrics and /prometheus to retrieve information about HTTP request counts, memory usage, thread pool statistics, database connection pool usage, and more. These metrics can be integrated with monitoring systems like Prometheus, Graphite, or Micrometer.
Auditing and Tracing: Actuator allows you to track and monitor the activities happening within your application. It provides an /auditevents endpoint to view audit events like login attempts, database changes, or any custom events. Additionally, Actuator integrates with distributed tracing systems like Zipkin or Spring Cloud Sleuth to trace requests as they flow through different components.
Environment Information: The actuator exposes an /info endpoint that displays general information about your application, such as version numbers, build details, and any custom information you want to include. It is useful for providing diagnostic details about your application in runtime environments.
Configuration Management: Actuator provides an /configprops endpoint that lists all the configuration properties used in your application. It helps in understanding the current configuration state and identifying potential issues or inconsistencies.
Remote Management: Actuator allows you to manage and interact with your application remotely. It provides various endpoints, such as /shutdown to gracefully shut down the application, /restart to restart the application, and /actuator to list all available endpoints. These endpoints can be secured using Spring Security for proper access control.
Enabling the Actuator: The simplest way to enable the features is to add a dependency to the spring-boot-starter-actuator dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
}
16. How to Use Jetty Instead of Tomcat in Spring-Boot-Starter-Web?
- Exclude Tomcat from the spring-boot-starter-web dependency.
- Add the spring-boot-starter-jetty dependency.
<!-- Spring Boot Web Starter with Tomcat excluded -->
<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>
<!-- Add Spring Boot Jetty Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
17. How to generate a WAR file with Spring Boot?
- Set the packaging type to war in Maven build tool:
<packaging>war</packaging>
- Add spring-boot-starter-tomcat as the provided scope
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
- Spring Boot Application or Main class extends SpringBootServletInitializer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Springboot2WebappJspApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Springboot2WebappJspApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(Springboot2WebappJspApplication.class, args);
}
}
18. How many types of projects we can create using Spring Boot?
- If we have a spring-boot-starter dependency in a classpath then the spring boot application comes under the default category.
- If we have spring-boot-starter-web dependency in a classpath then the spring boot application comes under the servlet category.
- If we have a spring-boot-starter-webflux dependency in a classpath then the spring boot application comes under the reactive category.
19. How to Change Default Embedded Tomcat Server Port and Context Path in Spring Boot Application?
server.port=8080
server.servlet.context-path=/springboot-webapp
Read more at Spring Boot How to Change Port and Context Path.
20. What Embedded servers does Spring Boot support?
Spring Boot provides support for several embedded servers out-of-the-box. These embedded servers allow you to package your Spring Boot application as a standalone executable JAR file, containing the application and the server runtime.Here are the embedded servers supported by Spring Boot:
Apache Tomcat: Tomcat is the default embedded server in Spring Boot. It provides a robust and widely used HTTP server and servlet container. Spring Boot uses Tomcat as the default embedded server when you include the spring-boot-starter-web dependency.
Jetty: Jetty is another popular choice for embedded servers. It is lightweight, fast, and has a small memory footprint. Spring Boot provides support for Jetty through the spring-boot-starter-jetty dependency.
Undertow: Undertow is a high-performance web server designed for modern applications. It is known for its scalability and low resource consumption. Spring Boot offers support for Undertow through the spring-boot-starter-undertow dependency.
By default, when you create a Spring Boot application, it uses Apache Tomcat as the embedded server. However, you can easily switch to Jetty or Undertow by excluding the Tomcat dependency and including the desired server dependency in your project's build configuration.
21. How to use logging with Spring Boot?
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR
logging.level.net.guides=DEBUG
22. What is the Spring Boot Starter Parent and How to Use it?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
- Configuration - Java Version and Other Properties
- Dependency Management - Version of dependencies
- Default Plugin Configuration
23. How to Write Integration Tests in Spring Boot Application?
24. How to Test Spring MVC Controllers?
25. @SpringBootTest vs @WebMvcTest?
@SpringBootTest annotation loads the full application context so that we can able to test various components. So basically, the @SpringBootTest annotation tells Spring Boot to look for the main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context.@WebMvcTest annotation loads only the specified controller and its dependencies only without loading the entire application. For example, let's say you have multiple Spring MVC controllers in your Spring boot project - EmployeeController, UserController, LoginController, etc then we can use @WebMvcTest annotation to test only specific Spring MVC controllers without loading all the controllers and their dependencies.
Spring Boot provides @SpringBootTest annotation for Integration testing.
Spring boot provides @WebMvcTest annotation for testing Spring MVC controllers (Unit testing).
26. How to Implement Security for Spring Boot Application?
- Enable HTTP basic security
- Register the AuthenticationManager bean with an in-memory store and a single user
- Ignore paths for commonly used static resource locations (such as /css/, /js/, /images/**, etc.)
- Enable common low-level features such as XSS, CSRF, caching, etc.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
security.user.name=admin
security.user.password=secret
security.user.role=USER,ADMIN
27. @RestController vs @Controller in Spring Boot?
Primary Purpose
Response Body
Content Negotiation
Usage:
28. @PathVariable vs @RequestParam in Spring
Usage
URL Example
Declaration
Optional Values
Default Values
Use Case
29. Explain database initialization in Spring Boot
30. What are the advantages of using Spring Boot for microservices?
Simplified development: Spring Boot provides a range of features and defaults, reducing the amount of code and configuration required to develop microservices.
Auto-configuration: It automatically configures various components based on classpath dependencies, reducing the need for manual configuration.
Embedded servers: Spring Boot includes embedded servers like Tomcat, Jetty, or Undertow, making it easy to deploy microservices as standalone JAR files.
Cloud-native support: Spring Boot integrates well with cloud platforms and provides support for building cloud-native microservices.
Comments
Post a Comment
Leave Comment