Preparing for Spring Professional Certification. Spring boot

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.







Table of contents
  1. Dependency injection, container, IoC, beans
  2. AOP (aspect-oriented programming)
  3. JDBC, Transactions, JPA, Spring Data
  4. Spring boot
  5. Spring mvc
  6. Spring security
  7. REST
  8. Testing











Questions



What is spring boot?

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


What are the benefits of Spring Boot?
  • 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.


How does he work? How does he understand what and how to configure?

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.









What affects the configuration of Spring Boot?

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



How are property defined? Where is the default PropertySource located in Spring Boot?

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:







  1. General settings from ~ / spring-boot devtools.properties directory
  2. Settings from @TestPropertySource



    annotation from tests
  3. Attributes @ SpringBootTest # properties
  4. Command line arguments
  5. Properties from SPRING_APPLICATION_JSON
  6. Parameters for initializing ServletConfig
  7. Parameters for initializing ServletContext
  8. JNDI attributes from java: comp / env
  9. Java System Properties (System.getProperties ())
  10. OS variables
  11. RandomValueProperySource
  12. Properties for profiles, for example YAML or application- {profile} .properties
  13. application.properties and yaml not from your jar
  14. application.properties and yaml from your jar
  15. @PropertySource



    on @ Configuration class
  16. Property by default (set via SpringApplication.setDefaultProperties()



    )


Also add that property.yml always override property.properties.







Describe the main annotations and configs for Spring Boot?

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







What are the differences between an embedded container and a WAR?

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.







What built-in containers does Spring support?

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.







What does @EnableAutoConfiguration do?

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.







Does Spring Boot scan components? Where does he look for them by default?

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



.







What is Spring Boot Starter POM? How can it be useful?

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.







Can you control logging through Spring Boot?

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
      
      






All Articles