Greetings to all. This article will cover the basic questions on the topic of "Spring Boot". It is aimed at beginning developers, and can be useful in preparing for an interview. It turned out to be quite compact compared to other articles.
- Dependency injection, container, IoC, beans
- AOP (aspect-oriented programming)
- JDBC, Transactions, JPA, Spring Data
- Spring boot
- Spring mvc
- Spring security
- REST
- Testing
Questions
Spring Boot is a set of pre-configured modules that work in the Spring Framework and simplify the configuration of a Spring application.
Here are some central modules:
- Spring-boot-dependencies. Contains version dependencies.
- Spring-boot-starter-parent. Parent pom.xml
- Spring-boot-starters. Parent module for starters
- Spring-boot-autoconfigure. Contains modules for auto tuning starters
- Spring-boot-actuator. Allows you to monitor the application and manage its creation
- Auto-configure many settings to avoid boilerplate code
- Allows you to easily configure the application when the default settings do not suit
- Spring Boot project can compile to standalone JAR file
- Provides a set of dependencies that are already tested to work correctly with each other
- It has built-in web containers that make it easy to test the application.
It assumes what you need based on the dependencies in the classpath. “Configuration Agreement” - it will autoconfigure Spring with specially selected settings, which can then be overridden.
There are several annotations for this, @ConditionalOnClass
, @ConditionalOnBean
, @ConditionalOnMissingBean
and @ConditionalOnMissingClass
, which allow you to apply conditions to @ Configuration classes and @ Bean methods in these classes.
For example:
A bean will only be created if a specific dependency is in the classpath.
Use
@ConditionalOnClass
, setting the class name from classpath there.
A bean will be created only if there is no bean of this type or with that name in the container.
Use
@ConditionalOnMissingBean
, setting the name or type of the bean to check there.
There is a list of condition annotations, each of which is used to control the creation of beans. Here is a list of such annotations (actually there are more):
annotation
| Condition for passing
|
@ConditionalOnClass
| Class presence in classpath
|
@ConditionalOnMissingClass
| Missing class in classpath
|
@ConditionalOnBean
| The presence of a bin or its type (bean class)
|
@ConditionalOnMissingBean
| Lack of a bin or its type
|
@ConditionalOnProperty
| Presence of Spring Property
|
@ConditionalOnResource
| The presence of a resource file
|
@ConditionalOnWebApplication
| If this is a web application, WebApplicationContext will be used.
|
@ConditionalOnNotWebApplication
| If it is not a web application
|
Spring Boot uses a special order of PropertySource to allow overriding property values. Here is the order of the sources to get the properties of the application:
- General settings from ~ / spring-boot devtools.properties directory
- Settings from
@TestPropertySource
annotation from tests - Attributes @ SpringBootTest # properties
- Command line arguments
- Properties from SPRING_APPLICATION_JSON
- Parameters for initializing ServletConfig
- Parameters for initializing ServletContext
- JNDI attributes from java: comp / env
- Java System Properties (System.getProperties ())
- OS variables
- RandomValueProperySource
- Properties for profiles, for example YAML or application- {profile} .properties
- application.properties and yaml not from your jar
- application.properties and yaml from your jar
-
@PropertySource
on @ Configuration class - Property by default (set via
SpringApplication.setDefaultProperties()
)
Also add that property.yml always override property.properties.
The @SpringBootApplication
provides 3 properties:
-
@EnableAutoConfiguration
- enables Spring Boot's auto-configuration mechanism -
@ComponentScan
- enables component scanning in the package of the class where it is located -
@Configuration
- allows you to register additional. beans in context
Here are other key annotations:
@EnableConfigurationProperties
- allows you to use @ConfigurationProperties
with the @ConfigurationProperties
annotation
@ConfigurationProperties
- allows you to associate files from files with bins
@WebMvcTest
- used for Spring MVC tests
@SpringBootTest
- used when you need Spring Boot functions in tests
@DataJpaTest
- used to test JPA components
An embedded container is a server that comes with the final application, while a WAR is an archive that can be deployed to an external server.
Servlet containers are good for managing multiple applications on the same host, but they are not very useful for managing just one application.
With a cloud environment using one application per virtual machine is the preferred and more common way. Modern frameworks want to be more compatible with the clouds, so they are switching to embedded containers.
Spring Boot supports Tomcat, Jetty, and Undertow.
The default is TomCat. In order to change the container, just add the desired dependency in pom.xml.
It allows you to use autoconfiguration. Autoconfiguration in Spring Boot tries to create and configure beans based on dependencies in the classpath, in order to allow the developer to quickly start working with various technologies and remove the boilerplate code.
Yes, it does if there is an @SpringBootApplication
annotation that contains an @ComponentScanning
annotation.
By default, Spring searches for beans in the same package as the annotated class. This can be overridden by specifying classes (or package) in the scanBasePackageClasses
or scanBasePackage
.
Starters provide a set of handy dependency descriptors that you can include in your application. You get one source for spring and related technologies without having to search and copy-paste deployment descriptors.
For example, if you want to get started with Spring JPA, just add the spring-boot-starter-data-jpa dependency to your project.
Starters contain most of the dependencies you need to run the project, work quickly and consistently, and support sets of transitive dependencies.
Yes. By default, messages with priorities ERROR, WARN, INFO will be displayed in the application. To enable the output of DEBUG or TRACE levels, you must use the --debug
/ --trace
or set the property properties debug / trace = true.
logging.level.root = WARN logging.level.org.springframework.web = DEBUG logging.level.org.hibernate = ERROR
By default, Sprin Boot only logs to the console. If you want to log events to a file, you must set the logging.file or logging.path properties to true (for example, in application.properties).
Color logging
In consoles supporting ANSI, to improve readability, messages of different levels can be highlighted with different colors. This is how it is configured in the property file:
logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss}) {yellow}
If you want to write went to other files, you can also configure this (YAML):
logging: path: /var/logs/ file: bookWarn.log level: root: WARN org. springframework: security: DEBUG