Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Popular Interview Questions on Spring Boot Framework

Published On: February 15, 2025

As people look to expand their knowledge and agility in a world that is changing quickly, the need for spring book skills has increased. In order to create scalable, safe, and effective applications, professionals are increasingly seeking expertise in Spring Boot, Spring MVC, and Spring Security. To help with your preparation, here is a list of 40 popular interview questions on Spring Boot and their brief answers. Explore our Spring Boot course syllabus.

Spring Boot Interview Questions for Freshers

Here are the Spring Boot interview questions for freshers with answers:

1. What is Spring Boot?

Spring Boot is a framework designed to make developing, configuring, and deploying Spring-based applications easier. The Spring framework serves as the foundation for it. 

2. What are the advantages of using Spring Boot?

The advantages of using Spring Boot are:

  • Auto-configuration
  • Embedded servers (Tomcat, Jetty)
  • Simplified dependency management
  • No XML configuration
  • Production-ready features (actuator, metrics)

3. Write the difference between Spring and Spring Boot.

While Spring Boot offers auto-configuration and convention-over-configuration, Spring necessitates manual configuration.

Spring Spring Boot
FeaturesA complete model for programming and configuration.Pre-configured templates and autoconfiguration.
PurposeCreate applications at the enterprise level. Create applications based on Spring.
BenefitsCreate reusable and modular code.Simplified procedure for development.
Use CasesWebsites with event-driven, microservice-based, or serverless architectures.Stand-alone app development.

4. What is the purpose of the @SpringBootApplication annotation?

In Spring Boot, a configuration class is identified by the @SpringBootApplication annotation. It makes component scanning, auto-configuration, and additional bean registration possible. 

It bootstraps a Spring Boot application by combining @Configuration, @EnableAutoConfiguration, and @ComponentScan.

Example:

@SpringBootApplication

public class Application {

  public static void main(String[] args) {

    SpringApplication.run(Application.class, args);

  }

}

5. What is Spring Boot Starter?

A set of pre-configured dependencies called Spring Boot Starters helps in the development of Spring-based applications. They cover a range of Spring and associated technologies, including context, aop, beans, and more.

Starters, such as spring-boot-starter-web for web applications, are dependency descriptors that make it easier to include related dependencies.

Examples:

For Database Access: spring-boot-starter-data-jpa

For Spring Web Services: spring-boot-starter-web-services

6. What is the default embedded server in Spring Boot?

Apache Tomcat is Spring Boot’s default embedded server. Tomcat-specific properties can be used to alter its behavior.  

  • By default, Tomcat is used as the embedded container by the Spring Boot starters, such as spring-boot-starter-web.
  • Some pre-configured default behavior is included with the basic Tomcat server.
  • The default Tomcat port can be altered. 

7. How do you change the default port in Spring Boot?

The following interfaces and settings files allow us to modify the embedded server’s default port number in a Spring Boot application:

  • application. properties.
  • application. yml.
  • Command-Line Interface.
  • WebServerFactoryCustomizer Interface.
  • EmbeddedServletContainer Interface.

In the application.properties file, set server.port (for example, server.port=8081). 

Recommended: Spring Boot Online Course Program.

8. What is the use of application.properties in Spring Boot?

Spring Boot’s application properties are configuration options that aid in personalizing the behavior of an application. The application.properties file contains them. Database connections, server ports, and other application-specific features are configured using it. 

Endpoints, Loggers, Metrics, Tracing, Auditing, Recording HTTP exchanges, and Process monitoring are some common application properties in Spring Boot.

9. How do you enable debugging in Spring Boot?

By starting your application with the –debug flag, you can also activate a “debug” mode. Additionally, you can set debug=true in your application.properties

Several fundamental loggers, including the embedded container, Hibernate, and Spring Boot, are set up to produce additional data when the debug mode is activated. Either set debug=true in application.properties or add –debug to the command line.

10. What is the difference between @RestController and @Controller?

Components or classes known as controllers manage client requests and choose which response to return. Annotations called RestControllers aid in the creation of controllers that provide data back to the client directly.  

Combining @Controller and @ResponseBody, @RestController returns data (like JSON) directly rather than via a view.

@Controller@RestController
PurposeReturns views (HTML Page).Returns data (JSON, XML).
Use CaseFor web apps that require HTML-based views.To provide APIs that provide clients, mobile apps, or other applications with data.
FunctionBefore responding, data is formatted using a separate rendering process.Embeds data into the response body and bypasses the rendering stage.

11. What is Spring Boot Actuator?

