0%

java注解合并,继承

文章字数:125,阅读全文大约需要1分钟

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

例子

SpringMVC中的注解

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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提供的注解,用来为其它属性赋值。
比如

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

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