Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

    void method1(){

     @Value("#{AppProperties['service.name']}") String name;
     -------
      -------
   } 
like image 616
TP_JAVA Avatar asked Sep 12 '12 15:09

TP_JAVA


2 Answers

accessor private for a method variable is unappropriate.

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

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

So either you declare name as a class attribute or as a method parameter...

like image 170
yodamad Avatar answered Oct 14 '22 09:10

yodamad


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

like image 34
Erhan Bagdemir Avatar answered Oct 14 '22 10:10

Erhan Bagdemir