A module called Spring Boot Actuator aids developers in managing and keeping an eye on Spring Boot applications. It offers information on resource utilization, performance, and application health. It has features like endpoints, health indicators, custom endpoints, security, and sanitization.

12. How do you secure actuator endpoints?

You can control which users can access particular actuator endpoints and what actions they can take on them by integrating Spring Security to implement authentication and authorization mechanisms. This is usually done within a Spring Boot application. 

To secure actuator endpoints, you must carefully manage which endpoints are exposed using configuration settings like management.endpoints.web.exposure.include within your application properties file. Access rules are configured based on user roles.

13. What is the difference between @PathVariable and @RequestParam?

When working with a lot of arguments or when the parameters are optional, use @RequestParam to retrieve the query parameters. 

In RESTful APIs, where the resource hierarchy is reflected in the URL structure, use @PathVariable when the arguments are required and part of the URL route.

14. How do you handle exceptions in Spring Boot?

For managing global exceptions, use @ControllerAdvice and @ExceptionHandler.

  • @ExceptionTo handle exceptions in specific Handler classes or Handler methods, utilize the handler annotation that Spring Boot provides. 
  • Spring Configuration automatically recognizes any method that has this annotation as an Exception Handler Method.

For learning fundamentals, enroll in our Java training in Chennai.

15. What is the purpose of @ConfigurationProperties?

It links a Java object to external configuration properties, such as those from application.properties. We can quickly map all of the properties or YAML files into an object using @ConfigurationProperties. 

Additionally, it enables us to use JSR-303 bean validation to verify properties. The annotation automatically pulls information from the application.properties file.

16. How do you configure a database connection in Spring Boot?

You can construct an entity class, set up the database connection in application.properties, and add the database connector dependency to pom.xml in order to configure a database connection in Spring Boot.  

Utilize properties such as spring.datasource.password, spring.datasource.username, and spring.datasource.url.

17. What is the difference between @Component, @Service, and @Repository?

Whereas @Service and @Repository are more specialized annotations for distinct application layers, @Component is a general annotation for any Spring-managed bean in Spring.

@Component@Service@Repository
An all-purpose annotation for any bean that is maintained by Spring.It shows the business logic layer, where business logic is stored in classes.It represents the persistence layer, where classes are used to perform database activities like retrieval and storing.
Any bean controlled by Spring is a generic archetype.A service layer bean is indicated.It shows that a DAO layer bean is present and that exception translation is enabled.

Check your skills with our Java interview questions and answers.

18. What is Spring Boot DevTools?

Developer-friendly features like live reloading and automatic restarts are offered by DevTools. Boots for Spring DevTools is a set of tools designed to increase the productivity of developers. It has functions like debugging, live reloading, and automated restart.

By including the module requirement in your build, you can incorporate Spring Boot DevTools into any project. It offers browser extensions for Firefox, Safari, and Chrome.  

19. How do you enable CORS in Spring Boot?

The @CrossOrigin annotation can be used at the method, class, or global level to enable Cross-Origin Resource Sharing (CORS) in Spring Boot.  

We can use the @CrossOrigin annotation or configure it globally using a WebMvcConfigurer.

