Top Spring Boot Interview Questions for 3 years experience

Sharing is caring

Here are the list of most common Spring Boot Interview questions for 3 years experienced candidates. This question bank with answers can be leveraged by any fresher to candidates having 2 to 4 years of experience in Spring Boot, who would like to appear in interview for the role of Spring Boot Developer.

What is Spring boot?

Spring boot is a framework provided by spring that helps to create stand-alone, production grade application.

It is ideal for the development of microservices and provides opiniated view of dependencies for simplification of build configuration.

Why are we using Spring boot over Spring MVC?

Spring boot is preferred over Spring MVC because using spring boot, it is easy to create standalone, production grade application.

It provides different starters template for simplification of dependencies and build configuration.

It also provides embedded server support including tomcat as a part of its starter projects. There is no requirement of XML based configuration like Web.xml or Spring.xml.

Also, it provides production ready features such as health checks information for application and metrics.

What is Spring boot starter projects? What are the common starters used in project?

Spring boot starters are template projects provided by Spring boot that provides commonly used dependencies to our application. These starter projects are generally, bundle of many dependencies which are called transitive dependencies.

Example of spring boot starter project :

  • spring-boot-starter-web
  • spring-boot-starter-parent
  • spring-boot-starter-jpa

Which is the default embedded server in Spring Boot?

Tomcat is the default embedded server which comes automatically along with spring-boot-starter-web dependency which has spring-boot-starter-tomcat as transitive dependency.

Which is the default port of tomcat? How to change it?

Default port of tomcat is 8080 which can be changed by adding a configuration using key as server.port in application.properties or application.yml. Eg. : server.port= 8090

The same can also be changed using command line or Spring Application class as well.

Order in which spring boot read the values of configuration keys

  • Embedded server configuration
  • Command line
  • Properties file
  • Spring application class

How can you change embedded server in Spring boot?

We can change default embedded server by removing dependency of tomcat (spring-boot-starter-tomcat) from spring-boot-starter-web by using exclusions tag in pom.xml. We can then add dependency for servers like jetty, netty, underdow in pom.xml.

What is transitive dependency?

Whenever a dependency is added in a spring boot application, whatever subsequent dependency is needed for that dependency to work, are added automatically by spring boot. These are called transitive dependencies.

How many ways we can do dependency injection?

Dependency injection can be done in 3 ways :

  • Setter injection
  • Constructor injection (recommended by Spring)
  • Field injection

Is spring boot different from spring framework?

Spring boot has also all functionalities as spring framework. It tries to address some concerns which spring framework has, using opinionated approach. Spring requires lot of configuration in xml based configuration files without which it cannot work. Spring boot comes with some default configuration and user can run them with minimal effort. However user can modify those default settings and provide custom one as well. Thus, it is easier and faster to develop an enterprise-based application using Spring Boot.

How to add properties to Spring boot?

We can add properties as key value pair in application.properties or application.yml inside the src/main/resources. The format varies slightly between properties and yml files.

We can also create environment specific setting creating application-<profilename>.yml where profile name can be configured inside application.yml.

Can you deploy Spring boot application – as a jar or war?

We can deploy spring boot application both as jar or war. If we need to deploy in any external webserver like tomcat or weblogic, we will generate war.

How can you generate war?

To generate war from spring boot application, we need to add tag <packaging>war</packaging> in pom.xml. Also, it is recommended to mention scope as “provided” in tomcat. Also extend springboot main application class with “SpringBootServletInitializer” because if this is not done, it may generate war, but it may not be executable war.

What is the default logging framework that Spring boot provides?

There are different ways of logging. The default logging framework used by spring boot is logback which is provided by spring-boot-starter-logging which is again a transitive dependency of spring-boot-starter-web.

pom logback dependency

What are the common annotations used in Spring boot?

The following are the most common annotation used in Spring boot application:

  • @SpringBootApplication
  • @Service
  • @Controller
  • @Repository
  • @RequestMapping
  • @ControllerAdvice

What is the use of @SpringBootApplication?

The annotation @SpringBootApplication consists of @ComponentScan, @EnableAutoConfiguration, @Configuration. It also marks the main class of spring boot application from which execution starts.

What is the use of @EnableAutoConfiguration?

This annotation provides best opinionated configuration based on dependencies added to the classpath. When a dependency is added to pom.xml, then dependency is added to classpath and based on the scanning of classpath by @ComponentScan, spring boot provides the preferred configuration for the application.

What is @ComponentScan?

This annotation scans the base package and sub-packages for classes so that all the classes that are marked as component would be auto discovered and registered as beans in Spring Application Context during start of server. The package of the class annotated with @SpringBootApplication is considered as the base package unless some other package is specifically mentioned in the annotation.


Sharing is caring

Leave a Reply

Your email address will not be published. Required fields are marked *