spring-boot-interview-questions.adoc
spring-boot-interview-questions.adoc
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.
(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)
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?
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:
== 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.
== 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.
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.
== 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?
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?
The two popular, modern logging libraries, Logback and Log4j2, also support hot
reloading of the logging configuration, without having to bounce your application
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.
== 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!