Spring Boot Flashcards
What Is Spring Boot and What Are Its Main Features?
Spring Boot is essentially a framework for rapid application development built on top of the Spring Framework. With its auto-configuration and embedded application server support, combined with the extensive documentation and community support it enjoys, Spring Boot is one of the most popular technologies in the Java ecosystem as of date.
Here are a few salient features:
Starters – a set of dependency descriptors to include relevant dependencies at a go
Auto-configuration – a way to automatically configure an application based on the dependencies present on the classpath
Actuator – to get production-ready features such as monitoring
Security
Logging
What is JAX-RS ?
What makes Spring Boot superior to JAX-RS?
What Spring Boot features help develop Microservices Applications?
Why Spring Boot is preferred over any other framework?
What are the key dependencies of Spring Boot?
What are the advantages of Spring Boot?
What are the features of Spring Boot?
How do you create a Spring Boot application using Maven?
We can include Spring Boot in a Maven project just like we would any other library. However, the best way is to inherit from the spring-boot-starter-parent project and declare dependencies to Spring Boot starters. Doing this lets our project reuse the default settings of Spring Boot.
Inheriting the spring-boot-starter-parent project is straightforward — we only need to specify a parent element in pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0.RELEASE</version>
</parent>
We can find the latest version of spring-boot-starter-parent on Maven Central.
Using the starter parent project is convenient but not always feasible. For instance, if our company requires all projects to inherit from a standard POM, we can still benefit from Spring Boot’s dependency management using a custom parent.
How do you create a Spring Boot project using Spring Initializer?
What are the Spring Boot Starters?
What is Spring Boot Actuator?
What Are the Differences Between Spring and Spring Boot?
The Spring Framework provides multiple features that make the development of web applications easier. These features include dependency injection, data binding, aspect-oriented programming, data access and many more.
Over the years, Spring has been growing more and more complex, and the amount of configuration such application requires can be intimidating. This is where Spring Boot comes in handy — it makes configuring a Spring application a breeze.
Essentially, while Spring is unopinionated, Spring Boot takes an opinionated view of the platform and libraries, letting us get started quickly.
Here are two of the most important benefits Spring Boot brings in:
Auto-configure applications based on the artifacts it finds on the classpath
Provide non-functional features common to applications in production, such as security or health checks
Please check out our other tutorial for a detailed comparison between vanilla Spring and Spring Boot.
How to Deploy Spring Boot Web Applications as Jar and War Files?
Traditionally, we package a web application as a WAR file and then deploy it into an external server. Doing this allows us to arrange multiple applications on the same server. When CPU and memory were scarce, this was a great way to save resources.
But things have changed. Computer hardware is fairly cheap now, and the attention has turned to server configuration. A small mistake in configuring the server during deployment may lead to catastrophic consequences.
Spring tackles this problem by providing a plugin, namely spring-boot-maven-plugin, to package a web application as an executable JAR.
To include this plugin, just add a plugin element to pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
With this plugin in place, we’ll get a fat JAR after executing the package phase. This JAR contains all the necessary dependencies, including an embedded server. So, we no longer need to worry about configuring an external server.
We can then run the application just like we would an ordinary executable JAR.
Notice that the packaging element in the pom.xml file must be set to jar to build a JAR file:
<packaging>jar</packaging>
If we don’t include this element, it also defaults to jar.
To build a WAR file, we change the packaging element to war:
<packaging>war</packaging>
and leave the container dependency off the packaged file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
After executing the Maven package phase, we’ll have a deployable WAR file.
How to Use Spring Boot for Command-Line Applications?
Just like any other Java program, a Spring Boot command-line application must have a main method.
This method serves as an entry point, which invokes the SpringApplication#run method to bootstrap the application:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
// other statements
}
}
The SpringApplication class then fires up a Spring container and auto-configures beans.
Notice we must pass a configuration class to the run method to work as the primary configuration source. By convention, this argument is the entry class itself.
After calling the run method, we can execute other statements as in a regular program.