I have a small job written with Spring Boot Batch 2.2.2. It takes a date as a parameter, and since several components need that date, I place it as a bean in the Spring context :
@Bean
@StepScope
public Date processingDate(){
if(isEmpty(applicationArguments.getSourceArgs())){
throw new IllegalArgumentException("No parameter received - expecting a date to be passed as a command line parameter.");
}
SimpleDateFormat sdf = new SimpleDateFormat(EXPECTED_DATE_FORMAT);
String expectedDateFromCommandLine=applicationArguments.getSourceArgs()[0];
try {
return sdf.parse(expectedDateFromCommandLine);
} catch (ParseException e) {
throw new IllegalArgumentException("Expecting the parameter date to have this format : "+ EXPECTED_DATE_FORMAT,e);
}
}
It works well, no issue.
Now I am doing some refactoring, and thought I should use LocalDate instead of Date, as it is now recommended since Java 8.
@Bean
@StepScope
public LocalDate processingDate(){
if(isEmpty(applicationArguments.getSourceArgs())){
throw new IllegalArgumentException("No parameter received - expecting a date to be passed as a command line parameter.");
}
String expectedDateFromCommandLine=applicationArguments.getSourceArgs()[0];
return LocalDate.parse(expectedDateFromCommandLine, DateTimeFormatter.ofPattern(EXPECTED_DATE_FORMAT));
}
However, Spring doesn't like it :
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class java.time.LocalDate: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class java.time.LocalDate
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:208)
I understand that behind the scene, Spring does some magic with some proxy-ing and all.. But there must be an easy way to make this possible, right ?