2016-12-05 93 views
0

在我的Grails 3.1.14我的应用程序需要添加一个bean,在异步方法调用的回调创建:添加Spring bean的异步

Vertx.clusteredVertx([:]){ AsyncResult<Vertx> res -> 
    if(res.succeeded()){ 
    Vertx vertx = res.result() // << this should be injected into the appContext 
    } 
} 

,这样的实例可以被装配成跨越其他文物整个应用。

实现此目的的正确方法是什么?

我可以用StaticApplicationContext吗?或者它会破坏什么?

另一种方法是使用“容器bean”并在异步方法完成时设置它,但它有点难看。

回答

0

您可以使用InitializingBean接口创建基于应用程序上下文事件配置其属性的包装bean。

class VertxWrapper implements InitializingBean { 

    Vertx vertx 

    void afterPropertiesSet() throws Exception { 
     Vertx.clusteredVertx([:]){ AsyncResult<Vertx> res -> 
      this.vertx = res.result() // << this should be injected into 
     } 
    } 
    } 
} 

,然后简单地访问vertxWrapper.vertx从已vertxWrapper注入任何豆。