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!
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.
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.
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.
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.
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.
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.
Combine @Conditional annotations to enable configuration when there is a specified bean in the container.
Combine @Conditional
annotations to enable configuration when there are no specified beans in the container.
Combine @Conditional
annotations to enable configuration when there is a specified Class in the container.
Combine @Conditional annotations to enable configuration when there is no specified Class in the container.
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
}
Combine @Conditional
annotations to enable configuration when the current project type is not a WEB project.
Combine @Conditional
annotations to enable configuration when the specified property has a specified value.
Combine -@Conditional- annotations to enable configuration when the SpEL expression is true.
Combine the @Conditional
annotation to enable configuration when the running Java JVM is within the specified version range.
Combine @Conditional
annotations to enable configuration when there is a specified resource under the classpath.
Combine @Conditional
annotations to enable configuration when the specified JNDI exists.
Combine @Conditional annotations to enable configuration when the specified cloud platform is active.
Combine @Conditional
annotations to enable configuration when the specified class has only one bean in the container, or if there are multiple but preferred.
Used to load additional configuration (such as .properties files), available on classes annotated with @Configuration
, or on methods annotated with @Bean
.
It is generally used in conjunction with the @ConfigurationProperties
annotation to enable the ability to configure the bean using the @ConfigurationProperties
annotation.
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 {
...
}
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.
This is a new annotation added by Spring 3.0 to import one or more @Configuration
annotation-modified classes.
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