Spring Boot Flashcards

1
Q

What is Spring Boot and what are its main features?

A

Spring Boot is a framework for rapid application development, built on top of Spring framework. It provides

  • auto configuration
  • embedded Tomcat server
  • Spring Initializr to quickly create skeleton of project
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between Spring and Spring Boot?

A

Spring framework provides necessary plumbing to develop Java applications so that there is minimum configuration. But as application grows, so do the dependencies and configurations, hence we have spring boot.

Spring Boot is an extension of Spring framework for rapid application development by providing auto-configuration of dependencies.

With Spring, we need to provide at least spring-web and spring-webmvc dependencies to build a web application. With Spring Boot, we only need to provide spring-boot-starter-web dependency.

Spring requires defining the dispatcher servlet, mappings, and other supporting configurations. We can do this using either the web.xml file or an Initializer class. Spring Boot provides auto configuration.

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

How does Spring Boot bootstrap the application?

A

The entry point of a Spring Boot application is the class which is annotated with @SpringBootApplication.

The embedded Tomcat server runs the psv main method and takes care of binding of the servlet, filter and ServletContextInitializer beans from application context to embedded servlet container.

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

Which is the main maven dependency for Spring Boot application and why?

A

spring-boot-starter-parent adds all the necessary jars to classpath automatically. It also provides a dependency-management section so that we can omit the version tag for other dependencies.

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

What is Spring Initializr?

A

It is a quick and convenient way to create the skeleton of Spring boot project.

Steps:

  1. Go to start.spring.io or choose Spring starter project in IDE with STS plugin.
  2. Select dependency management tool (either Maven or Gradle), a language (Java, Kotlin or Groovy), a packaging scheme (Jar or War), version and dependencies and create the project.
  3. The project contains Tomcat server and will be auto-configured.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the Spring Boot starters?

A

A spring boot starter is a one-stop-shop for all the dependencies under org.springframework.boot providing auto dependency resolution and their names start with spring-boot-starter.

Currently there are 50+ starters such as

  • spring-boot-starter: core starter, including auto-configuration support, logging, and YAML
  • spring-boot-starter-aop: starter for aspect-oriented programming with Spring AOP and AspectJ
  • spring-boot-starter-data-jpa: starter for using Spring Data JPA with Hibernate
  • spring-boot-starter-security: starter for using Spring Security
  • spring-boot-starter-test: starter for testing Spring Boot applications
  • spring-boot-starter-web: starter for building web, including RESTful, applications using Spring MVC
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we disable a specific auto configuration?

A

We can either use the exclude attribute of @EnableAutoConfiguration annotation:

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class MyConfiguration { }

or we can set it in application.properties file in Spring Boot application by providing fully configured name of the property to the spring.autoconfigure.exclude key:

spring. autoconfigure.exclude=org.springframework.boot.
autoconfigure. jdbc.DataSourceAutoConfiguration

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

What does the spring-boot-maven-plugin do?

A

The plugin provides Spring Boot support in Maven, allowing us to package a web application as an executable jar/war file, containing all the dependencies and embedded server to run an application “in-place”. It has following goals:

spring-boot:run
spring-boot:repackage
spring-boot:start and spring-boot:stop
spring-boot:build-info

If we do not provide the packaging, it defaults to jar.

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

What Does it Mean that Spring Boot Supports Relaxed Binding?

A

It means that the key of a property doesn’t need to be an exact match of a property name. Such an environment property can be written in camelCase, kebab-case, snake_case, or in uppercase with words separated by underscores.

For example, if a property in a bean class with the @ConfigurationProperties annotation is named myProp, it can be bound to any of these environment properties: myProp, my-prop, my_prop, or MY_PROP.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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 and are included with spring-boot-devtools dependency.

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.

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

How to write integration tests for spring boot application?

A

Spring boot provides a class-level annotation- @SpringBootTest which creates an ApplicationContext from configuration classes indicated by its classes attribute or the primary configuration class annotated with @SpringBootApplication or @SpringBootConfiguration.

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

What Is Spring Boot Actuator?

A

Spring Boot Actuator exposes operational information about the running application by providing production-ready features to the application without having to actually implement them manually. It allows us to

  • monitor the app
  • gather the metrics
  • understand the traffic, or the state of database

It uses HTTP endpoints or JMX beans to enable us to interact with it. For actuator to be enabled, we need to add spring-boot-starter-actuator dependency to the POM file.

By default, we can only see /health and /info and all actuator endpoints are placed under /actuator path. We can enable other endpoints by providing management.endpoints.web.exposure.include=* property in properties file.

For eg. we can check application health by hitting the link localhost:8080/actuator/health.

Below are some of the predefined endpoints:

/beans returns all available beans in our BeanFactory
/env returns the current environment properties
/health summarizes the health status of our application
/metrics details metrics of our application

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

What are the basic annotations offered by Spring Boot?

A

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

The basic ones are:

@EnableAutoConfiguration – to make Spring Boot look for auto-configuration beans on its classpath and automatically apply them.

@SpringBootApplication – used to denote the main class of a Boot Application. It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.

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

How can we change the default port in spring boot?

A

One of three ways:

  1. By using a properties file – either application.properties (or application.yml) with property server.port
  2. Programmatically – in our main @SpringBootApplication class, we can set the server.port on the SpringApplication instance
  3. By using the command line – when running the application as a jar file
    java -jar -Dserver.port=8081 myspringproject.jar
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which embedded server does Spring boot support by default and can we change it?

A

Spring boot by default supports tomcat but can also use Jetty and Undertow.

To change the default server, we need to exclude the Tomcat dependency from spring-boot-starter-web and include the new server dependency.

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

What are spring boot profiles and why do we need them?

A

Spring boot profiles provide us a way to deal with multiple environments such as dev, qa and prod and managing their config properties.

For this, Spring has the provision of profiles, to help separate the configuration for each environment. So that instead of maintaining this programmatically, the properties can be kept in separate files such as application-dev.properties and application-prod.properties.

The default application.properties points to the currently active profile using spring.profiles.active so that the correct configuration is picked up.

spring.profiles.active=dev

17
Q

What is Thymeleaf?

A

It is a server-side Java template engine for both web and standalone environments. It is ideal for HTML5 web development.

HTML templates written in Thymeleaf still look and work like HTML.

To use thymeleaf, we need to add spring-boot-starter-thymeleaf dependency in pom file.

18
Q

How can we enable HTTP/2 support in Spring Boot?

A

By providing the

server.http2.enabled=true

property in application.properties file