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

spring-boot-interview-questions.adoc

The document presents 11 thought-provoking Spring Boot interview questions aimed at fostering understanding rather than rote memorization. Each question addresses key concepts such as application configurations, dependency management, and database access, encouraging deeper discussions. The author emphasizes the importance of grasping these concepts for a solid foundation in Spring Boot interviews.

Uploaded by

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

spring-boot-interview-questions.adoc

The document presents 11 thought-provoking Spring Boot interview questions aimed at fostering understanding rather than rote memorization. Each question addresses key concepts such as application configurations, dependency management, and database access, encouraging deeper discussions. The author emphasizes the importance of grasping these concepts for a solid foundation in Spring Boot interviews.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

= 11 Spring Boot Interview Questions That Make You Think

Marco Behler
2020-12-12
:page-layout: layout-guides
:linkattrs:
:page-image: "/images/guides/undraw_interview_rmcf.png"
:page-metadescription: Here are 11 Spring Boot interview questions that don't focus
on trivial details, but rather make you think and thus learn a lot along the way.
:page-published: true
:page-hidequicklinks: true
:page-tags: ["spring boot interview questions", "spring interview questions"]
:page-commento_id: /guides/spring-interview-questions

Most lists of Spring Boot Interview questions make you memorize random details from
the Spring Boot documentation. But memorization is a poor substitute for truly
understanding and feeling confident about Spring Boot.

So today, we’re going to take a different approach. Rather than listing 50


questions on Spring Boot minutiae, we’re going to focus on 11 that make you think
and thus learn a lot along the way.

Here they are, in random order.

(Note: If you, yourself, are giving Spring Boot Interviews, you might want to ask
these questions in an open-end format leading to discussions, instead of expecting
text-book answers)

== 1. Is the following statement true or false: "Every Spring Boot application is a


web application running within an embedded Apache Tomcat". Give reasons for your
answer.

The statement is false.

When it comes to web applications, Spring Boot works with a variety of servlet
containers. The default one is http://tomcat.apache.org/[Apache Tomcat], but you
can also use it with https://www.eclipse.org/jetty/[Jetty],
https://undertow.io/[Undertow], or without an embedded servlet container at all.

What’s more, Spring Boot isn’t tied to just web applications, though one might get
that impression by using the spring-boot-starter-web dependency and thus Spring
Boot’s web https://www.marcobehler.com/guides/spring-boot[autoconfiguration]. You
can write all kinds of services with it, from batch jobs and command-line services,
over to messaging backends and reactive web applications.

== 2. What is the difference between Spring Boot and Spring MVC? Or between Spring
Boot and Spring Framework? Can you use them together in the same project?

Spring Boot builds _on top_ of https://spring.io/projects/spring-framework[Spring


Framework] and Spring MVC.

Example: Spring Framework offers you the ability to read in .properties files from
a variety of places, e.g. with the help of @PropertySource annotations. It also
offers you the ability to write JSON REST controllers with the help of its Web MVC
framework.
The issue is, you have to tell Spring from where to read in your properties and
properly configure its web framework for e.g. JSON support. Spring Boot, on the
other hand, takes these individual pieces and pre-configures them for you. Example:

* _It always and automatically_ looks for application.properties files in various


predefined places and reads them in.
* _It always boots up an embedded Tomcat_ so you can immediately see the results of
writing your @RestControllers and start writing web applications
* _It automatically_ configures everything for you to send/receive JSON, without
needing to worry a ton about specific Maven/Gradle dependencies.
All, by running the main method in a Java class, which is annotated with the
@SpringBootApplication annotation. +
If this explanation still leaves you with questions, have a look at
https://www.marcobehler.com/guides/spring-framework[this extensive guide I wrote],
covering the topic in more detail.

== 3. Name two ways to create a new Spring Boot project from scratch? Also, how do
you know what spring-boot-starters your project needs?

You can create new Spring Boot projects through the https://start.spring.io/[Spring
Initializr] web application or the
https://docs.spring.io/spring-boot/docs/current/reference/html/getting-
started.html#getting-started-installing-the-cli[Spring Boot CLI]. Interestingly
enough, Spring Initializr is not just a website where you can generate project
skeleton .zip files. It is also an API, that you can programmatically call. All
major IDEs (Spring Tool Suite, IntelliJ IDEA Ultimate, Netbeans and VSCode)
directly integrate with it, so that you can create new Spring Boot projects right
out of your IDE.

As for starters, you need to read


https://docs.spring.io/spring-boot/docs/2.3.3.RELEASE/reference/htmlsingle/#using-
boot-starter[the documentation] and have a bit of experience. If working with a web
application, you will start with _spring-boot-starter-web_ and then add the
appropriate starter from the documentation, once you want to include a certain
technology. After a while, you will get a good feel for what starters you need for
which technologies.

== 4. Why do you not need to specify dependency versions in your pom.xml file when
including 3rd party libraries? Does that hold true for all 3rd party libraries or
only some? How can you find out what libraries Spring Boot supports?

This is because Spring Boot does _some_ dependency management for you.

On a high-level, Spring Boot starters pull-in a parent pom.xml file (or a


build.gradle file) which has all the dependencies and respective versions defined
that a specific Spring Boot version supports - a so-called bill of materials. You
can then simply use those pre-defined versions, or override the version numbers in
your own build scripts.

