Skip to content
Advertisement

Using LocalDate in Spring context and avoid CGLib issue

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 :

JavaScript

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.

JavaScript

However, Spring doesn’t like it :

JavaScript

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 ?

Advertisement

Answer

From the Javadoc of StepScope:

JavaScript

Now the proxy mode TARGET_CLASS means the proxy will be a CGLIB proxy (See ScopedProxyMode#TARGET_CLASS) which mean a sub-class of the bean type will be created for the proxy. Since you are declaring a step scoped bean of type LocalDate which is a final class, Spring (Batch) is unable to create the proxy, hence the error.

I don’t see the added value of having a step scoped LocalDate bean. A step scoped bean is useful for late binding of job parameters or attributes from the step/job execution context. But if you really want that bean to be step scoped, you can try another proxy mode like:

JavaScript
Advertisement