HowTo: Reference other spring beans in Vert.x Verticle running in Spring

Kidong Lee
2 min readJan 26, 2019

--

Vert.x Verticle can be instantiated just from the java class constructor without parameters. But sometimes, developers want to pass java object as parameter to the verticle, because the passed referenced objects have to be used, for instance, to call DAO service, external API, etc.

If Vert.x verticle is run in Spring, you can do it.

I will show you how to get and use referenced java objects as spring bean in the constructor of Verticle.

In the following listing, SomeVerticle verticle will be deployed:

vertx.deployVerticle("mykidong.SomeVerticle", deploymentOptions, ar -> {
if(ar.succeeded())
{
log.info("http server verticles deployed completely...");
}
else
{
log.error("something wrong: " + ar.cause().toString());
}
});

Let’s see the Verticle class SomeVerticle :

package mykidong;public class SomeVerticle extends AbstractVerticle
{
private Handler<RoutingContext> someHandler;

public SomeVerticle()
{
// hm... how to get 'someHandler' which has to be used in this verticle!
}

@Override
public void start() throws Exception {
...
router.post(uri).handler(MultipartBodyHandler.create()
.setMergeFormAttributes(true));
router.post(uri).handler(someHandler); // HERE!!
...
}
}

As you can see in the constructor SomeVerticle() , someHandler instance has to be got to handle requests in routing context as in router.post(uri).handle(someHandler) .

This someHandler might not only handle multipart file upload, but also call external API, or DAO services which are referenced and injected as spring beans.

someHandler could be defined as a spring bean like this:

<bean id="someHandler" class="mykidong.SomeHandler" p:someDao-ref="someDao" />

To get a spring bean from outside spring context, write a class of ApplicationContextAware like this:

package mykidong;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContext implements ApplicationContextAware {
private static ApplicationContext context;

public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
}
public static ApplicationContext getApplicationContext() {
return context;

}
}

And define it as spring bean:

<bean id="springContext" class="mykidong.SpringContext" />

Now you can get the instance of someHandler from outside of spring context, namely, SomeVerticle verticle constructor.

You can add the following codes to the verticle constructor:

    private Handler<RoutingContext> someHandler;

public SomeVerticle()
{
ApplicationContext applicationContext = SpringContext.getApplicationContext();

someHandler = applicationContext.getBean("someHandler", SomeHandler.class);
}

You can get ApplicationContext from SpringContext as a class of ApplicationContextAware , and finally, someHandler can be referenced from ApplicationContext .

Let’s see the whole codes of verticle SomeVerticle :

package mykidong;public class SomeVerticle extends AbstractVerticle
{
private Handler<RoutingContext> someHandler;

public SomeVerticle()
{
ApplicationContext applicationContext = SpringContext.getApplicationContext();

someHandler = applicationContext.getBean("someHandler", SomeHandler.class);
}

@Override
public void start() throws Exception {
...
router.post(uri).handler(MultipartBodyHandler.create()
.setMergeFormAttributes(true));
router.post(uri).handler(someHandler); // HERE!!
...
}
}

If your verticle is run in spring, you can use references of other spring beans in verticle with ease.

--

--

Kidong Lee
Kidong Lee

Written by Kidong Lee

Founder of Cloud Chef Labs | Chango | Unified Data Lakehouse Platform | Iceberg centric Data Lakehouses https://www.cloudchef-labs.com/

No responses yet