Spring 自定义注释
Spring Custom Annotations
我不确定是否可以轻松地做我想做的事。我有以下控制器,它既可以用作休息控制器,也可以用作 websocket 控制器:
@RestController
@RequestMapping("rest/user")
@MessageMapping("/user")
public class UserController {
@Autowired
private UserRepo userRepo;
@RequestMapping("/list")
@MessageMapping("/list")
@SendTo("/channel/user")
public apiResImpl list() {
Iterable<UserImpl> users = userRepo.findAll();
return users != null ? new apiResImpl("success", users) : new apiResImpl("fail");
}
...
我想将这些注释中的几个包装在两个自定义注释@ApiController 和@ApiMapping
像这样:
@ApiController("/user")
public class UserController {
@Autowired
private UserRepo userRepo;
@ApiMapping("/list")
public apiResImpl list() {
Iterable<UserImpl> users = userRepo.findAll();
return users != null ? new apiResImpl("success", users) : new apiResImpl("fail");
}
...
我遇到的问题是能够将值传递到内部传递给 spring 注释的自定义注释中。这个@interface 会是什么样子?有例子吗?
谢谢!
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@RestController
@RequestMapping(value="/rest/*")
@MessageMapping
public @interface ApiController {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@RequestMapping // meta-annotated RequestMapping
@MessageMapping // meta-annotated MessageMapping
public @interface ApiController {
@AliasFor(annotation = RequestMapping.class, attribute = "value") // -> alias for RequestMapping value attribute
String[] value() default {};
@AliasFor(annotation = MessageMapping.class, attribute = "value") // -> alias for MessageMapping value attribute
String[] mvalue() default{};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@RequestMapping
@MessageMapping
public @interface ApiMapping {
@AliasFor(annotation = RequestMapping.class, attribute = "value")
String[] value() default {};
@AliasFor(annotation = MessageMapping.class, attribute = "value")
String[] mvalue() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "method")
RequestMethod[] method() default {};
}
参考下面link我想出了这个解决方案,使用这种技术我们可以给元注释注释的属性一个别名,即(这里是RequestMapping,MessageMapping)。
AliasFor 的一个缺点是,它是不可重复的注释。
我不确定是否可以轻松地做我想做的事。我有以下控制器,它既可以用作休息控制器,也可以用作 websocket 控制器:
@RestController
@RequestMapping("rest/user")
@MessageMapping("/user")
public class UserController {
@Autowired
private UserRepo userRepo;
@RequestMapping("/list")
@MessageMapping("/list")
@SendTo("/channel/user")
public apiResImpl list() {
Iterable<UserImpl> users = userRepo.findAll();
return users != null ? new apiResImpl("success", users) : new apiResImpl("fail");
}
...
我想将这些注释中的几个包装在两个自定义注释@ApiController 和@ApiMapping
像这样:
@ApiController("/user")
public class UserController {
@Autowired
private UserRepo userRepo;
@ApiMapping("/list")
public apiResImpl list() {
Iterable<UserImpl> users = userRepo.findAll();
return users != null ? new apiResImpl("success", users) : new apiResImpl("fail");
}
...
我遇到的问题是能够将值传递到内部传递给 spring 注释的自定义注释中。这个@interface 会是什么样子?有例子吗?
谢谢!
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@RestController
@RequestMapping(value="/rest/*")
@MessageMapping
public @interface ApiController {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@RequestMapping // meta-annotated RequestMapping
@MessageMapping // meta-annotated MessageMapping
public @interface ApiController {
@AliasFor(annotation = RequestMapping.class, attribute = "value") // -> alias for RequestMapping value attribute
String[] value() default {};
@AliasFor(annotation = MessageMapping.class, attribute = "value") // -> alias for MessageMapping value attribute
String[] mvalue() default{};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@RequestMapping
@MessageMapping
public @interface ApiMapping {
@AliasFor(annotation = RequestMapping.class, attribute = "value")
String[] value() default {};
@AliasFor(annotation = MessageMapping.class, attribute = "value")
String[] mvalue() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "method")
RequestMethod[] method() default {};
}
参考下面link我想出了这个解决方案,使用这种技术我们可以给元注释注释的属性一个别名,即(这里是RequestMapping,MessageMapping)。
AliasFor 的一个缺点是,它是不可重复的注释。