0

我目前有一个 spring bean,它有一个用 @Scheduled 注释的方法,以支持从外部源自动刷新其状态。我还用范围作为原型对 bean 进行了注释。现在,我正在尝试拥有一个这些 bean 池并将它们用于我的一项服务中。当应用程序启动时,我收到以下错误

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.

我已经在我的其他服务中自动装配了 ModelManagerServicePool,并使用 getTarget() 和 releaseTarget API 来访问模型管理器对象。

我在 Spring 文档中读到,从 Spring 4.3.x 开始,非单例 bean 支持 @Scheduled,所以我怀疑我使用对象池的方式或 ModelManagerService 实例在对象池。任何建议都会非常有帮助。我尝试设置属性“spring.aop.proxy-target-class=true”,但没有帮助。

源代码:

@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
   }
4

0 回答 0