We have an application that houses a bunch of very similar cron-scheduled jobs. They all use @Scheduled
annotation and have a couple of fields in common, like limit
and timeout
.
Mentioning @Scheduled
parameters here might be useless, but they get to be mentioned the metadata on compile, so having extra unused fields is worth. A bit clearer documentation is always a plus.
@Data
class JobProperties {
private String cron;
private String fixedDelay;
private String fixedRate;
private String initialDelay;
private int limit;
@DurationUnit(ChronoUnit.MINUTES)
private Duration timeout;
}
I want to take an approach of sharing the dto between the job configurations from this answer:
@Configuration
public class JobConfiguration {
// Lies in other file, listed just for the clarity
private static final String OUR_PREFIX = "com.xobotun.github.example";
@Bean
@ConfigurationProperties(prefix = OUR_PREFIX + '.' + Job1.techName)
public JobProperties job1Config() {
return new JobProperties();
}
@Bean
@ConfigurationProperties(prefix = OUR_PREFIX + '.' + Job2.techName)
public JobProperties job2Config() {
return new JobProperties();
}
// Repeat a couple more times
}
But I also want to make this dto immutable. Nobody sane would write into the properties file, but accidents may happen. Better to prevent stuff like this on the compiler's level. So I want to declare all fields as final
, and declare these properties injectable not through setters, but via constructor using @ConstructorBinding
, like in the answer here. Maybe through @AllArgsConstructor(onConstructor_ = @ConstructorBinding)
.
However, my problem is that this annotation is actually processed by ConfigurationPropertiesBindConstructorProvider, and I create instances myself in my JobConfiguration
class.
➥ Has anyone encountered this issue? Only thing I found is this question, but it was asked a year and eight months ago, and haven't received any sensible answers, sadly.
I'll go with the mutable dto by now, but I'll be eager to see if there is any way to spring-bootize these into the code. Maybe through some factories, I don't know. :D