Injecting externalized value into Spring annotation

前端 未结 5 943
一个人的身影
一个人的身影 2020-12-29 04:04

I\'ve been thinking around the Java feature that evaluates annotation values in compile-time and it seems to really make difficult externalizing annotation values.

H

5条回答
  •  既然无缘
    2020-12-29 04:36

    If you want to make this work with annotation rather than bean configuration xml, you can use the following annotations: @Component, @PropertySource with PropertySourcesPlaceholderConfigurer Bean itself, like this:

    @Component
    @PropertySource({ "classpath:scheduling.properties" })
    public class SomeClass {
    
        @Scheduled(fixedDelay = "${delay}")
        public void someMethod(){
            // perform something
        }
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }   
    }
    

提交回复
热议问题