Combining annotations and meta-annotations

  Since spring 2, in response to the annotation function introduced by jdk1.5, spring has begun to add a large number of annotations to replace xml configuration. With the extensive use of annotations, we find that when multiple identical annotations are used multiple times in different classes or methods, the code looks cumbersome. This is the so-called boilerplate code, the code to be eliminated in the spring design principles. For example the following code:

  If we combine the above three annotations into one annotation, it will save trouble! These are composite annotations, as opposed to meta-annotations.
  Meta-annotations are annotations that can be annotated to other annotations, and annotated annotations are composite annotations.
  Um, what a mess. Meta-annotation is the basic data that can be used with other annotations to construct new annotations, and the constructed new data is combined annotations. Whether an annotation is a composite annotation or a meta-annotation is relative, it is a composite annotation relative to one annotation, and it may be a meta-annotation relative to another.
------ Example: @Configuration and @ComponentScan annotations are combined into a new annotation ------------------------------- ---------

Annotation class:

@Target(ElementType.TYPE) //jdk5 annotation, indicating the target type, whether it is a class, a method or a variable, etc.
@Retention(RetentionPolicy.RUNTIME)//jdk annotation, indicating how long the annotation will be saved, life cycle related
@Documented//jdk annotation, indicating whether the document is exported by the javadoc command
@Configuration //Combined Configuration annotations
@ComponentScan //Combination ComponentScan annotation
public @interface WiselyConfiguration {
    String[] value() default {};
}

The new annotation uses the class:

  @WiselyConfiguration("com.wzy.bj.myannotation")
  public class DemoConfig {
  }

Test service class:

@Service
public class DemoService {
    public void doService(){
        System.out.println("doing my demoService,,,,,");
    }
}
Test class:

public class AnnotationMain {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
        DemoService demoService = context.getBean(DemoService.class);
        demoService.doService();

        context.close();
    }
}

Test Results:

  

 

Guess you like

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