Spring Boot Flashcards
What is Spring Boot?
Spring Boot is a set of pre-configured frameworks designed to reduce boilerplate configuration (infrastructure). Spring Boot provides OOTB ready to use infrastructure beans.
What are the advantages of using Spring Boot?
Most important advantage is easy configuration of Spring application.
Additional to this it provides something more:
- Integrated web containers that allow for easy testing
- Starters – sets of dependencies that help setting up a “typical” application
- Little or no configuration
- Decrease amount of boilerplate code
Spring boot allows:
1) Setting up the infrastructure for a Spring project in a really short time
2) Providing a default common infrastructure configuration but allowing easy divergence from the default as the application grows. So any default configuration can easily be overridden.
3) Providing a range of nonfunctional features common to a wide range of projects (embedded servers, security, metrics, health checks, externalized configuration).
4) Remove the need for XML and code configuration.
Features
* Create stand-alone Spring applications
* Embed Tomcat, Jetty or Undertow directly (no need to
deploy WAR files)
* Provide opinionated ‘starter’ POMs to simplify your
Maven configuration
* Automatically configure Spring whenever possible
* Provide production-ready features such as metrics,
health checks and externalized configuration
* Absolutely no code generation and no requirement
for XML configuration
Why is Spring Boot “opinionated”?
It is opinionated, because it provides a default common infrastructure configuration. It makes assumptions on what you need based on dependencies from the classpath.
How does it work? How does it know what to configure?
It scans dependencies from classpath and adds support for different components by initializing “maybe” required beans. For example if it sees a dependency on Thymeleaf, 3 beans that Thymeleaf uses are automatically initialized:
ThymeleafViewResolver
SpringTemplateEngine
TemplateResolver
Moreover it also scans the templates directory from resources for Thymeleaf templates.
What things affect what Spring Boot sets up?
Specified dependencies – only if @EnableAutoConfiguration or @SpringBootApplication annotations are used.
@EnableAutoConfiguration is a specific Spring Boot annotation that has the purpose to enable Spring Application Context, attempting to ques and configure beans that you are likely to need based on the specified dependencies. This annotation works well with Spring-provided starter dependencies, but it is not directly tied to them, so other dependencies outside the starters can be used. I.e. if there is a specific embedded server on the classpath, this will be used unless there is another EmbeddedServletContainerFactory configuration in the project.
@SpringBootApplication is a top-level annotation designed to use only at class level, it is equivalent to declaring the following three annotations: @Configuration, @EnableAutoConfiguration, @ComponentScan
How are properties defined? Where?
Properties may be defined either in .properties format, or in YAML format.
Default Spring names for properties files are: application.properties and application.yml.
Those default names may be changed to custom ones by setting the System property “spring.config.name” to the required property file name.
The property file mus be placed in one of the following locations relatively to the class that will use it:
1) /config subdirectory of the current directory
2) the current directory
3) classpath config package
4) the classpath root (resources package)
Would you recognize common Spring Boot annotations and configuration properties if you saw them in the exam?
@EnableAutoConfiguration - enables Spring Application Context to attempt to ques and configure beans based on the specified dependencies. This annotation works well with Spring-provided starter dependencies, but it is not directly tied to them, so other dependencies outside the starters can be used. I.e. if there is a specific embedded server on the classpath, this will be used unless there is another EmbeddedServletContainerFactory configuration in the project.
@SpringBootApplication is a top-level annotation designed to use only at class level, it is equivalent to declaring the following three annotations: @Configuration, @EnableAutoConfiguration, @ComponentScan
@ConfigurationProperties - can be used on a class that loads and holds the YAML or *.properties values. This annotation also provides the possibility to validate the external properties and define a prefix for the properties. So if there are multiple prefixes, multiple classes can be defined.
@EnableConfigurationProperties - can be used on a Configuration class. With @ConfigurationProperties annotated class name as argument it enables support for latest.
What is the difference between an embedded container and a WAR?
Embedded container is a server that comes with the resulting application whereas WAR is an archive that can be deployed on an external container.
What embedded containers does Spring Boot support?
TomCat
Jetty
Undertow
What is a Spring Boot starter POM? Why is it useful?
Starter POM is a set of spring starter dependencies.It should be defined as a parent in a Maven project to enable Spring Boot.
Can you control logging with Spring Boot? How?
Logging is initialized before the application context, so it is impossible to control logging from using @PropertySources in @Configuration classes. System properties and conventional Spring Boot external configuration files should be used.
Depending on the logging system that is used, Spring Boot will look for the specific configuration files.
The logfile name to use by default by Spring Boot can be configured using the “logging.file” Spring Environment variable.