You can find the list of all currently supported 3rd party libraries and versions
in the https://github.com/spring-projects/spring-boot/blob/master/spring-boot-
project/spring-boot-dependencies/build.gradle[spring-boot-dependencies] project.

== 5. You want to make your application configurable, say specify a different


database connection for development and production environments. What are your
options?

By default, Spring Boot pulls in properties


https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-
features.html#boot-features-external-config[from almost 20 locations], from
environment variables and command-line arguments to configuration files
(application.properties). +
These locations are also ordered, so that the locations lower in the list, override
earlier ones. You can, for example, put a default application.properties inside
your deployable .jar file, whereas your ops colleagues only override some of them
by supplying their own application.properties file on the deployment machine.

It is essential to understand these locations and the default behavior, as to not


re-implement Spring Boot’s properties functionality with a custom
PropertySourceProvider or use more heavyweight options like spring-cloud
configuration server.

You also need to make sure to understand Spring’s https://docs.spring.io/spring-


boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-
config-relaxed-binding-from-environment-variables[relaxed properties binding
concept], in that you can bind properties from those locations to configuration
bean properties, without an explicit name match.

== 6. Is the following statement true or false: "Every Spring Boot project must use
Thymeleaf as an HTML templating engine". What are your options when it comes to
rendering HTML?

The statement is false.

Spring Boot works with a variety of HTML template engines, and while
https://www.thymeleaf.org/[Thymeleaf] is a popular choice and fully integrated with
Spring, you can also use many others, like
https://freemarker.apache.org/[Freemarker], https://velocity.apache.org/[Velocity]
or even https://docs.oracle.com/javaee/5/tutorial/doc/bnagy.html[JSP] (though not
strictly a templating engine).

It is usually recommended to go with the option you are most comfortable with /
that is being used in your company as the default.

== 7. How can you implement relational database access with Spring Boot? What are
your options?

Spring Boot integrates with plenty of Java database access libraries


(https://www.marcobehler.com/guides/java-databases[see a comprehensive list here]).
The majority of users will probably go with https://spring.io/guides/gs/accessing-
data-jpa/[spring-boot-starter-jpa], https://spring.io/guides/gs/relational-data-
access/[spring-boot-starter-jdbc] or one of the corresponding
https://spring.io/projects/spring-data[spring-data projects].

There are also more lightweight alternatives, such as https://www.jooq.org/[jOOQ]


or https://mybatis.org/mybatis-3/[myBatis]. Finally, you can always go with NoSQL
options, such as https://spring.io/guides/gs/accessing-data-mongodb/[MongoDB] etc.
== 8. You need to configure logging in your application but you want to
differentiate between the log levels on your machine and the log levels on
different environments (qa, test, prod). What options do you have?

First off, it is always a good idea to properly understand the


https://www.marcobehler.com/guides/a-guide-to-logging-in-java[Java logging
ecosystem] and then read the corresponding Spring Boot
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-
features-logging[chapter on logging].

There are various options when it comes to configuring your output:

* Directly in an application.properties file (that can and should of course differ


in DEV and PRD environments).
* Depending on the used logging framework, specifying a custom configuration file
(such as logback-spring.xml).
* Or even through JMX at runtime.

The two popular, modern logging libraries, Logback and Log4j2, also support hot
reloading of the logging configuration, without having to bounce your application

== 9. What is the easiest way to deploy a Spring Boot application in production?


What other options are there?

The simplest way to deploy your Spring Boot application is as a .jar file with an
embedded servlet container, to any server or platform that has a JRE installed. +
For organizational and historical reasons, you can also deploy your Spring Boot
application as a .war file, into an existing servlet container or application
server.

Last but not least, you can, of course, also put your .jar file into a Docker image
and even deploy those with Kubernetes.

== 10. You’ve been told to enable "Spring Security" on your application. What
happens when you add the Spring Security starter to your application?

This is a bit of a trick question. Adding the Spring Security Starter to your app,
will suddenly prompt you with a login every time you try to access your
application. Also, form submissions/REST endpoints will work differently or are
outright blocked.

The gist is, that you "do not simply enable" security on a Spring Boot application,
you need a solid understanding of what you are doing.

Luckily, I wrote a comprehensive https://www.marcobehler.com/guides/spring-


security[guide on Spring Security], that explains all the gory aspects of security
in the easiest way possible.

== 11. How would you find out which auto-configurations Spring Boot applied on
startup and which conditions it evaluated?

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-
features.html[Spring Boot Actuato]r can provide that information, through HTTP or
JMX endpoints. Alternatively, you can start-up your Spring Boot application with
the "--debug" flag.
Do note, that the information on evaluated conditions is a bit "raw" and not easily
digested. For that, https://www.marcobehler.com/guides/spring-boot[read this guide]
to make sure you understand how Spring Boot’s auto-configurations work.

== Fin

There is obviously no guarantee that you will meet these questions in your own
Spring Boot interview, though knowing (and understanding) the answers to them
should prove as a solid foundation for any interview.

If you want to get a deeper understanding of the entire Spring ecosystem, you might
also want to check out the other Spring articles
https://www.marcobehler.com/guides[on my blog] and the
https://www.marcobehler.com/courses/spring-professional[Confident Spring Developer]
course.

Do you have any other questions you think might be useful for interviews? Let me
know in the comment section.

Enjoy!

You might also like