In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to use @ AliasFor annotations in SpringBoot". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use @ AliasFor annotations in SpringBoot" can help you solve your doubts.
Usage 1: introduction to the properties of annotations as aliases to each other
It can be annotated to two attributes of a custom annotation, indicating that the two are aliases to each other, that is, the two attributes actually have the same meaning.
One of the attribute names must be "value"
Regardless of which property name is specified to set the property value, the other property name is the same property value, or you can default the property name.
If both specify attribute values, the values must be the same, or an error will be reported.
Use it simply. After using it like this, @ MyAnno (location= "shanghai") can be directly written as: @ MyAnno ("shanghai")
The reasons for this feature are:
If a custom annotation has an attribute, and the attribute is named in order to reflect its meaning, the caller must specify the attribute every time the custom annotation is used, and then set it, which is a little troublesome.
Example
Notes
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 "";}
Controller
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") / * / / the result is the same as the above @ 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 ();}}
test
Front-end access: http://localhost:8080/hello/test1
Frontend result (both value and location are the same value)
Value:location;loation:location
Usage 2. Inherits the properties of the parent annotation and does not override the introduction of the attribute name
The reading and writing of the attribute value of the child annotation is actually the reading and writing of the attribute value of the parent annotation. (for inherited properties)
At this point, only inherited attribute values can be read and written.
Code
Notes
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 "; / / this cannot be written and can only have an attribute with the same name as the parent attribute, otherwise error / / @ AliasFor (annotation = MyAnnotation.class) / / String value () default";}
Controller
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 ();}}
test
Front-end access: http://localhost:8080/hello/test
Result
Loation (sub): location (my)
Location:
Location:location (my)
Usage 3: inherit the property of the parent annotation and override the introduction of the property name
The reading and writing of the attribute value of the child annotation is actually the reading and writing of the attribute value of the parent annotation. (for overridden properties)
No matter which property name is specified to set the property value, the other property name is the same property value and cannot be the default property name.
If both specify attribute values, the values must be the same, or an error will be reported.
Code
Notes
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 "" / / the subLocation attribute is written as the following two results are the same / / @ AliasFor (attribute = "value", annotation = MyAnnotation.class) / / String subLocation () default ""; / / @ AliasFor (value = "location", annotation = MyAnnotation.class) / / String subLocation () default "; / /}
Controller
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") / / this is the same as the above result / / @ SubMyAnnotation ("subLocation") / / the default property name will report an error @ 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 ();}}
test
Front-end access: http://localhost:8080/hello/test
Result
SubValue:subLocation;subLoation:subLocation
Value:;location:
Value:subLocation;location:subLocation
After reading this, the article "how to use @ AliasFor annotations in SpringBoot" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.