Spring Batch - "job" scoped beans can not be injected into "job" or "step" scoped beans - spring

I am using spring batch version 3.0.2.RELEASE and spring framework version 3.2.12.RELEASE. And I am trying to inject a job scoped bean to another job scoped bean.
My configuration looks like this
<bean id="beanA" class="com.trial.BeanA" scope="job" >
<property name="beanB" ref="beanB" />
</bean>
<bean id="beanB" class="com.trial.BeanB" scope="job"/>
It throws an exception with details:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.beanA' defined in class path resource [trial-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.trial.BeanB' for property 'beanB'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.trial.BeanB] for property 'beanB': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:331)
at org.springframework.batch.core.scope.JobScope.get(JobScope.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:34)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:184)
at com.sun.proxy.$Proxy13.getValue(Unknown Source)
at org.springframework.batch.core.listener.CompositeJobExecutionListener.beforeJob(CompositeJobExecutionListener.java:73)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'com.trial.BeanB' for property 'beanB'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.trial.BeanB] for property 'beanB': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:464)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:495)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:489)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1465)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1424)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 18 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy1 implementing com.trial.BeanB,org.springframework.beans.factory.InitializingBean,org.springframework.aop.scope.ScopedObject,java.io.Serializable,org.springframework.aop.framework.AopInfrastructureBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.trial.BeanB] for property 'beanB': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:449)
... 24 more
Even I try to inject a "job" scoped bean to a "step" scoped bean, it fails and throws a similar exception.
How can I solve the issue?

You are using java proxying which requires interfaces, yet your BeanB doesn't implement an interface that BeanA is coded against. You need to either switch to dynamic subclassing or have BeanB implement an interface and have BeanA expect that interface.

Related

Spring batch -Error in Bean Creation

i want to create a memory jobrepository and i get this exception, Any idea why I am getting this exception?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobExplorerInitial' defined in class path resource [fr/rsi/gdficoba/batch/integrationinitial/job/datasource.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy22 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean' for property 'repositoryFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy22 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean] for property 'repositoryFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
....
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290) [spring-batch-core-3.0.3.RELEASE.jar:3.0.3.RELEASE]
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:590) [spring-batch-core-3.0.3.RELEASE.jar:3.0.3.RELEASE]
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.proxy.$Proxy22 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean' for property 'repositoryFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy22 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean] for property 'repositoryFactory': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:474) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
..
... 13 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy22 implementing org.springframework.batch.core.repository.JobRepository,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean] for property 'repositoryFactory': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) ~[spring-beans-4.0.5.RELEASE.jar:4.0.5.RELEASE]
... 19 common frames omitted
my configuration is
<bean id="repositoryFactory"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="jobExplorerInitial" class="org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean>
<property name="repositoryFactory" ref="repositoryFactory" />
</bean>
You have to add a '&':
<property name="repositoryFactory" ref="&repositoryFactory"/>

why Mixing Spring AOP proxy mechanisms (CGLIB and JDKDynmic) for dependent beans does not work

I have two public classes configured as follows in Spring application context:
public class LoadErrorData{
private ExceptionData exceptionData;
public LoadErrorData() { }
// reminder
}
public class ExceptionData implements Serializable{
private Resource exceptionDataResource;
public ExceptionData() { }
// reminder
}
Spring applicationContext.xml :
<beans:bean id="loadErrorData" class="com.startup.LoadErrorData" init-method="startup">
<beans:property name="exceptionData" ref="exceptionData"/>
</beans:bean>
<beans:bean id="exceptionData" class="com.server.ExceptionData" init-method="startup">
<beans:property name="exceptionDataResource" value="classpath:${exception.datafile.path}"/>
</beans:bean>
It gives following Exception while initializing :
Oct 07, 2013 1:45:21 PM org.apache.catalina.core.StandardContext
listenerStart SEVERE: Exception sending context initialized event to
listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'loadErrorData' defined in class path resource
[spring/startup-config.xml]: Initialization of bean failed; nested
exception is
org.springframework.beans.ConversionNotSupportedException: Failed to
convert property value of type 'com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'com.server.IICExceptionData' for property
'exceptionData'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.server.IICExceptionData] for property
'exceptionData': no matching editors or conversion strategy found at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at
org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at
org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at
org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
at
org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
at
org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166) at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724) Caused by:
org.springframework.beans.ConversionNotSupportedException: Failed to
convert property value of type 'com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'com.server.IICExceptionData' for property
'exceptionData'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.server.IICExceptionData] for property
'exceptionData': no matching editors or conversion strategy found at
org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485)
at
org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
at
org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 19 more Caused by: java.lang.IllegalStateException: Cannot convert
value of type [com.sun.proxy.$Proxy12 implementing
java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.server.IICExceptionData] for property
'exceptionData': no matching editors or conversion strategy found at
org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
at
org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
... 25 more
but when i put
<aop:scoped-proxy proxy-target-class="true"/>
in ExceptionData bean configuration OR if i remove
implements Serializable
from ExceptionData
exception goes away. My AOP pointcut is as follows:
expression="execution(* com..*(..))"
It seems that Spring is trying to create CGLIB proxy for bean LoadErrorData (as it does not implement any interface) and JDKdynamicProxy for its dependency ExceptionData (as it does implement interface Serializable). And it is not able to do so. Because when i tell spring to create CGLIB proxy explicitly for ExceptionData it works fine. Why is this so? Why it is not able to create JDKDynamic Proxy for a bean(ExceptionData) who is a dependency of bean (LoadErrorData) for which it is trying to Create CGLIB proxy? According to documentation it says that Spring will detect it automatically.
Actually the situation you describe is exactly what is happening.
LoadErrorData is going to be a CgLIB proxy (due to no interfaces.
ExceptionData is going to be a JDK Dynamic Proxy.
However your LoadErrorData expects a bean of type ExceptionData, the JDK Dynamic proxy is only a Serializable (as it will only proxy interfaces and not classes). It will never be possible to cast this to an instance of ExceptionData and hence the loading of the context will fail. (This is also what the stacktrace tells you from the error message).
It will work if you are forcing class based proxies for everything or by removing the Serializable interface. Either way will lead to a classbased proxy.

Not an managed type for Spring Data Mongodb

I have the following mongo configuration:
<context:component-scan base-package="com.example" />
<mongo:repositories base-package="com.example.repositories.mongodb" />
I have the repository as follows:
package com.example.repositories.mongodb;
public interface ReviewRepository extends CrudRepository<Review, String> {...}
and the bean:
package com.example.domain;
#Document(collection="Review")
public class Review implements Serializable { ... }
Unfortunately, when I start the app, I get the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private
com.example.repositories.mongodb.ReviewRepository
com.example.Controller.reviewRepository; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'reviewRepository': FactoryBean threw
exception on object creation; nested exception is
java.lang.IllegalArgumentException: Not an managed type: class
com.example.domain.Review
....
Caused by: java.lang.IllegalArgumentException: Not an managed type:
class com.example.domain.Review
I think that this might be caused by the repository and the bean it manages not being in the same package. But I get the same issue also when I do place them in the same package. Any ideas?
In the same project we were also using mysql and it was scanning the package: com.example.repositories. This caused the issue and once we changed it to com.example.repositories.mysql it was solved.

spring aop - exception while creating advice around JdbcTemplate methods

I've a web application that uses apache dbcp and spring jdbc to perform database operations on an oracle database. I need to write a performance logger that logs the individual times of each database operation. I tried writing an around advice on all 'execute' methods of org.springframework.jdbc.core.JdbcTemplate but it results in an error when spring gets initialized. The logger class and the exception stacktrace is as follows:-
I also tried to use CGLIB proxies by enabling but it errors out on dao classes that extends from spring's StoredProcedure class and use constructor injection.
#Aspect
public class Logger {
#Around("this(org.springframework.jdbc.core.JdbcTemplate) && execution(* execute(*))")
public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object result = pjp.proceed();
LOGGER.debug("time consumed = " + (System.currentTimeMillis() - time));
return result;
}
Exception stacktrace:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'myDao' defined in class path resource [spring/my-dao.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.jdbc.core.JdbcTemplate]:
Could not convert constructor argument value of type [$Proxy7] to required type [org.springframework.jdbc.core.JdbcTemplate]:
Failed to convert value of type '$Proxy7 implementing org.springframework.jdbc.core.JdbcOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'org.springframework.jdbc.core.JdbcTemplate';
nested exception is
java.lang.IllegalStateException: Cannot convert value of type [$Proxy7 implementing org.springframework.jdbc.core.JdbcOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [org.springframework.jdbc.core.JdbcTemplate]:
no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:702)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)

Downcast Spring Factory Bean

// Two third-party factories:
public class OneStaticMethodFactory {
...
public static Object createFactory(String oneInstanceMethodFactoryClassName) {
...
}
}
public class OneInstanceMethodFactory {
...
public OneObject createObject() {
...
}
}
// Hardwired code:
OneInstanceMethodFactory oneInstanceMethodFactory =
(OneInstanceMethodFactory)
OneStaticMethodFactory.createFactory("pkg.to.OneInstanceMethodFactory");
OneObject oneObject =
oneInstanceMethodFactory.createObject();
// Attempt to replace them by Spring beans:
<bean id="oneInstanceMethodFactory" class="pkg.to.OneStaticMethodFactory"
factory-method="createFactory">
<constructor-arg value="pkg.to.OneInstanceMethodFactory">
</constructor-arg>
</bean>
<bean id="oneObject" factory-bean="oneInstanceMethodFactory"
factory-method="createObject">
</bean>
// Run Spring IoC:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'oneObject' defined in class path resource [applicationContext.xml]:
No matching factory method found: factory bean 'oneInstanceMethodFactory';
factory method 'createObject()'.
Check that a method with the specified name and arguments exists and that it is non-static.
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:528)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 26 more
To solve the problem above, how to downcast oneInstanceMethodFactory bean from compile-time type Object to runtime type OneInstanceMethodFactory in Spring? (Modification of the two third-party factory classes would not be the point on question.)
#EDIT
Thanks to the hints from #Boris Treukhov, finally i figured out that the problem had nothing to do with type downcast but indeed OneStaticMethodFactory#createFactory returned other runtime type than OneInstanceMethodFactory due to a typo myself. Now everything works as expected after the migration from the hardwired code to Spring IoC / DI above.
You should check that 'oneInstanceMethodFactory', is really a OneInstanceMethodFactory instance, either by checking what bean type was created with name "oneInstanceMethodFactory" through the container's getBean() method or by examining the value returned by createFactory in the debugger.
In my experience this exception is very straightforward, usually there's really no such a public non-static method.
Downcasting makes no difference because the methods are called through the reflection method which takes an Object instance as this argument. The bean type is determined by the value returned from the factory method, Spring don't need to know the class before the bean is initialized. In fact oneObject bean depends on oneInstanceMethodFactory so the latter bean will be fully initialized before the former, and the Spring will be able to call its createObject method.

Resources