Spring Boot Interview Questions: What is Spring Boot? Difference between Spring Boot and Spring Cloud What features does Spring Boot have? Priority of Spring Boot configuration files
1. What is Spring Boot?
Spring Boot is the scaffolding for Spring, and you can use Spring Boot to focus on developing Spring applications without having to worry too much about XML configuration. Enables developers to develop individual microservices in a very short time. Spring Boot uses the philosophy of “habit over configuration.” In short, it provides a bundle of dependencies that are packaged and already solve the dependency problem according to usage habits. Using Spring Boot can get enterprise projects up and running quickly with little or no Spring configuration.
Core features of Spring Boot:
- Standalone Spring projects: Spring Boot can be run independently as a JAR package.
- Embedded Servlet containers: Spring Boot can choose to embed Tomcat, Jetty, or Undertow without deploying projects as war packages.
- Simplified Maven configuration: Spring provides recommended base POM files to simplify Maven configuration.
- Automatic Configuration of Spring: Spring Boot automatically configures the Spring framework based on project dependencies, greatly reducing the configuration to be used by projects.
- Production-ready functionality: Provides functionality that can be used directly in the production environment, such as performance metrics, application information, and application health checks.
- No code generation and XML configuration: Spring Boot does not generate code. You can implement all of Spring’s configurations without any XML configuration at all.
2. The difference between Spring Boot and Spring Cloud
- Spring Boot is a set of fast configuration scaffolding for Spring, which can be used to quickly develop individual microservices. Spring Cloud is a Cloud application development tool based on Spring Boot implementation
- Spring Boot focuses on a single entity for fast and easy integration, while Spring Cloud is a global service governance framework.
- Spring Boot uses the concept of default being greater than configuration. Many integration solutions have already been selected for you. A large part of Spring Cloud is based on Spring Boot.
Spring Boot can use development projects independently of Spring Cloud, but Spring Cloud can't do without Spring Boot
Is a dependent relationship.
3. What are the features of Spring Boot
Spring Boot has two main features: startup dependency and auto assembly.
< span style = “box-sizing: border-box; color: RGB (74, 74, 74); line-height: 20px; font-size: 14px! Important; word-break: inherit! Important;”
In the poM file of Spring-boot-Dependencies, there are a number of dependencies, and the version number of each dependency is defined.The version number is defined according to the SpringBoot version number. Different Versions of The SpringBoot version depend on different versions.
This is a solution to the problem of conflicting dependency versions in our original project to actually manage all dependency versions in the Spring Boot application.
In addition to inheriting parent dependencies, our project also needs to introducespring-boot-starter-web
Dependencies, which help us import the components that the Web module depends on to function properly, and the versions of these dependencies are managed by the parent project. This one also references tomcat dependencies, and we can start the project through the Tomcat port of auto-assembly.
Conclusion: In terms of starting dependencies, Spring Boot helps us manage the versions of each dependency, so that there will be no version conflict of each dependency. In addition, it helps us package various dependencies so that we do not need to import a large number of dependencies as before, as long as we introduce the coordinates of starting dependencies can be carried out web development, also reflects the use of dependency transfer.
Automatic assembly: Automatic assembly is also a feature of SpringBoot. After creating a New SpringBoot project, you will find the @SpringBootApplication annotation on the boot class, and SpringBoot is automatically assembled by this annotation. Click on the @SpringBootApplication annotation to see something like this:
As you can see, the @SpringBootApplication annotation is made up of three annotations,@SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan
. The three annotations, @ComponentScan, are used to configure the path for the Spring container to scan for objects in the package in which the class is currently started.If you want to use classes from other services in this service, add them after the @SpringBootApplication annotation (scanBasePackages = {" other package paths: com.aaa.bbb", "package paths "}).
Next, look at the @springBootConfiguration annotation:There’s nothing in there. In fact, the @SpringBootConfiguration annotation is the same as the @Configuration annotation, and the @Configuration annotation is pretty familiar. Classes that use the @Configuration annotation, This means that the class is created by the SpringBoot beanConfiguration file class
. The startup class annotated with @SpringBootConfiguration is itself a configuration class for the IOC container.
The final annotation is @enableAutoConfiguration. This annotation is the key annotation for SpringBoot autoconfiguration. Click here to see:As you can see, the annotation using the @ @ EnableAutoConfiguration AutoConfigurationPackage annotation, also import a AutoConfigurationImportSelector class, First look at @ AutoConfigurationPackage annotations, the annotations is actually import a AutoConfigurationPackages. The Registrar. Class components, the following figure:
You can see that this class has two methods. The first is registerBeanDefinitions, which, as the name implies, is used to register beans.The Register method scans the main configuration class package and its subpackages for components registered with the IOC container.
That’s what the @AutoConfigurationPackage annotation does.
Then look at the import AutoConfigurationImportSelector class is what to do, we only focus on getCandidateConfigurations () this method:Create a breakpoint on the first line of the method:Configurations you can see that you get the full set of class names. Where are these configuration classes scanned? “> < div style =” box-check: none; box-check: none; box-check: none
This makes it clear that autowiring scans these configuration classes and assembles them.
Take a look at how the following class is assembled:The annotation marked below is the assembly configuration file:Click on the ServerProperties class to see:As you can see, this class reads configuration files starting with server, such as server.port, server.address, etc. These configuration files are in the current package spring-configuration-metadata.json:Click on it to see a lot of JSON data:Search for server.port, server.address:
Port is the tomcat default port number. DefaultValue is 8080 by default.
Conclusion: Automatic configuration, first thanks to the ability to scan components of the main configuration class package and its subpackages, register these components in the IOC container, second it can use the spring framework’s original SpringFactoriesLoader support, Load meta-INF/Spring. factories to get the full class name of the component and reflect it to the corresponding IOC container Configuration class with @Configuration in JavaConfig form that meets @Conditional requirements. It then initializes the configuration with some default values.
4. Priority of the Spring Boot configuration file
Every Spring Boot project depends on spring-boot-starter-parent. Spring-boot-starter -parent
Yml -> YAMl -> properties. This is where the configuration file is loaded. The configuration file can start with application and end with yML, yaml, and properties. Server. port = 8081 / yML/properties / 8082 / server.port = 8082 It overwrites the configuration in YML.
At the end
If you think my article is helpful to you, please pay attention to my wechat official account :” A happy and painful programmer “(no advertising, simply share original articles, pJ practical tools, a variety of Java learning resources, looking forward to common progress with you).