简体   繁体   中英

custom annotation based upon logic and modify the variable value in java spring

In Java we can use custom annotation to validate the fields but apart from this is there any way we can set the value of variable based upon logic using custom annotation in java or spring boot. See example below

@CustomAnnotation
private String name;

if setter method set the value to name is "$123See" our custom annotation automatically modify the value to name "see" by remove some characters or based upon our own logic.

This is not possible directly and without a background process. Basically, attribute annotations are performed and populated by class analysis.

#1 way : You create a setter method for private attributes and define annotate on it. So, you have to define (example) @Around AOP to your annotation. https://www.baeldung.com/spring-aop-annotation

#2 way : You add your annotation to your attribute and you have to analyse it with Java reflection, direct after setting the attribute or before setting it with another logic. https://www.baeldung.com/java-custom-annotation

(You can write private attr. in reflection temporary: Set private field value with reflection )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM