0% found this document useful (0 votes)
176 views

Spring Boot Interview

The @SpringBootApplication annotation combines @Configuration, @EnableAutoConfiguration, and @ComponentScan to simplify bootstrapping a Spring Boot application. It can exclude specific packages or classes from component scanning using the exclude attribute. Specific auto-configuration classes can be excluded using the @EnableAutoConfiguration annotation's exclude or excludeName attributes or by setting properties in application.properties. Spring Actuator provides production-ready REST endpoints to monitor and manage Spring Boot applications. It can be enabled by adding the spring-boot-starter-actuator dependency. The Spring Initializer generates a starter project structure and dependencies. The shutdown endpoint allows graceful application shutdown when enabled. The Embedded Tomcat port can be configured using server.port in application.properties

Uploaded by

Nagaraju Guddati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

Spring Boot Interview

The @SpringBootApplication annotation combines @Configuration, @EnableAutoConfiguration, and @ComponentScan to simplify bootstrapping a Spring Boot application. It can exclude specific packages or classes from component scanning using the exclude attribute. Specific auto-configuration classes can be excluded using the @EnableAutoConfiguration annotation's exclude or excludeName attributes or by setting properties in application.properties. Spring Actuator provides production-ready REST endpoints to monitor and manage Spring Boot applications. It can be enabled by adding the spring-boot-starter-actuator dependency. The Spring Initializer generates a starter project structure and dependencies. The shutdown endpoint allows graceful application shutdown when enabled. The Embedded Tomcat port can be configured using server.port in application.properties

Uploaded by

Nagaraju Guddati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) What does the @SpringBootApplication annotation do internally?

As per the Spring Boot doc, the @SpringBootApplication annotation is


equivalent to using @Configuration, @EnableAutoConfiguration,
and @ComponentScan with their default attributes. Spring Boot enables
the developer to use a single annotation instead of using multiple. But, as
we know, Spring provided loosely coupled features that we can use for each
individual annotation as per our project needs.
2) How to exclude any package without using the basePackages filter?
There are different ways you can filter any package. But Spring Boot
provides a trickier option for achieving this without touching the
component scan. You can use the exclude attribute while using the
annotation  @SpringBootApplication. See the following code snippet:
@SpringBootApplication(exclude= {Employee.class})
public class FooAppConfiguration {}

3) How to disable a specific auto-configuration class?


You can use the exclude attribute of@EnableAutoConfiguration, if you find
any specific auto-configuration classes that you do not want are being
applied.
//By using "exclude"
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

On the other foot, if the class is not on the classpath, you can use
the excludeName attribute of the annotation and specify the fully qualified
name instead.
//By using "excludeName"
@EnableAutoConfiguration(excludeName={Foo.class})

Also, Spring Boot provides the facility to control the list of auto-
configuration classes to exclude by using
the spring.autoconfigure.exclude property. You can add into the
application.properties. And you can add multiple classes with comma
separated.
//By using property file
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataS
ourceAutoConfiguration

4) What is Spring Actuator? What are its advantages?


This is one of the most common interview questions in Spring Boot. As per
the Spring doc:
"An actuator is a manufacturing term that refers to a mechanical device
for moving or controlling something. Actuators can generate a large
amount of motion from a small change."
As we know, Spring Boot provides lots of auto-configuration features that
help developers quickly develop production components. But if you think
about debugging and how to debug, if something goes wrong, we always
need to analyze the logs and dig through the data flow of our application to
check to see what's going on. So, the Spring Actuator provides easy access
to those kinds of features. It provides many features, i.e. what beans are
created, the mapping in the controller, the CPU usage, etc. Automatically
gathering and auditing health and metrics can then be applied to your
application.
It provides a very easy way to access the few production-ready REST
endpoints and fetch all kinds of information from the web. But by using
these endpoints, you can do many things to see here the endpoint docs.
There is no need to worry about security; if Spring Security is present, then
these endpoints are secured by default using Spring Security’s content-
negotiation strategy. Or else, we can configure custom security by the help
of RequestMatcher.
5) How to enable/disable the Actuator?
Enabling/disabling the actuator is easy; the simplest way is to enable
features to add the dependency (Maven/Gradle) to the spring-boot-starter-
actuator, i.e. Starter. If you don't want the actuator to be enabled, then don't
add the dependency.
Maven dependency:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

6) What is the Spring Initializer?


This may not be a difficult question, but the interviewer always checks the
subject knowledge of the candidate. It's often that you can't always expect
questions that you have prepared. However, this is a very common
question asked almost all of the time.
The Spring Initializer is a web application that generates a Spring Boot
project with everything you need to start it quickly. As always, we need a
good skeleton of the project; it helps you to create a project
structure/skeleton properly. You can learn more about the Initializer here.
7) What is a shutdown in the actuator?
Shutdown is an endpoint that allows the application to be gracefully
shutdown. This feature is not enabled by default. You can enable this by
using management.endpoint.shutdown.enabled=true in your
application.properties file. But be careful about this if you are using this.
8) Is this possible to change the port of Embedded Tomcat server in
Spring boot?
Yes, it's possible to change the port. You can use the application.properties
file to change the port. But you need to mention "server.port" (i.e.
server.port=8081). Make sure you have application.properties in your
project classpath; REST Spring framework will take care of the rest. If you
mention server.port=0 , then it will automatically assign any available port.
9) Can we override or replace the Embedded Tomcat server in Spring
Boot?
Yes, we can replace the Embedded Tomcat with any other servers by using
the Starter dependencies. You can use spring-boot-starter-jetty 
or spring-boot-starter-undertow as a dependency for each project as you
need.
10) Can we disable the default web server in the Spring Boot
application?
The major strong point in Spring is to provide flexibility to build your
application loosely coupled. Spring provides features to disable the web
server in a quick configuration. Yes, we can use the application.properties
to configure the web application type, i.e.  spring.main.web-application-
type=none.

You might also like