Method-Level CORS:

  • Include the annotation in a spring-mvc handler that has @RequestMapping annotated. 
  • Example: @CrossOrigin(origins = “http://localhost:9000”).

Class-Level CORS:

  • Put the annotation at the class’s top.
  • Example: @RestController @RequestMapping(“API/coffee”) @CrossOrigin public class CoffeeController {

Global CORS:

  • For the MVC framework, set the global configuration as a @Configuration annotation.

20. What is the purpose of @EnableAutoConfiguration?

By reducing boilerplate code, the @EnableAutoConfiguration annotation in Spring Boot tells the framework to automatically configure your application context based on the dependencies listed on your classpath. 

  • This allows the framework to “guess” what beans you probably need and set them up without manual configuration, greatly accelerating development.
  • It makes it possible for Spring Boot’s auto-configuration feature to set up beans automatically according to dependencies.

Example:

@SpringBootApplication

public class MyApplication {

    // … 

}

Spring Boot Interview Questions for Experienced

Here are the Spring Boot interview questions and answers for experienced applicants.

1. What is Spring Boot’s auto-configuration?

Beans are automatically configured via auto-configuration according to the dependencies in the classpath. 

By adding the required dependency to your project, Spring Boot’s auto-configuration feature sets up your Spring application automatically based on the libraries (dependencies) on your classpath.

This eliminates the need to manually define common configurations for commonly used technologies, enabling quicker development with less boilerplate code.

Example: 

Your code won’t need to explicitly configure a connection pool or other parameters for dealing with a MySQL database if you include the “mysql” database connector dependency in your Spring Boot project.

2. How do you create a custom auto-configuration in Spring Boot?

In META-INF/spring.factories, create a @Configuration class and register it.

Below are the steps in creating a custom auto-configuration in Spring Boot:

  • Step 1: Create a class annotated with @Configuration and using conditional annotations such as @ConditionalOnClass or @ConditionalOnProperty to specify when the auto-configuration.
  • Step 2: We should define the beans that will be automatically configured based on your conditions within the class. 
  • Step 3: We must register your auto-configuration class in the META-INF/spring.factories file to inform Spring Boot of it.

3. What is the difference between @Mock and @MockBean?

“@Mock” is for isolated unit testing, and “@MockBean” is for Spring-aware integration testing, where you want to use the Spring dependency injection mechanism to mock beans within the context. 

@Mock@MockBean
It is used in isolated unit tests for simple mocking in conjunction with Mockito.It is used to simulate Spring beans in the context of an application during Spring Boot testing.
It doesn’t communicate with the Spring application context directly.In the Spring context, it automatically swaps out the real bean for the mimicked one.
It requires the use of @InjectMocks or MockitoAnnotations.initMocks to manually inject mock objects into the test class.It allows us to fake particular dependencies and test how your class communicates with other Spring beans. 

Enrich your skills with our J2EE training in Chennai.

4. How do you profile-specific configurations in Spring Boot?

Use application-{profile}.properties and activate the profile using spring.profiles.active.

  • Using the “spring.profiles.active” property in your main application configuration file, you can activate the desired profile by telling Spring Boot which profile to load settings from. 
  • You can also use the @Profile annotation on Spring beans to specify which profile they should be active in. 
  • This allows you to achieve profile-specific configuration in Spring Boot by creating separate configuration files for different profiles (such as “dev”, “prod”, and “test”) with a naming convention like “application-{profile}.properties” or “application-{profile}.yml.”  

5. What is the purpose of @Conditional annotations in Spring Boot?

They use certain circumstances (e.g., @ConditionalOnClass, @ConditionalOnProperty) to conditionally load beans.

We can dynamically modify settings based on the environment or other criteria by using the @Conditional annotation in Spring Boot to conditionally register beans within the application context. This means that a bean will only be produced if certain circumstances are met at runtime.

6. How do you implement caching in Spring Boot?

We can set up a cache provider by using the @EnableCaching annotation.

Example: EhCache, Redis.

7. What is the difference between @SpringBootTest and @WebMvcTest?

The web layer is the sole focus of @WebMvcTest, whereas @SpringBootTest loads the complete application environment.

  • @WebMvcTest only offers the simulated test environment.
  • @SpringBootTest can step in both the real webEnvironment (WebEnvironment. RANDOM_PORT) and the simulated webEnvironment (WebEnvironment. MOCK).

8. How do you enable HTTPS in Spring Boot?

Set up SSL settings in the program.attributes and supply the keystore file.

  • To activate HTTPS in a Spring Boot application, you must first obtain an SSL certificate and then set up your application to use it by indicating the keystore location and other relevant information.
  • By setting properties like server.ssl.enabled=true and entering the required certificate information, such as the keystore path and password, you can effectively instruct the server to use HTTPS. 
  • For development, you can create a self-signed certificate, but for production, use one issued by a reliable Certificate Authority (CA).

9. What is the purpose of CommandLineRunner and ApplicationRunner?

Command Line and Application Runner You can run the code after the Spring Boot application has begun thanks to runner interfaces. As soon as the application launches, you can use these APIs to carry out any task. They let you run code once the program has launched.

10. How do you externalize configuration in Spring Boot?

Make use of cloud configuration services, environment variables, or application.properties.

  • You usually create a specific configuration file (such as “application.properties” or “application.yaml”) in the classpath of your project to externalize configuration in Spring Boot. 
  • In this file, you define key-value pairs that represent your application settings. 
  • This way, you can change the configuration values without recompiling the code and manage different environments with ease by using profiles. 
  • You can then access these values within your application by using the @Value annotation to inject them directly into your beans.

Accelerate your career with our advanced Java training in Chennai.

11. What is Spring Cloud?

An open-source framework called Spring Cloud aids programmers in creating cloud applications. It offers tools for popular distributed system patterns, such as circuit breakers, configuration management, and service discovery.  

The salient features of Spring Cloud are, microservices, event-driven apps, distributed systems, and cloud environments. They will be used to automate administrative tasks, improve fault tolerance, and build apps for mobile devices. 

12. How do you implement service discovery in Spring Boot?

Use Spring Cloud Netflix Eureka or Consul.

  • By adding the required dependencies to your Spring Boot project and configuring the Eureka client and server in your application properties file, you can create a dedicated “Eureka Server”.
  • It serves as a registry for all of your microservices, enabling other services to find and connect to them by registering with the Eureka server and querying it to find available instances. 

This is how you usually implement service discovery in Spring Boot using the Spring Cloud Netflix Eureka component.

13. What is the purpose of @LoadBalanced in Spring Boot?

It allows client-side load balancing in your application.

  • In Spring Boot, the @LoadBalanced annotation is used to mark a bean, usually a RestTemplate, as eligible for load balancing. 
  • This means that when this bean makes requests to other services, Spring Cloud will automatically distribute the traffic across multiple instances of that service, ensuring high availability and preventing any single instance from becoming overloaded.

14. How do you implement circuit breakers in Spring Boot?

Use Spring Cloud Netflix Hystrix or Resilience4j.

  • The “Resilience4j” library is the main tool used to implement circuit breakers in Spring Boot. 
  • You add the required dependency to your project and then use @CircuitBreaker to annotate methods in your service layer that specify when a circuit breaker should be triggered. 

This allows you to specify fallback methods to run when the circuit is open. You can also customize the behavior of the circuit breaker by configuring parameters like failure thresholds and timeout durations in your application properties file.

15. What is the difference between @FeignClient and RestTemplate?

@FeignClient is a declarative REST client, while RestTemplate is programmatic.

While both @FeignClient and RestTemplate are used in Spring Boot to make HTTP requests, here are the major differences:

@FeignClientRestTemplate
It defines REST API clients in a more declarative and concise manner.It offers a more flexible, lower-level method of creating and executing HTTP requests.
It is perfect for microservices communication.It is better suited for simpler scenarios where fine-grained control is required. 
FeignClient uses annotations to hide a large portion of the HTTP complexity, making your code cleaner.RestTemplate requires more explicit code to build requests.

Hone your skills with our Hibernate course in Chennai.

16. How do you write unit tests in Spring Boot?

For integration testing, use @SpringBootTest; for mocking dependencies, use @MockBean.

  • Step 1: Creating a New Spring Boot Project.
  • Step 2: Adding dependencies.
  • Step 3: Create the folder structure. 
  • Step 4: Writing code for user registration.

17. What is the purpose of @DataJpaTest?

@DataJPATest configures an in-memory database and tests JPA repositories.

  • It basically offers a lightweight method of testing your database interactions with an embedded in-memory database. 
  • The @DataJpaTest annotation in Spring Boot is used to specifically test the JPA (Java Persistence API) layer of your application. 
  • It is allowing you to concentrate on testing your repositories and entity mappings without loading the entire application context.

18. How do you test REST APIs in Spring Boot?

We can test REST APIs in Spring Boot using MockMvc or TestRestTemplate.

  • The main tool for testing REST APIs in Spring Boot is the Spring Test framework, which uses tools like TestRestTemplate to send HTTP requests to your API endpoints and annotations like @SpringBootTest to launch a full application context. 
  • The response data is then asserted using JUnit assertions. 
  • For a more efficient testing experience of RESTful web services, you can also use well-known libraries like Rest Assured.

19. What is the difference between @Mock and @Spy?

While @Spy encapsulates an existing object and permits partial mocking, @Mock generates a mock object.

  • In unit tests, mock objects replicate behavior and take the place of actual objects to confirm how objects interact. 
  • Real objects with instrumentation to trace method calls and maintain their original behavior while monitoring interactions are known as spy objects.

20. How do you test actuator endpoints?

Invoke actuator endpoints and verify replies using TestRestTemplate or MockMvc.

  • Actuator endpoints can be tested by sending GET requests to the specific actuator endpoint URLs within your application using an HTTP client, such as a web browser, or a specialized tool. 
  • Usually, this can be done by going to http://\your-app-host>:\port>/actuator/ in a browser, where it could be “health”, “env”, “metrics”, etc., depending on the information you wish to retrieve. For more complex requests, you can use tools like Postman.  

Explore all our software courses to launch your job in IT.

Conclusion

Since enterprise-level programming frequently uses Spring, a well-liked framework for Java applications, knowledge in this field is extremely valuable. From fundamentals to more complex ideas, a variety of Spring Boot subjects are covered in these interview questions on Spring Boot framework. To ace the interview, practice with real-time projects in our Spring Boot training institute in Chennai.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.