I. Combination annotations and meta-annotations
Starting with spring 2, in response to the annotation functionality introduced by JDK 1.5, spring began to add annotations to replace the XML configuration in large numbers. Spring annotations are mainly used to configure the injected bean, and the facets-related configuration (@Transactional). With the extensive use of annotations, it is quite verbose to use the same multiple annotations in each class. This is called template code and is the code to be eliminated in spring design principles.
The so-called meta-annotations are annotations to other annotations, annotated annotations are called combined annotations, and combinations of annotations have the function of meta-annotations. Many of the annotations in spring can be used as meta annotations, and spring itself has many combinations of annotations, such as @configuration, a combination of @component annotations, which indicates that the class is actually a bean.
Using @configuration and @componentscan annotations to the configuration class in previous studies, the following two meta-annotations are grouped together so that we can simply write an annotation to represent two annotations.
Example:
(1) Example combination annotations
Packagecom.ecworking.annotation;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;Importjava.lang.annotation.*; @Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented @configuration//combining @configuration meta-annotations@ComponentScan//combining @componentscan meta-annotations Public@Interfacewiselyconfiguration {string[] value ()default{};//overriding the value parameter}
(2) Demo Service Bean.
Package com.ecworking.annotation; Import Org.springframework.stereotype.Service; @Service Public class Demoservice { publicvoid Outputresult () { System.out.println ( "The Bean is also available from the combination annotation configuration");} }
(3) New configuration class
Package// use @wiselyconfiguration instead of @configuration and @componentscanpublic class Democonfig {}
(4) Operation
Packagecom.ecworking.annotation;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext; Public classMain { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Democonfig.class); Demoservice Demoservice= Context.getbean (Demoservice.class); Demoservice.outputresult (); Context.close (); }}
Operation Result:
Spring Boot Combat Notes (ix)--Spring advanced topics (combination annotations and meta annotations)