Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codeman688/5d71924abd9e51ed1263e2362d3d095e to your computer and use it in GitHub Desktop.
Save codeman688/5d71924abd9e51ed1263e2362d3d095e to your computer and use it in GitHub Desktop.

An overview of 25 Spring Boot core annotations

We have been learning and applying Spring Boot for some time. How much do you know about Spring Boot annotations? Today I collected the 25 core annotations of Spring Boot, which everyone must master!

1、@SpringBootApplication

This is the most central annotation of Spring Boot. It is used on the Spring Boot main class to identify this as a Spring Boot application that is used to enable Spring Boot. In fact, this annotation is a combination of the three annotations @SpringBootConfiguration, @EnableAutoConfiguration, @ComponentScan. You can also use these three annotations instead of the @SpringBootApplication annotation.

2、@EnableAutoConfiguration

Allow Spring Boot to automatically configure annotations. After opening this annotation, Spring Boot can configure Spring Beans based on packages or classes in the current classpath. For example, if the current classpath has the JAR package of Mybatis, then the @MybatisAutoConfiguration annotation can configure each Spring Bean of Mybatis according to the relevant parameters.

3、@Configuration

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

4、@SpringBootConfiguration

This annotation is a variant of the @Configuration annotation, which is only used to imply that this is for Spring Boot configuration, and the separate extraction is beneficial for subsequent extensions.

5、@ComponentScan

This is an annotation added by Spring 3.1 to replace component-scan in the configuration file. It will turn on component scanning, which automatically scans the classes annotated by @Component under the package path and registers the scanned class as a bean in the applicaton context.

6、@Conditional

This is a new annotation added by Spring 4.0 to identify a Spring Bean or Configuration profile that will be opened when the specified criteria are met.

7、@ConditionalOnBean

Combine @Conditional annotations to enable configuration when there is a specified bean in the container.

8、@ConditionalOnMissingBean

Combine @Conditional annotations to enable configuration when there are no specified beans in the container.

9、@ConditionalOnClass

Combine @Conditional annotations to enable configuration when there is a specified Class in the container.

10、@ConditionalOnMissingClass

Combine @Conditional annotations to enable configuration when there is no specified Class in the container.

11、@ConditionalOnWebApplication

Combine @Conditional annotations to enable configuration when the current project type is a WEB project. There are three possible types of web projects:

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

Combine @Conditional annotations to enable configuration when the current project type is not a WEB project.

13、@ConditionalOnProperty

Combine @Conditional annotations to enable configuration when the specified property has a specified value.

14、@ConditionalOnExpression

Combine -@Conditional- annotations to enable configuration when the SpEL expression is true.

15、@ConditionalOnJava

Combine the @Conditional annotation to enable configuration when the running Java JVM is within the specified version range.

16、@ConditionalOnResource

Combine @Conditional annotations to enable configuration when there is a specified resource under the classpath.

17、@ConditionalOnJndi

Combine @Conditional annotations to enable configuration when the specified JNDI exists.

18、@ConditionalOnCloudPlatform

Combine @Conditional annotations to enable configuration when the specified cloud platform is active.

19、@ConditionalOnSingleCandidate

Combine @Conditional annotations to enable configuration when the specified class has only one bean in the container, or if there are multiple but preferred.

20、@ConfigurationProperties

Used to load additional configuration (such as .properties files), available on classes annotated with @Configuration, or on methods annotated with @Bean.

21、@EnableConfigurationProperties

It is generally used in conjunction with the @ConfigurationProperties annotation to enable the ability to configure the bean using the @ConfigurationProperties annotation.

22、@AutoConfigureAfter

Used in the auto-configuration class, it means that the auto-configuration class needs to be configured after the other specified auto-configuration class is configured. For example, Mybatis's auto-configuration class needs to be after the data source auto-configuration class.

@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration {
  ...
}

23、@AutoConfigureBefore

Used in the auto-configuration class, it means that the auto-configuration class needs to be configured before the other specified auto-configuration class is configured.

24、@Import

This is a new annotation added by Spring 3.0 to import one or more @Configuration annotation-modified classes.

25、@ImportResource

This is a new annotation added by Spring 3.0 to import one or more Spring configuration files, which is very useful for Spring Boot compatible old projects, because some configurations cannot be configured with Java Config and can only be imported with this annotation.

This article was archived on github. https://github.com/codeman-cs/SpringBoot/wiki/An-overview-of-25-Spring-Boot-core-annotations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment