Ranjan
Ranjan

Reputation: 328

Exposing CDI beans from Quarkus extensions

I am trying to implement a Quarkus extension which based on runtime configuration provides a SecurityIdentityAugmentor.

deployment:

MyProcessor

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep 
private MyMarkerConfiguredBuildItem setupAugmentor(MyRecorder recorder, MyAugmentorConfig config, BeanContainerBuildItem beanContainer) {
    recorder.sertConfig(config, beanContainer.getValue();
    return new MyMarkerConfiguredBuildItem ();
}

runtime:

  1. MyRecorder:
public void setConfig(MyAugmentorConfig config, BeanContainer beanContainer) {
    beanContainer.instance(MyProducer.class).setConfig(config);
}
  1. MyProducer :
@ApplicationScoped
public class MyProducer {

    public void setConfig(MyConfig config) {
        this.config = config;
    }

    @Produces
    @ApplicationScoped
    public MyAugmentor createAugmentor() {
        return new MyAugmentor(this.config);
    }
}

The only way I can get MyAugmentor instance produced in my client application is by adding a beans.xml in the extension's runtime module. However I don't see beans.xml in other extensions in the github repo. Could someone point me in the right direction?

Upvotes: 0

Views: 1362

Answers (1)

loicmathieu
loicmathieu

Reputation: 5552

You just need to use a build step to register your producer bean (as it will not be automatically discovered because extensions are not indexed) inside the deployment module of your extension:

@BuildStep
public AdditionalBeanBuildItem producer() {
    return new AdditionalBeanBuildItem(MyProducer.class);
}

The producer must be in the runtime module of your extension, you can inject your configuration inside it.

@ApplicationScoped
public class MyProducer {

    @Inject MyConfig config;

    @Produces
    @ApplicationScoped
    public MyAugmentor createAugmentor() {
        return new MyAugmentor(this.config);
    }
}

This construct is used in a lot of existing extensions, you can see for exemple my Firestore extension here: https://github.com/quarkiverse/quarkus-google-cloud-services/tree/master/firestore

Upvotes: 2

Related Questions