This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Awareness of the annotations in the @SpringBootApplication
@SpringBootApplication: Indicates a Configuration class that declares one or more @Bean methods and triggers autoconfiguration and component scanning, equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan
SpringbootApplication annotations are as follows, do one by one analysis:
-
@target (elementType.type) : Indicates the context in which the annotation TYPE is applicable (i.e., the Target of the annotation). In this case, interface, class, enumeration, annotation
-
@Retention(retentionPolicy.runtime) : Indicates how long annotations with an annotation type should be retained. In this case, annotations will be protected by the JVM
So it is read and used at run time by the JVM or other code that uses reflection
-
By default, annotations with types will be Documented by Javadoc and similar tools
-
@inherited indicates automatic inheritance of annotation types (that is, children can inherit from their parents)
The next three annotations are the heart of the @SpringBootApplication
- @ SpringBootConfiguration:
Official explanation: Indicates a class that provides the Spring Boot application @Configuration. Can be used as an alternative to Spring’s standard @Configuration annotation so that Configuration can be found automatically (for example in tests).
The application should contain only one @SpringBootConfiguration, which most idiomatic SpringBoot applications will inherit from @SpringBootApplication. (Simple understanding: @SpringBootConfiguration, which actually identifies a Configuration class, @configuration identifies an annotated class in Spring, In springBoot, it uses @SpringBootConfiguration as an alternative. If I click on the source code, I see that it has @Configuration annotation.
- EnableAutoConfiguration: Auto configuration, try to guess and configure the beans you might need
@ import import one or more @ the Configuration class, allowing the import @ the Configuration class, ImportSelector and importbeandefinitionregistry implementation, And conventional component class, similar to AnnotationConfigApplicationContext. Register, the @autowired injection should be used to access the import @ @ Bean in the Configuration class definition (Populically, this means loading qualified classes into the IOC container)
- @ComponentScan
You can specify basepackagclasses or basePackages to define specific classes or packages to scan. If not specified, scan from the package of the class that declared the annotation ().
The @ComponentScan annotation will scan all components under the same package as the @SpringBootApplication annotation, as shown in the figure below: Everything under the Resume package is scanned
Further? If there are calls to multiple modules, for example, in order to reduce the redundancy of code, I create a new module and put some common classes, such as unified interception and processing of exceptions thrown by the controller layer and unified processing of data returned by the controller layer. If every module is written once, this code will be repeated. How do you do that as a single common module? The @ComponentScan annotation will only scan all components in the @SpringBootApplication annotation, so @ComponentScan should be annotated separately. At this point, the outside annotation will override @ComponentScan in the @SpringBootApplication annotation. The @ComponentScan annotation will be more flexible
As shown in the figure below: The WxResume module processes the data returned by the Controller layer in a unified way using the UnifiedAdivice of the PublicAPI module, so it is necessary to scan the classes under the Response package in the PublicAPI module when starting the WxResume module
How do you scan it in? Add @ComponentScan to the boot class of the WxResume module as shown below:
@EnableEurekaClient @MapperScan("com.flscode.resume.mapper") @ComponentScan({"com.flscode.publicapi.Response","com.flscode.resume"}) @SpringBootApplication public class WxresumeApplication { public static void main(String[] args) { SpringApplication.run(WxresumeApplication.class,args); }}Copy the code
If you use the @ComponentScan annotation, make sure you scan your com.flscode.resume package. Because this annotation overwrites componentScan in springbootApplication, you can put some common data into the common module. If a module needs to be used, you need to scan it into the module. This reduces code redundancy and improves development efficiency