Spring Boot Interview Questions Advanced.

Jirapong Bualuang
9 min readApr 15, 2023

--

Spring framework and Spring boot for Java. #credit image edureka.

13. What Are the Basic Annotations that Spring Boot Offers?

Spring Boot offers several annotations that simplify the development of Spring-based applications. Here are some of the basic annotations that Spring Boot provides:

@SpringBootApplication: This annotation is used to mark the main class of a Spring Boot application. It enables component scanning, auto-configuration, and starts the embedded web server.

@Controller: This annotation is used to mark a class as a Spring MVC controller. It handles HTTP requests and returns HTTP responses.

@RestController: This annotation is a combination of @Controller and @ResponseBody. It is used to mark a class as a RESTful controller. It handles HTTP requests and returns JSON/XML responses.

@Service: This annotation is used to mark a class as a service. It encapsulates the business logic of an application.

@Repository: This annotation is used to mark a class as a repository. It is used to interact with a database.

@Component: This annotation is the base annotation for all Spring-managed components. It is used to mark a class as a bean.

@Autowired: This annotation is used to inject dependencies into a Spring-managed bean.

@Qualifier: This annotation is used to specify which bean to inject when multiple beans of the same type are available.

@Value: This annotation is used to inject values from the properties file into a Spring-managed bean.

@Configuration: This annotation is used to mark a class as a configuration class. It provides configuration to the Spring application context.

@Profile: This annotation is used to activate/deactivate a bean based on the specified profile.

@RequestMapping: This annotation is used to map an HTTP request to a method in a controller.

@PathVariable: This annotation is used to extract a variable from the URL path.

These are some of the basic annotations that Spring Boot provides. There are many more annotations available that you can explore in the Spring documentation.

14. What is Spring Boot dependency management?

Spring Boot dependency management is used to manage dependencies and configuration automatically without you specifying the version for any of that dependencies.

15. Can we create a non-web application in Spring Boot?

Yes, it is possible to create a non-web application in Spring Boot. Although Spring Boot is often used to develop web applications, it can be used for non-web applications as well.

Spring Boot provides several features that are not related to web development. For example, Spring Boot provides support for data access, caching, messaging, scheduling, and batch processing. These features can be used to develop non-web applications such as command-line applications, standalone applications, background tasks, and batch jobs.

To create a non-web application in Spring Boot, you can exclude the web-related dependencies from your project. You can do this by adding the spring-boot-starter dependency to your pom.xml file and excluding the web-related dependencies using the exclude tag.

For example, to create a command-line application, you can add the spring-boot-starter dependency to your pom.xml file and exclude the web-related dependencies as follows:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>

Once you have excluded the web-related dependencies, you can use the Spring Boot features to develop your non-web application. For example, you can use Spring Data to access a database, use Spring Batch to process large volumes of data, or use Spring Integration to integrate with other systems.

In summary, Spring Boot can be used to develop non-web applications as well, and it provides several features that are not related to web development.

16. Is it possible to change the port of the embedded Tomcat server in Spring Boot?

Yes, it is possible to change the port of the embedded Tomcat server in Spring Boot. By default, Spring Boot applications run on port 8080. However, you can change the port by modifying the application.properties or application.yml file.

To change the port of the embedded Tomcat server in Spring Boot, you can add the following line to your application.properties file:

server.port=8081

In the above example, the server will start on port 8081 instead of the default port 8080.

Alternatively, you can also specify the port in the application.yml file as follows:

server:
port: 8081

In addition to the server.port property, you can also specify other properties related to the Tomcat server in the application.properties or application.yml file. For example, you can set the maximum number of threads, the maximum size of the request, or the timeout for idle connections.

In summary, it is possible to change the port of the embedded Tomcat server in Spring Boot by modifying the application.properties or application.yml file. This provides flexibility and allows you to run your Spring Boot application on a different port than the default port 8080.

17. What is the default port of tomcat in spring boot?

The default port of the tomcat server-id 8080. It can be changed by adding sever.port properties in the application.property file.

18. Can we override or replace the Embedded tomcat server in Spring Boot?

Yes, we can replace the Embedded Tomcat server with any server by using the Starter dependency in the pom.xml file. Like you can use spring-boot-starter-jetty as a dependency for using a jetty server in your project.

19. Can we disable the default web server in the Spring boot application?

Yes, we can use application.properties to configure the web application type i.e spring.main.web-application-type=none.

20. How to disable a specific auto-configuration class?

You can use exclude attribute of @EnableAutoConfiguration if you want auto-configuration not to apply to any specific class.

//use of exclude
@EnableAutoConfiguration(exclude={className})

21. Explain @RestController annotation in Sprint boot?

It is a combination of @Controller and @ResponseBody, used for creating a restful controller. It converts the response to JSON or XML. It ensures that data returned by each method will be written straight into the response body instead of returning a template

22. What is the difference between @RestController and @Controller in Spring Boot?

@Controller Map of the model object to view or template and make it human readable but @RestController simply returns the object and object data is directly written in HTTP response as JSON or XML.

23. Describe the flow of HTTPS requests through the Spring Boot application?

