How to use the @value annotation inside a method to read a property from a property file?

Can I use the @value annotation inside a method to read a property?

void method1(){ @Value("#{AppProperties['service.name']}") String name; ------- ------- } 
+4
source share
2 answers

accessor private is not suitable for a method variable.

If you look at the definition of the @Value annotation, it can only be placed at the field level, in the PARAMETER or METHOD parameter.

 @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Value { 

Thus, either you declare the name as an attribute of the class, or as a parameter of a method ...

+3
source

no :) you can use annotations to annotate classes, fields, methods and their arguments. but not in methods, since there is no way to get local variable methods using reflection to process these annotations. Use @Value in your field and read the value from your method.

+4
source

Source: https://habr.com/ru/post/1433834/


All Articles