Detailed explanation of springboot annotations

code annotation

@RestController: indicates that the current class is a control class, which is developed based on the restfull style provided by springboot. This annotation is a combined annotation
composed of @Controller and @ResponseBody. If the current class indicates RestController, the returned data is returned in json format .

@RequestMapping: indicates the access method path, mainly to provide routing selection, when accessed through http, it will be mapped to the specified url.


Start the entry class annotation

@SpringBootApplication Spring Boot projects generally have an entry class of *Application, and there will be a main method in the entry class, which is the entry method of a standard Java application. The @SpringBootApplication annotation is the core annotation of Spring Boot, which is actually a composite annotation.

This annotation mainly combines the following annotations:
1. @SpringBootConfiguration: This is the configuration annotation of the Spring Boot project, which is also a combined annotation. In the Spring Boot project, it is recommended to use @SpringBootConfiguration instead of @Configuration, @Configuration annotates this class, which is equivalent to Configuring beans in XML; using the @Bean annotation method is equivalent to configuring beans in XML.
2. @EnableAutoConfiguration: Enable automatic configuration, this annotation will make Spring Boot automatically configure the configuration items of the project according to the jar package that the project depends on: For example: we The dependency of spring-boot-starter-web is added, and the dependency of SpringMVC will be introduced into the project, and Spring Boot will automatically configure tomcat and SpringMVC

3. @ComponentScan: By default, the sibling directory of the class where @SpringBootApplication is located and all its subdirectories are scanned.

@SpringBootApplication can exclude some configuration on initialization. For example, if you need to exclude configurations such as Redis, you only need to add @SpringBootApplication(exclude = RedisAutoConfiguration.class) to @SpringBootApplication. This way redis will not be injected at startup.

Introduce external resource annotations

@ImportResource: To import an external custom configuration file, such as a custom redis-context.xml configuration file, you need to import and use @ImportResource({"classpath:redis-context.xlm"}).

@ConfigurationProperties: Load property configuration by introducing properties in the application.properties configuration file. For example, get the durid database configuration through the configuration file, @ConfigurationProperties(prefix = "spring.datasource.druid")


Conditional annotation

The power of Spring Boot lies in the use of a new feature of the Spring 4 framework: the @Conditional annotation, which enables some configuration to be enabled only when certain conditions are met. The usage is described in Spring Boot's org.springframework.boot.autoconfigure.condition package, the following are based on Condtional annotations.

@ConditionalOnBean: Initialized when the specified bean exists in the container.
@ConditionalOnClass: Initialized when the specified class is on the classpath.
@ConditionalOnExpression: When based on Spel expression as a judgment condition.
@ConditionalOnJava: Based on the JVM version as a judgment condition.
@ConditionalOnJndi: Find the specified location if jndi exists.
@ConditionalOnMissingBean: When there is no specified bean in the container.
@ConditionalOnMissingClass: When the specified class is not on the classpath.
@ConditionalOnNotWebApplication: when the project is not a web project.
@ConditionalOnProperty: When the specified property has the specified value.
@ConditionalOnResource: Whether the class path has the specified value.
@ConditionalOnSingleCandidate: When the specified bean has only one or more in the container, but the preferred bean is specified.
@ConditionalOnWebApplication: When the project is a web project.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325576432&siteId=291194637