Spring Boot Flashcards
What is Spring Boot and what are its main features?
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
What is the difference between Spring and Spring Boot?
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 does Spring Boot bootstrap the application?
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.
Which is the main maven dependency for Spring Boot application and why?
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.
What is Spring Initializr?
It is a quick and convenient way to create the skeleton of Spring boot project.
Steps:
- Go to start.spring.io or choose Spring starter project in IDE with STS plugin.
- 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.
- The project contains Tomcat server and will be auto-configured.
What are the Spring Boot starters?
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 can we disable a specific auto configuration?
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
What does the spring-boot-maven-plugin do?
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.
What Does it Mean that Spring Boot Supports Relaxed Binding?
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.
What is Spring Boot Devtools Used For?
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 to write integration tests for spring boot application?
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.
What Is Spring Boot Actuator?
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
What are the basic annotations offered by Spring Boot?
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 can we change the default port in spring boot?
One of three ways:
- By using a properties file – either application.properties (or application.yml) with property server.port
- Programmatically – in our main @SpringBootApplication class, we can set the server.port on the SpringApplication instance
- By using the command line – when running the application as a jar file
java -jar -Dserver.port=8081 myspringproject.jar
Which embedded server does Spring boot support by default and can we change it?
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.