Spring Boot Flashcards

1
Q

What Is Spring Boot and What Are Its Main Features?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is JAX-RS ?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What makes Spring Boot superior to JAX-RS?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What Spring Boot features help develop Microservices Applications?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why Spring Boot is preferred over any other framework?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the key dependencies of Spring Boot?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of Spring Boot?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the features of Spring Boot?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you create a Spring Boot application using Maven?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a Spring Boot project using Spring Initializer?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the Spring Boot Starters?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Spring Boot Actuator?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What Are the Differences Between Spring and Spring Boot?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to Deploy Spring Boot Web Applications as Jar and War Files?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to Use Spring Boot for Command-Line Applications?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What Are Possible Sources of External Configuration?

A

Spring Boot provides support for external configuration, allowing us to run the same application in various environments. We can use properties files, YAML files, environment variables, system properties and command-line option arguments to specify configuration properties.

We can then gain access to those properties using the @Value annotation, a bound object via the @ConfigurationProperties annotation, or the Environment abstraction.

17
Q

What Is Spring Boot DevTools Used For?

A

Spring Boot Developer Tools, or DevTools, is a set of tools making the development process easier.

To include these development-time features, we just need to add a dependency to the pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

The spring-boot-devtools module is automatically disabled if the application runs in production. The repackaging of archives also excludes this module by default. So, it won’t bring any overhead to our final product.
By default, DevTools applies properties suitable to a development environment. These properties disable template caching, enable debug logging for the web group, and so on. As a result, we have this sensible development-time configuration without setting any properties.

Applications using DevTools restart whenever a file on the classpath changes. This is a very helpful feature in development, as it gives quick feedback for modifications.

By default, static resources, including view templates, don’t set off a restart. Instead, a resource change triggers a browser refresh. Notice this can only happen if the LiveReload extension is installed in the browser to interact with the embedded LiveReload server that DevTools contains.

18
Q

How to Write Integration Tests?

A

When running integration tests for a Spring application, we must have an ApplicationContext.

To make our life easier, Spring Boot provides a special annotation for testing — @SpringBootTest. This annotation creates an ApplicationContext from configuration classes indicated by its classes attribute.

In case the classes attribute isn’t set, Spring Boot searches for the primary configuration class. The search starts from the package containing the test until it finds a class annotated with @SpringBootApplication or @SpringBootConfiguration.

19
Q

What Is Spring Boot Actuator Used For?

A

Essentially, Actuator brings Spring Boot applications to life by enabling production-ready features. These features allow us to monitor and manage applications when they’re running in production.

Integrating Spring Boot Actuator into a project is very simple. All we need to do is include the spring-boot-starter-actuator starter in the pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Spring Boot Actuator can expose operational information using either HTTP or JMX endpoints. But most applications go for HTTP, where the identity of an endpoint and the /actuator prefix form a URL path.

20
Q

What Basic Annotations Does Spring Boot Offer?

A

The primary annotations that Spring Boot offers reside in its org.springframework.boot.autoconfigure and its sub-packages.

Here are a couple of basic ones:

@EnableAutoConfiguration – to make Spring Boot look for auto-configuration beans on its classpath and automatically apply them
@SpringBootApplication – to denote the main class of a Boot Application. This annotation combines @Configuration, @EnableAutoConfiguration and @ComponentScan annotations with their default attributes.

21
Q

How to Change the Default Port in Spring Boot?

A

We can change the default port of a server embedded in Spring Boot using one of these ways:

Using a properties file – We can define this in an application.properties (or application.yml) file using the property server.port.
Programmatically – In our main @SpringBootApplication class, we can set the server.port on the SpringApplication instance.
Using the command line – When running the application as a jar file, we can set the server.port as a java command argument:
java -jar -Dserver.port=8081 myspringproject.jar

22
Q

Which Embedded Servers Does Spring Boot Support, and How to Change the Default?

A

As of date, Spring MVC supports Tomcat, Jetty and Undertow. Tomcat is the default application server supported by Spring Boot’s web starter.

Spring WebFlux supports Reactor Netty, Tomcat, Jetty and Undertow with Reactor Netty as default.

In Spring MVC, to change the default, let’s say to Jetty, we need to exclude Tomcat and include Jetty in the dependencies:

23
Q

Why Do We Need Spring Profiles?

A

When developing applications for the enterprise, we typically deal with multiple environments such as Dev, QA and Prod. The configuration properties for these environments are different.

24
Q

What is Loose Coupling ?

A
25
Q

What is a Dependency ?

A
26
Q

What is IOC

A
27
Q

What is Dependency Injection ?

A
28
Q

Dependency Injection examples

A
29
Q

What is AutoWiring ?

A
30
Q
A