@springbootapplication 注解,spring boot注解详解

  @springbootapplication 注解,spring boot注解详解

  

目录

简介用法1:注解的属性互为别名简介实例用法2.继承父注解的属性,不重写属性名简介代码用法3:继承父注解的属性,并重写属性名简介代码

 

  

简介

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

 

  

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

 

  

简介

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

 

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

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

  

实例

注解

 

  包com。举例。注释;导入org。spring框架。核心。注释。的别名;导入Java。郎。注释。*;@保留(保留政策.运行时)@Target({ElementType .方法,元素类型.TYPE })@ documentated @ inherited public @ interface my annotation { @ alias for(attribute= location )String value()default " ";@AliasFor(attribute=value )字符串位置()默认值"";}控制器

  包com。举例。控制器;导入com。举例。注释。我的注解;导入org。spring框架。核心。注释。注释工具;导入org。spring框架。网络。绑定。注释。请求映射;导入org。spring框架。网络。绑定。注释。休息控制器;@ rest控制器@请求映射(/hello )公共类你好控制器{ @我的注释(value= location )/*//下边这两种写法结果与上边是相同的@我的批注( location )@我的批注(location= location )*/@请求映射(/test1 )公共字符串test1(){我的批注我的批注=null请尝试{我的批注=批注实用程序。获取注释(这个。获取类().getMethod(test1 ),我的批注。类);} catch(nosuchmethodeexception e){ e . printstacktrace();}返回值: 我的注释。value();我的注解。location();} }测试

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

  前端结果(值和位置都是同一个值)

  值值:位置位置:位置

  

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

简介

子注解

 

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

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

  

 

  

 

  

代码

注解

 

  

package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})@Documented@Inheritedpublic @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@MyAnnotationpublic @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@Inheritedpublic @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@MyAnnotationpublic @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:subLocationvalue:;location:value:subLocation;location:subLocation

 

  

以上就是SpringBoot中注解@AliasFor的使用详解的详细内容,更多关于SpringBoot注解@AliasFor的资料请关注盛行IT其它相关文章!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: