Spring Batch With Annotation and Caching

A JobExecutionListener can be used to populate the cache with reference data before the job is executed and clear the cache after the job is finished. Here is an example: import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobExecutionListener; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.Step; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import … Read more

Spring Cache with collection of items/entities

In fact, it is possible, even with Spring’s Caching Abstraction, but not out-of-the-box (OOTB). Essentially, you must customize Spring’s caching infrastructure (Explained further below) By default, Spring’s caching infrastructure uses the entire @Cacheable method parameter arguments as the cache “key”, as explained here. Of course you can also customize the key resolution using either a … Read more

How to test Spring’s declarative caching support on Spring Data repositories?

If you want to test a technical aspect like caching, don’t use a database at all. It’s important to understand what you’d like to test here. You want to make sure the method invocation is avoided for the invocation with the very same arguments. The repository fronting a database is a completely orthogonal aspect to … Read more

Spring cache @Cacheable method ignored when called from within the same class

This is because of the way proxies are created for handling caching, transaction related functionality in Spring. This is a very good reference of how Spring handles it – Transactions, Caching and AOP: understanding proxy usage in Spring In short, a self call bypasses the dynamic proxy and any cross cutting concern like caching, transaction … Read more