Interview questions-10SpringBoot

SpringBoot

SpringBoot principle (automatic assembly)

1. First, start the startup class through the SpringApplication.run() method. His principle is that the run() method refreshes the container through refreshContext(context), and then explains the @SpringBootApplication
annotation and injects =bean into the container Inside, then we will parse the springApplication class we wrote. springApplication
itself is a configuration class because there is an annotation inside the @SpringBootApplication annotation that is the @SpringBootConfiguration configuration class.
It inherits the @Configuration configuration class indicating that the annotated class is a configuration class
2 There is an annotation inside @SpringBootApplication called @ComponentScan. Its function is to scan all packages in this class and the same directory.
4. Then the final internal annotation is @EnableAutoConfiguration. This annotation is the key to automatic assembly. Among the annotations in it are An annotation is called @import,
which means to load the class AutoConfigurationImportSelector. When this class is loaded, it will call a method called selectImports.
He has a method
called getCandidateConfigurations, which is scanned by SpringFactoriesLoader.loadFactoryNames() For all jar packages with META-INF/spring.factories, there is a spring.factories file in spring-boot-autoconfigure-xxxxjar. It stores
key -value pairs in the form of kay-value
One of the keys is EnableAutoConfiguration. All automatic assembly will use this key to find all classes ending in AutoConfiguration and then assemble. There are many automatic configuration classes. Springboot is not so stupid. It advocates loading on demand.
It will remove the duplicate classes-filter out the classes that we have configured with the exclude annotation, and the remaining auto-configuration classes need to meet certain conditions if they are to be effective. If these conditions are met, spring
boot is realized through conditional annotations. There are many conditional annotations in the AutoConfiguration class. It will automatically inject the corresponding beans into the container according to the jar package you imported and the beans in the container, so automatic assembly is realized

What are the spring annotations?

@Configuration regards a class as an IoC container, and if @Bean is registered on one of its method headers, it will be used as a Bean in the Spring container.
@Scope annotation scope @Lazy(true) means lazy initialization @Service is used to annotate business layer components,
@Controller is used to annotate control layer components (such as actions in struts) @Repository is used to annotate data access components, namely DAO components.
@Component refers to components in general. When components are not easily classified, we can use this annotation to mark them. @Scope is used to specify the scope of the scope (used on the class)
@PostConstruct is used to specify the initialization method (used on the method) @PreDestory is used to specify the destruction method (used on the method)
@DependsOn: define Bean initialization and destruction order
@Primary: Bean multiple candidates when there is an automatic assembly, is annotated as @Primary Bean will serve as the first choice for those who would otherwise throw an exception @Autowired
by type assembly, if we want to use the default assembly by name, you can Used in conjunction with @Qualifier annotations. It is as follows: @Autowired
@Qualifier("personDaoBean") There are multiple instances for use.
@Resource is assembled by name by default. When no bean matching the name is found, it will be assembled by type. @PostConstruct Initialize annotation
@PreDestroy Destroy annotation default singleton start and load @Async asynchronous method call

What is Spring Boot?

Spring Boot is a sub-project under the Spring open source organization. It is a one-stop solution for Spring components. It mainly simplifies the
difficulty of using Spring , saves heavy configuration, provides various starters, and developers can quickly get started.

What are the advantages of Spring Boot?

Spring Boot has the following advantages:
1. Easy to use, improve development efficiency, and provide a faster and more extensive introductory experience for Spring development.
2. Use out of the box, away from tedious configuration.
3. Provides a series of non-business functions common to large-scale projects, such as: embedded server, security management, operation data monitoring, operation status check and external configuration, etc.
4. No code generation and no need for XML configuration.
5. Avoid a large number of Maven imports and various version conflicts.

What is the core annotation of Spring Boot? Which annotations are mainly composed of?

The annotation on the startup class is @SpringBootApplication, which is also the core annotation of Spring Boot. The main combination includes the following three annotations:
@SpringBootConfiguration: Combines the @Configuration annotation to realize the function of the configuration file.
@EnableAutoConfiguration: Turn on the automatic configuration function, you can also turn off a certain automatic configuration option, such as turning off the data source automatic configuration function:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class
}). @ComponentScan: Spring component scanning.

Guess you like

Origin blog.csdn.net/zyf_fly66/article/details/114082918