Detailed Explanation of Spring Combined Annotation and Meta Annotation Examples in JSP

  • 2021-12-04 10:51:55
  • OfStack

Detailed Explanation of Spring Combined Annotation and Meta Annotation Examples in JSP

Summary: Annotation (Annotation), also called metadata. Description of 1 code level. It is at the same level as class, interface and enumeration. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, and so on to describe these elements

1. The annotations that can be annotated to other annotations are called meta-annotations, and the annotated annotations are called combined annotations, which can simplify a lot of repetitive annotation operations

2. Example combination annotation


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@ComponentScan
public @interface GroupAnnotation {
  String[] value() default {};
}

Code explanation: Combine @ Configuration and @ ComponentScan meta-annotations and override value parameters

3. Write a generic Bean


@Servicepublic class DemoService
 { 
 public void sys()
 {   System.out.println(" Example of combined annotations "); 
 }
}

4. Configuration classes using composite annotations


@GroupAnnotation("com.xuanwu.annotation")
public class DemoConfig {
}

Step 5 Run


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

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: