Description
FrostbittenKing opened BATCH-2821 and commented
I fail to correctly use the RepositoryMethodReference with the RepositoryItemReaderBuilder.
After I found your sample tests (they are only unit tests), i tried it in an actual spring boot batch application, but the application always terminates on startup, when it tries to instantiate the bean for.
-- Snip
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.data.RepositoryItemReader]: Factory method 'reader' threw exception; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.sun.proxy.$Proxy202Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.data.RepositoryItemReader]: Factory method 'reader' threw exception; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.sun.proxy.$Proxy202 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583) ... 64 common frames omittedCaused by: java.lang.IllegalArgumentException: Cannot subclass final class com.sun.proxy.$Proxy202 at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:565) at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329) at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:492) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91) at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:480) at org.springframework.cglib.proxy.Enhancer.create(Enhancer.java:305) at org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder$RepositoryMethodReference.methodIs(RepositoryItemReaderBuilder.java:270) at sample.batch.jobs.SampleJob.reader(SampleJob.java:124) at sample.batch.jobs.SampleJob$$EnhancerBySpringCGLIB$$879fb0c0.CGLIB$reader$4(<generated>) at sample.batch.jobs.SampleJob$$EnhancerBySpringCGLIB$$879fb0c0$$FastClassBySpringCGLIB$$f29d5ef8.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:365) at sample.batch.jobs.SampleJob$$EnhancerBySpringCGLIB$$879fb0c0.reader(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 65 common frames omitted
Sorry, cannot list the complete stacktrace (work-related).
I'm not sure how this is supposed to work, since the class gets proxied (and as documented) cannot be final, but this is a Repository Interface extending JpaRepository, which makes it final by design, as far as I understand.
I'm sorry for asking but was this ever tested outside a unit test?
Following sample fails, and blows up with a stacktrace similar to the one listed above:
@Repository
public interface SampleRepository extends JpaRepository<JpaSample,Long>
{
// ...
}
public class SampleReaderIT extends AbstractIntegrationTest // common base class for spring full blown integration tests
{
@Test
public void sampleTest() throws Exception
{
RepositoryItemReaderBuilder.RepositoryMethodReference<SampleRepository> methodReference =
new RepositoryItemReaderBuilder.RepositoryMethodReference<>(sampleRepository); methodReference.methodIs().getOne(null); new RepositoryItemReaderBuilder<JpaSample>()
.repository(methodReference)
.pageSize(100)
.saveState(false)
.sorts(MapUtils.putAll(new HashMap<>(), new Object[][]`"id", Sort.Direction.ASC`))
.build();
}
}
No further details from BATCH-2821