SpringBoot中注解@AliasFor的使用详解

简介

本文用示例介绍@AliasFor(别名)注解的用法。

用法1:注解的属性互为别名

简介

它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义。

  • 其中一个属性名必须是"value"
  • 无论指明设置哪个属性名设置属性值,另一个属性名也是同样属性值,也可以缺省属性名。
  • 若两个都指明属性值,要求值必须相同,否则会报错。
  • 使用简洁。这样使用之后,@MyAnno(location="shanghai")可以直接写为:@MyAnno("shanghai");

这个功能产生的原因:

若自定义注解有一个属性,且该属性命名上为了体现其含义,调用方必须每次使用自定义注解的时候,都必须写明属性 ,然后设置,这样稍微麻烦。

实例

注解

package com.example.annotation;
    
import org.springframework.core.annotation.AliasFor;
    
import java.lang.annotation.*;
    
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
public @interface MyAnnotation {
     @AliasFor(attribute = "location")
     String value() default "";
    
     @AliasFor(attribute = "value")
     String location() default "";
}

控制器

package com.example.controller;
    
import com.example.annotation.MyAnnotation;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
    
@RestController
@RequestMapping("/hello")
public class HelloController {
    
     @MyAnnotation(value = "location")
     /*//下边这两种写法结果与上边是相同的
     @MyAnnotation("location")
     @MyAnnotation(location = "location")*/
     @RequestMapping("/test1")
     public String test1() {
         MyAnnotation myAnnotation = null;
         try {
             myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test1"), MyAnnotation.class);
         } catch (NoSuchMethodException e) {
             e.printStackTrace();
         }
    
         return "value:" + myAnnotation.value() + ";loation:" + myAnnotation.location();
     }
}

测试

前端访问:http://localhost:8080/hello/test1

前端结果(value和location都是同一个值)

value:location;loation:location

用法2.继承父注解的属性,不重写属性名

简介

子注解的属性值的读写,其实是对父注解的属性值的读写。(对继承的属性来说)

此时,只能读写继承了的属性值。

代码

注解

package com.example.annotation;
    
import org.springframework.core.annotation.AliasFor;
    
import java.lang.annotation.*;
    
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
public @interface MyAnnotation {
     @AliasFor(attribute = "location")
     String value() default "";
    
     @AliasFor(attribute = "value")
     String location() default "";
}
package com.example.annotation;
    
import org.springframework.core.annotation.AliasFor;
    
import java.lang.annotation.*;
    
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
@MyAnnotation
public @interface SubMyAnnotation {
     @AliasFor(annotation = MyAnnotation.class)
     String location() default "";
    
// 这个不能写,只能有一个与父属性名同名的属性,否则报错
// @AliasFor(annotation = MyAnnotation.class)
// String value() default "";
}

控制器

package com.example.controller;
    
import com.example.annotation.MyAnnotation;
import com.example.annotation.SubMyAnnotation;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
    
@RestController
@RequestMapping("/hello")
public class HelloController {
     @SubMyAnnotation(location = "location(my)")
     @RequestMapping("/test")
     public String test() {
         SubMyAnnotation subMyAnnotation = null;
         MyAnnotation myAnnotation = null;
         MyAnnotation myAnnotation1 = null;
    
         try {
             subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class);
             myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
             myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
         } catch (NoSuchMethodException e) {
             e.printStackTrace();
         }
    
         return "loation(sub):" + subMyAnnotation.location() + "\n" +
                 "location:" + myAnnotation.location() + "\n" +
                 "location:" + myAnnotation1.location();
     }
}

测试

前端访问:http://localhost:8080/hello/test

结果

loation(sub):location(my)
location:
location:location(my)

用法3:继承父注解的属性,并重写属性名

简介

子注解的属性值的读写,其实是对父注解的属性值的读写。(对重写的属性来说)

无论指明设置哪个属性名设置属性值,另一个属性名也是同样属性值,不可以缺省属性名。

若两个都指明属性值,要求值必须相同,否则会报错。

代码

注解

package com.example.annotation;
    
import org.springframework.core.annotation.AliasFor;
    
import java.lang.annotation.*;
    
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
public @interface MyAnnotation {
     @AliasFor(attribute = "location")
     String value() default "";
    
     @AliasFor(attribute = "value")
     String location() default "";
}

package com.example.annotation;
    
import org.springframework.core.annotation.AliasFor;
    
import java.lang.annotation.*;
    
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
@MyAnnotation
public @interface SubMyAnnotation {
     @AliasFor(attribute = "value", annotation = MyAnnotation.class)
     String subValue() default "";
    
     @AliasFor(attribute = "location", annotation = MyAnnotation.class)
     String subLocation() default "";
    
// subLocation属性写成下边这两种结果是一样的
// @AliasFor(attribute = "value", annotation = MyAnnotation.class)
// String subLocation() default "";
    
// @AliasFor(value = "location", annotation = MyAnnotation.class)
// String subLocation() default "";
//
}

控制器

package com.example.controller;
    
import com.example.annotation.MyAnnotation;
import com.example.annotation.SubMyAnnotation;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
    
@RestController
@RequestMapping("/hello")
public class HelloController {
     @SubMyAnnotation(subValue = "subLocation")
// @SubMyAnnotation(subLocation = "subLocation") //这个与上边结果相同
// @SubMyAnnotation("subLocation") //缺省属性名会报错
     @RequestMapping("/test")
     public String test() {
         SubMyAnnotation subMyAnnotation = null;
         MyAnnotation myAnnotation = null;
         MyAnnotation myAnnotation1 = null;
    
         try {
             subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class);
             myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
             myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
         } catch (NoSuchMethodException e) {
             e.printStackTrace();
         }
         return "subValue:" + subMyAnnotation.subValue() + ";subLoation:" + subMyAnnotation.subLocation() + "\n" +
                 "value:" + myAnnotation.value() + ";location:" + myAnnotation.location() + "\n" +
                 "value:" + myAnnotation1.value() + ";location:" + myAnnotation1.location();
     }
}

测试

前端访问:http://localhost:8080/hello/test

结果

subValue:subLocation;subLoation:subLocation
value:;location:
value:subLocation;location:subLocation

以上就是SpringBoot中注解@AliasFor的使用详解的详细内容!

另外一篇不错的文章: SpringBoot中的@AliasFor注解介绍_精通Springboot的博客-CSDN博客

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RestControllerAdvice 是一个用于全局异常处理和统一返回结果的注解。在 Spring Boot 项目,我们可以通过 @RestControllerAdvice 注解来定义一个全局的异常处理类。 使用 @RestControllerAdvice 注解的类可以包含以下几个注解: 1. @ExceptionHandler:用于处理特定异常类型的方法。 2. @InitBinder:用于在控制器初始化 WebDataBinder 的方法。 3. @ModelAttribute:将键值对添加到全局的 Model 。 4. @ResponseBody:将方法的返回值作为响应体。 当项目发生异常时,@RestControllerAdvice 注解的类会捕获到异常并根据具体的异常类型执行相应的处理方法。这样可以实现全局的异常处理,避免在每个接口都写异常处理逻辑。 下面是一个简单的示例代码: ```java @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { // 异常处理逻辑 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } } ``` 在上面的代码使用 @ExceptionHandler 注解来定义了一个处理 Exception 类型异常的方法。当项目出现 Exception 类型的异常时,会执行该方法,并返回一个带有异常信息的 ResponseEntity 对象。 注意:@RestControllerAdvice 注解只会扫描被 @Controller 或 @RestController 注解的类。因此,确保你的全局异常处理类被正确扫描并起作用。 希望能帮到你!如有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值