I currently have a spring bean which has a method annotated with @Scheduled to support automatic refresh of its state from external source. I have also annotated the bean with scope as prototype. Now, I am trying to have a pool of these beans and use them in one of my service. When the application starts I get the error below
Need to invoke method 'refreshModelManager' declared on target class 'ModelManagerService', but not found in any interface(s) of the exposed proxy type. Either pull the method up to an interface or switch to CGLIB proxies by enforcing proxy-target-class mode in your configuration.
I have Autowired the ModelManagerServicePool in my other service and using the getTarget() and releaseTarget API to get access to the model manager object.
I read in Spring documentation that @Scheduled is supported in non-singleton beans as of Spring 4.3.x, so I suspect there is something wrong with the way I am using the object pool or the way the instances of ModelManagerService are getting created in the object pool. Any suggestions would be very helpful. I tried setting the property 'spring.aop.proxy-target-class=true' but it didn't help.
Source Code:
@Configuration
public class DataApplicationConfiguration {
@Bean(initMethod="initializeMinIdleObjects")
public ModelManagerServicePool modelManagerServicePool() {
ModelManagerServicePool modelManagerServicePool =
new ModelManagerServicePool();
modelManagerServicePool.setMinIdle(1);
modelManagerServicePool.setMaxSize(1);
modelManagerServicePool.setMaxIdle(1);
modelManagerServicePool.setTargetBeanName("modelManagerService");
modelManagerServicePool.setTargetClass(ModelManagerService.class);
return modelManagerServicePool;
}
}
public class ModelManagerServicePool extends CommonsPool2TargetSource {
public void initializeMinIdleObjects() throws Exception {
List<ModelManagerService> services = new ArrayList<ModelManagerService>();
for(int i = 0; i < getMinIdle(); i++) {
services.add((ModelManagerService) this.getTarget());
}
for(ModelManagerService service : services) {
this.releaseTarget(service);
}
services.clear();
}
}
@Service("modelManagerService")
@Scope("prototype")
public class ModelManagerService {
private AtomicReference<ModelManager> ar =
new AtomicReference<ModelManager>();
@Scheduled(initialDelay = 5000, fixedDelayString = "${modelmanagerservice.refresh.internal}")
public void refreshModelManager() {
//Refresh state of the model manager
}