Home  >  Article  >  Java  >  What are the 25 core annotations of Spring Boot?

What are the 25 core annotations of Spring Boot?

hzc
hzcOriginal
2020-06-22 11:55:163506browse

What are the 25 core annotations of Spring Boot?

The 25 core annotations of Spring Boot

1, @SpringBootApplication

This is the most core annotation of Spring Boot. It is used on the Spring Boot main class to identify this as a Spring Boot application and is used to enable various capabilities of Spring Boot.

In fact, this annotation is a combination of the three annotations @SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan. You can also use these three annotations. To replace the @SpringBootApplication annotation.

2. @EnableAutoConfiguration

Allow Spring Boot to automatically configure annotations. After turning on this annotation, Spring Boot can configure the annotation according to the current class path. Package or class to configure Spring Beans.

For example: There is the Mybatis JAR package under the current class path, and the MybatisAutoConfiguration annotation can configure each Spring Bean of Mybatis based on relevant parameters.

3, @Configuration

This is an annotation added by Spring 3.0 to replace the applicationContext.xml configuration file. All this configuration files Everything that can be done in it can be registered through the class where this annotation is located.

4. @SpringBootConfiguration

This annotation is a variant of the @Configuration annotation. It is only used to modify the Spring Boot configuration, or to facilitate the subsequent expansion of Spring Boot. .

5, @ComponentScan

This is an annotation added by Spring 3.1 to replace the component-scan configuration in the configuration file and enable component scanning, that is, automatic scanning. The @Component annotation under the package path registers the bean instance into the context.

You can learn more about the first five annotations in this article "Detailed Explanation of the Three Core Annotations of Spring Boot".

6, @Conditional

This is a new annotation added by Spring 4.0. It is used to identify a Spring Bean or Configuration configuration file. The configuration will only be enabled when the specified conditions are met. .

7, @ConditionalOnBean

combination @Conditional annotation, the configuration is only enabled when there is a specified Bean in the container.

8, @ConditionalOnMissingBean

combination@Conditional annotation, and the opposite of @ConditionalOnBean annotation, when there is no specified in the container Bean configuration is enabled.

9, @ConditionalOnClass

combination @Conditional annotation, the configuration will only be enabled when there is a specified Class in the container.

10, @ConditionalOnMissingClass

combination@Conditional annotation, which is opposite to @ConditionalOnMissingClass annotation, when it is not specified in the container Only the Class can be configured.

11, @ConditionalOnWebApplication

combination @Conditional Note, the configuration can only be enabled when the current project type is a WEB project.

The current project has the following 3 types.

enum Type {    /**     * Any web application will match.     */    ANY,    /**     * Only servlet-based web application will match.     */    SERVLET,    /**     * Only reactive-based web application will match.     */    REACTIVE}

12, @ConditionalOnNotWebApplication

combination@Conditional annotation, contrary to @ConditionalOnWebApplication annotation, the current project type is not Configuration is only enabled for WEB projects.

13, @ConditionalOnProperty

Combination @Conditional Annotation, the configuration is only enabled when the specified property has the specified value.

14, @ConditionalOnExpression

combination @Conditional annotation, the configuration is only enabled when the SpEL expression is true.

15, @ConditionalOnJava

combination @Conditional annotation, the configuration is only enabled when the running Java JVM is in the specified version range.

16, @ConditionalOnResource

combination @Conditional Annotation, the configuration is only enabled when there is a specified resource in the class path.

17, @ConditionalOnJndi

combination @Conditional annotation, the configuration is only enabled when the specified JNDI exists.

18, @ConditionalOnCloudPlatform

combination @Conditional Note, the configuration is only enabled when the specified cloud platform is activated.

19, @ConditionalOnSingleCandidate

combination@Conditional annotation, when the specified class has only one Bean in the container, or there are multiple but Only enable configuration when it is preferred.

20, @ConfigurationProperties

is used to load additional configurations (such as .properties files), which can be used in the @Configuration annotation class, or@Bean Above the annotation method.

For the usage of this annotation, please refer to the article "Several Ways for Spring Boot to Read Configuration".

21. @EnableConfigurationProperties

is generally used in conjunction with the @ConfigurationProperties annotation to enable the @ConfigurationProperties annotation configuration. Bean support.

22、@AutoConfigureAfter

用在自动配置类上面,表示该自动配置类需要在另外指定的自动配置类配置完之后。

如 Mybatis 的自动配置类,需要在数据源自动配置类之后。

@AutoConfigureAfter(DataSourceAutoConfiguration.class)public class MybatisAutoConfiguration {

23、@AutoConfigureBefore

这个和 @AutoConfigureAfter 注解使用相反,表示该自动配置类需要在另外指定的自动配置类配置之前。

24、@Import

这是 Spring 3.0 添加的新注解,用来导入一个或者多个 @Configuration 注解修饰的类,这在 Spring Boot 里面应用很多。

25、@ImportResource

这是 Spring 3.0 添加的新注解,用来导入一个或者多个 Spring  配置文件,这对 Spring Boot 兼容老项目非常有用,因为有些配置无法通过 Java Config 的形式来配置就只能用这个注解来导入。

推荐教程: 《java教程

The above is the detailed content of What are the 25 core annotations of Spring Boot?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn