美文网首页
java注解合并,继承

java注解合并,继承

作者: 策马踏清风 | 来源:发表于2020-05-29 16:39 被阅读0次

    将多个注解合并到一起,这样不用每次都要写若干个重复的注解。

    例子

    SpringMVC中的注解

    • @RestController
    • @RequestMapping("/user")

    可以合并成@PathRestController("/user")

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
     
    import org.springframework.core.annotation.AliasFor;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @RestController
    @RequestMapping
    public @interface PathRestController {
        @AliasFor("path")
        String[] value() default {};
     
        @AliasFor("value")
        String[] path() default {};
    }
    

    @AliasFor

    这是Spring提供的注解,用来为其它属性赋值。
    比如

    @AliasFor("path")
    String[] value() default {};
    

    就是把value的值也给path赋值。以此实现value和path的值保持一致。

    相关文章

      网友评论

          本文标题:java注解合并,继承

          本文链接:https://www.haomeiwen.com/subject/fmcfzhtx.html