0

Normally, we can use a cron expression defined as "cron.expression" in the default property file, as follows:

@Scheduled(cron = "${cron.expression}")
public void demoServiceMethod(){

}

But I wish to define a property file for this class itself, and use the "cron.expression" property from this file. How can I do that?

P.S: I am using Java 1.7

Daud
  • 7,429
  • 18
  • 68
  • 115
  • `I wish to define a property file for this class itself` What do you mean by **this class**? Please share some example how your desired code should look – Nikolai Shevchenko Aug 21 '19 at 14:09
  • By **this class**, I mean that that while using @Value annotation in this class, properties should be picked up from this new property file, and everywhere else, the usual property sources should be used – Daud Aug 22 '19 at 07:11
  • is following format acceptable for you? `@Scheduled(cron = "file.properties:${cron.expression}")` – Nikolai Shevchenko Aug 22 '19 at 07:14
  • It didn't recognize the key `cron.expression` present in `file.properties` (which is placed at the classpath and is already beign picked up if we use `MessageBundle`). I als tried : `@Scheduled(cron = "classpath:file.properties:${cron.expression}")` and it didn't work either. – Daud Aug 22 '19 at 08:22
  • It wasn't supposed to work :) I just asked if that format would be OK for you to use – Nikolai Shevchenko Aug 22 '19 at 08:27

1 Answers1

0

Add to your class PropertySource

 @PropertySource("classpath:other.properties")

Or using Configuration

 @Configuration
 @PropertySources(value = {@PropertySource("classpath:/datasource.properties")})
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • According to the link you provided, *@PropertySource* can only be used in conjunction with *@Configuration*. As such, it would apply this setting to the entire application. I only want it to be applicable to this class – Daud Aug 21 '19 at 13:28
  • @daud see https://stackoverflow.com/questions/34195290/spring-scheduled-cron-details-from-property-file-exception – Ori Marko Aug 21 '19 at 13:31
  • According to the above link, a **PropertySourcesPlaceholderConfigurer** needs to be configured. But that again would set it for the entire application. I need properties to be picked up from a different property file, *only* for this specific class – Daud Aug 21 '19 at 13:42