When an HTTPS request is made to a Spring Boot application, the request flows through a series of components and processes. The flow of HTTPS requests through a Spring Boot application can be described as follows:

  • The client sends an HTTPS request to the Spring Boot application.
  • The request is received by the embedded Tomcat server in the Spring Boot application.
  • The SSL/TLS handshake process is initiated by the Tomcat server. The server presents its digital certificate to the client to authenticate itself.
  • If the client trusts the server’s certificate, it sends a message indicating that the handshake was successful. If the client does not trust the server’s certificate, it will reject the connection.
  • Once the SSL/TLS handshake is complete, the request is passed to the Spring MVC DispatcherServlet.
  • The DispatcherServlet analyzes the request and maps it to the appropriate controller and handler method.
  • The controller processes the request and returns a ModelAndView object to the DispatcherServlet.
  • The DispatcherServlet then resolves the view, generates the response, and sends it back to the client over the encrypted HTTPS connection.
  • If the client needs to make another request, the process repeats from step 1.

In summary, the flow of HTTPS requests through a Spring Boot application involves the SSL/TLS handshake process, the Spring MVC DispatcherServlet, and the controller and view components. This flow ensures that requests and responses are transmitted securely over an encrypted connection, protecting sensitive data from eavesdropping and tampering.

24. What is the difference between RequestMapping and GetMapping?

RequestMapping can be used with GET, POST, PUT, and many other request methods using the method attribute on the annotation. Whereas getMapping is only an extension of RequestMapping which helps you to improve on clarity on request.

25. What is the use of Profiles in spring boot?

While developing the application we deal with multiple environments such as dev, QA, Prod, and each environment requires a different configuration. For eg., we might be using an embedded H2 database for dev but for prod, we might have proprietary Oracle or DB2. Even if DBMS is the same across the environment, the URLs will be different.

To make this easy and clean, Spring has the provision of Profiles to keep the separate configuration of environments.

26. What is Spring Actuator? What are its advantages?

An actuator is an additional feature of Spring that helps you to monitor and manage your application when you push it to production. These actuators include auditing, health, CPU usage, HTTP hits, and metric gathering, and many more that are automatically applied to your application.

27. How to enable Actuator in Spring boot application?

Actuator is a set of Spring Boot modules that provides production-ready features to help you monitor and manage your application. To enable Actuator in a Spring Boot application, you can follow these steps:

  1. Add the Actuator dependency to your pom.xml file or build.gradle file. For example, if you are using Maven, add the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. Once the dependency is added, Actuator endpoints will be enabled by default. You can access them by visiting the URL http://localhost:8080/actuator in your browser.

Note that the actual port number may be different depending on the configuration of your application.

3. You can also customize the Actuator endpoints by modifying the application.properties or application.yml file. For example, to enable the /health endpoint, you can add the following line to your application.properties file:

management.endpoints.web.exposure.include=health

This will expose the /health endpoint, which will provide information about the health of your application.

4. You can also add custom Actuator endpoints by creating a new class that implements the Endpoint interface. For example, to create a custom endpoint that provides information about the number of active sessions in your application, you can create a class like this:

@Endpoint(id = "session-count")
public class SessionCountEndpoint {

private final HttpSession httpSession;

public SessionCountEndpoint(HttpSession httpSession) {
this.httpSession = httpSession;
}

@ReadOperation
public int getSessionCount() {
return httpSession.getActiveSessions().size();
}
}

This class creates a new endpoint with the ID session-count and provides information about the number of active sessions in the application. You can then access this endpoint by visiting the URL http://localhost:8080/actuator/session-count in your browser.

In summary, to enable Actuator in a Spring Boot application, you need to add the Actuator dependency to your project, and then you can access the Actuator endpoints at http://localhost:8080/actuator. You can customize the Actuator endpoints by modifying the application.properties or application.yml file, and you can also add custom endpoints by creating a new class that implements the Endpoint interface.

29. How to check the environment properties in your Spring boot application?

Spring Boot actuator “/env” returns the list of all the environment properties of running the spring boot application.

30. What is dependency Injection?

Dependency Injection (DI) is a design pattern used in software engineering and a fundamental concept in Spring Framework and Spring Boot. DI is a process of providing dependencies (objects) to a class, rather than the class creating its own dependencies. In other words, DI is a way of removing the hard-coded dependencies from the code and providing them externally.

The core idea behind DI is to decouple the classes in an application by removing hard-coded dependencies between them. Instead, dependencies are passed to a class via constructors, methods, or fields. This makes the code more modular, easier to test, and easier to maintain.

DI is typically implemented using an Inversion of Control (IoC) container, such as the Spring Framework. The container manages the lifecycle of objects and their dependencies, and automatically injects the necessary dependencies into objects at runtime. The dependencies are configured using a set of rules or configurations, which are typically stored in XML, Java annotations, or Java code.

In Spring Boot, DI is a key concept that is used throughout the framework. For example, when you use @Autowired annotation in your code, Spring Boot automatically injects the necessary dependencies into your objects, making it easier to write modular, testable, and maintainable code.

In summary, Dependency Injection is a design pattern used in software engineering to remove hard-coded dependencies between classes and to provide dependencies externally. It is a fundamental concept in Spring Framework and Spring Boot, and is typically implemented using an Inversion of Control container.

--

--

Jirapong Bualuang

Software Engineer | Co-Founder @ TAN TIME INNOVATION GROUP CO., LTD.