Java Annotation Easy-to-understand Series Tutorial Four Combination Custom Annotation

After the initial meeting of custom annotations, I sometimes encounter 1. An annotation has only one attribute and I want to add an attribute. 2. Two annotations are often used together. Combine the two annotations into one and write one to save trouble. Wait for the scene.

The main logic is: a new annotation is defined, then the original annotation is introduced, and the properties of the new annotation are bound to the properties of the original annotation .

1. Combine multiple annotations to become a new annotation: a, b annotations are merged into c annotation, c has all or part of the attributes of a and b. Take @SpringBootApplication as an example

Combinational logic: @SpringBootApplication composition @ComponentScan of basePackages and basePackageClasses and @EnableAutoConfiguration to exclude and excludeName

(1) You need to add the annotation @EnableAutoConfiguration you want to introduce in the new annotation @SpringBootApplication

(2) The annotation @AliasFor is used to determine the correspondence between the attributes of the original annotation and the attributes of the new annotation

The annotation of @AliasFor specifies which annotation is introduced. If the original annotation attribute is consistent with the new annotation attribute, it will automatically match. If the original annotation and the new annotation attribute name are inconsistent, you need to use attribute to specify what the attribute name of the original annotation is.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM,
				classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {


	@AliasFor(annotation = EnableAutoConfiguration.class)
	Class<?>[] exclude() default {};


	@AliasFor(annotation = EnableAutoConfiguration.class)
	String[] excludeName() default {};


	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};


	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class<?>[] scanBasePackageClasses() default {};

}

2. The original comment does not have a default value, and the situation that must be specified requires attention

Now I want to use shiro's @RequiresPermissions annotation to generate a new @ShiroRequiresPermissions annotation

(1) The original annotation @RequiresPermissions content

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresPermissions {


    String[] value();

    Logical logical() default Logical.AND; 

}

(2) New annotation @ShiroRequiresPermissions

The original annotation @RequiresPermissions(value = {}) is introduced, the default value is not set, and the value needs to be assigned.

It should be noted that if you write @RequiresPermissions(value = "") on a new annotation like this, the value of the new annotation will always use this value, even if you fill in the value when using the new annotation, it will not take effect. So you need to write @RequiresPermissions(value = ())

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequiresPermissions(value = {})
public @interface ShiroRequiresPermissions {

    @AliasFor(annotation = RequiresPermissions.class, attribute = "value")
    String[] value();

    @AliasFor(annotation = RequiresPermissions.class, attribute = "logical")
    Logical logical() default Logical.AND;

    String remark() default "";
}

Summary: Annotation combination needs to be divided into three steps: 1. Customize new annotations 2. Write new annotations to introduce annotations 3. Use @AliasFor to establish the attribute correspondence between new and old annotations.

Guess you like

Origin blog.csdn.net/Mint6/article/details/103836412