Injecting externalized value into Spring annotation

Viewed 52862

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.

However, I am unsure whether it is actually impossible, so I'd appreciate any suggestions or definitive answers on this.

More to the point, I am trying to externalize an annotation value which controls delays between scheduled method calls in Spring, e.g.:

public class SomeClass {

    private Properties props;
    private static final long delay = 0;

    @PostConstruct
    public void initializeBean() {
        Resource resource = new ClassPathResource("scheduling.properties");
        props = PropertiesLoaderUtils.loadProperties(resource);
        delay = props.getProperties("delayValue");
    }

    @Scheduled(fixedDelay = delay)
    public void someMethod(){
        // perform something
    }
}

Suppose that scheduling.properties is on classpath and contains property key delayValue along with its corresponding long value.

Now, this code has obvious compilation errors since we're trying to assign a value to final variable, but that is mandatory, since we can't assign the variable to annotation value, unless it is static final.

Is there any way of getting around this? I've been thinking about Spring's custom annotations, but the root issue remains - how to assign the externalized value to annotation?

Any idea is welcome.

EDIT: A small update - Quartz integration is overkill for this example. We just need a periodic execution with sub-minute resolution and that's all.

6 Answers

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();
    }   
}

We can use a field value from other beans. Suppose we have a bean named someBean with a field someValue equal to 10. Then, 10 will be assigned to the field:

    @Value("#{someBean.someValue}")  
    private Integer someBeanValue;  

Reference: A Quick Guide to Spring @Value - Baeldung

Related