Modify a class definition's annotation string parameter at runtime

后端 未结 7 1875
[愿得一人]
[愿得一人] 2020-11-22 04:55

Imagine there is a class:

@Something(someProperty = \"some value\")
public class Foobar {
    //...
}

Which is already compiled (I cannot c

7条回答
  •  不要未来只要你来
    2020-11-22 05:06

    SPRING can do this job very easily , might be useful for spring developer . follow these steps :-

    First Solution :- 1)create a Bean returning a value for someProperty . Here I injected the somePropertyValue with @Value annotation from DB or property file :-

        @Value("${config.somePropertyValue}")
        private String somePropertyValue;
    
        @Bean
        public String somePropertyValue(){
            return somePropertyValue;
        }
    

    2)After this , it is possible to inject the somePropertyValue into the @Something annotation like this :-

    @Something(someProperty = "#{@somePropertyValue}")
    public class Foobar {
        //...
    }
    

    Second solution :-

    1) create getter setter in bean :-

     @Component
        public class config{
             @Value("${config.somePropertyValue}")
             private String somePropertyValue;
    
             public String getSomePropertyValue() {
               return somePropertyValue;
             }
            public void setSomePropertyValue(String somePropertyValue) {
               this.somePropertyValue = somePropertyValue;
            }
        }
    

    2)After this , it is possible to inject the somePropertyValue into the @Something annotation like this :-

    @Something(someProperty = "#{config.somePropertyValue}")
    public class Foobar {
        //...
    }
    

提交回复
热议问题