Quarkus - Contexts and Dependency Injection

Quarkus DI solution is based on the Contexts and Dependency Injection for Java 2.0 specification. However, it is not a full CDI implementation verified by the TCK. Only a subset of the CDI features is implemented - see also the list of supported features and the list of limitations.

If you’re new to CDI then we recommend you to read the Introduction to CDI first.
Most of the existing CDI code should work just fine but there are some small differences which follow from the Quarkus architecture and goals.

1. Bean Discovery

Bean discovery in CDI is a complex process which involves legacy deployment structures and accessibility requirements of the underlying module architecture. However, Quarkus is using a simplified bean discovery. There is only single bean archive with the bean discovery mode annotated and no visibility boundaries.

The bean archive is synthesized from:

  • the application classes,

  • dependencies that contain a beans.xml descriptor (content is ignored),

  • dependencies that contain a Jandex index - META-INF/jandex.idx,

  • dependencies referenced by quarkus.index-dependency in application.properties,

  • and Quarkus integration code.

Bean classes that don’t have a bean defining annotation are not discovered. This behavior is defined by CDI. But producer methods and fields and observer methods are discovered even if the declaring class is not annotated with a bean defining annotation (this behavior is different to what is defined in CDI). In fact, the declaring bean classes are considered annotated with @Dependent.

Quarkus extensions may declare additional discovery rules. For example, @Scheduled business methods are registered even if the declaring class is not annotated with a bean defining annotation.

1.1. How to Generate a Jandex Index

A dependency with a Jandex index is automatically scanned for beans. To generate the index just add the following to your pom.xml:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.jboss.jandex</groupId>
  5. <artifactId>jandex-maven-plugin</artifactId>
  6. <version>1.0.7</version>
  7. <executions>
  8. <execution>
  9. <id>make-index</id>
  10. <goals>
  11. <goal>jandex</goal>
  12. </goals>
  13. </execution>
  14. </executions>
  15. </plugin>
  16. </plugins>
  17. </build>

If you can’t modify the dependency, you can still index it by adding quarkus.index-dependency entries to your application.properties:

  1. quarkus.index-dependency.<name>.group-id=
  2. quarkus.index-dependency.<name>.artifact-id=
  3. quarkus.index-dependency.<name>.classifier=(this one is optional)

For example, the following entries ensure that the org.acme:acme-api dependency is indexed:

1.2. How To Exclude Types and Dependencies from Discovery

It may happen that some beans from third-party libraries do not work correctly in Quarkus. A typical example is a bean injecting a portable extension. In such case, it’s possible to exclude types and dependencies from the bean discovery. The quarkus.arc.exclude-types property accepts a list of string values that are used to match classes that should be excluded.

Table 1. Value Examples

Value

Description

org.acme.Foo

Match the fully qualified name of the class

org.acme.

Match classes with package org.acme

org.acme.*

Match classes where the package starts with org.acme

Bar

Match the simple name of the class

Example application.properties

  1. quarkus.arc.exclude-types=org.acme.Foo,org.acme.*,Bar (1)(2)(3)
1Exclude the type org.acme.Foo.
2Exclude all types from the org.acme package.
3Exclude all types whose simple name is Bar

It is also possible to exclude a dependency artifact that would be otherwise scanned for beans. For example, because it contains a beans.xml descriptor.

Example application.properties

  1. quarkus.arc.exclude-dependency.acme.group-id=org.acme (1)
  2. quarkus.arc.exclude-dependency.acme.artifact-id=acme-services (2)
1Value is a group id for a dependency identified by name acme.
2Value is an artifact id for a dependency identified by name acme.

2. Native Executables and Private Members

Quarkus is using GraalVM to build a native executable. One of the limitations of GraalVM is the usage of Reflection. Reflective operations are supported but all relevant members must be registered for reflection explicitly. Those registrations result in a bigger native executable.

And if Quarkus DI needs to access a private member it has to use reflection. That’s why Quarkus users are encouraged not to use private members in their beans. This involves injection fields, constructors and initializers, observer methods, producer methods and fields, disposers and interceptor methods.

How to avoid using private members? You can use package-private modifiers:

  1. @ApplicationScoped
  2. public class CounterBean {
  3. @Inject
  4. CounterService counterService; (1)
  5. void onMessage(@Observes Event msg) { (2)
  6. }
  7. }
1A package-private injection field.
2A package-private observer method.

Or constructor injection:

  1. @ApplicationScoped
  2. public class CounterBean {
  3. private CounterService service;
  4. CounterBean(CounterService service) { (1)
  5. this.service = service;
  6. }
  7. }
1A package-private constructor injection. @Inject is optional in this particular case.

3. Supported Features

  • Programming model

    • Managed beans implemented by a Java class

      • @PostConstruct and @PreDestroy lifecycle callbacks
    • Producer methods and fields, disposers

    • Qualifiers

    • Alternatives

    • Stereotypes

  • Dependency injection and lookup

    • Field, constructor and initializer/setter injection

    • Type-safe resolution

    • Programmatic lookup via javax.enterprise.inject.Instance

    • Client proxies

    • Injection point metadata

  • Scopes and contexts

    • @Dependent, @ApplicationScoped, @Singleton, @RequestScoped and @SessionScoped

    • Custom scopes and contexts

  • Interceptors

    • Business method interceptors: @AroundInvoke

    • Interceptors for lifecycle event callbacks: @PostConstruct, @PreDestroy, @AroundConstruct

  • Events and observer methods, including asynchronous events and transactional observer methods

4. Limitations

  • @ConversationScoped is not supported

  • Decorators are not supported

  • Portable Extensions are not supported

  • BeanManager - only the following methods are implemented: getBeans(), createCreationalContext(), getReference(), getInjectableReference() , resolve(), getContext(), fireEvent(), getEvent() and createInstance()

  • Specialization is not supported

  • beans.xml descriptor content is ignored

  • Passivation and passivating scopes are not supported

  • Interceptor methods on superclasses are not implemented yet

  • @Interceptors is not supported

5. Non-standard Features

5.1. Eager Instantiation of Beans

5.1.1. Lazy By Default

By default, CDI beans are created lazily, when needed. What exactly “needed” means depends on the scope of a bean.

  • A normal scoped bean (@ApplicationScoped, @RequestScoped, etc.) is needed when a method is invoked upon an injected instance (contextual reference per the specification).

    In other words, injecting a normal scoped bean will not suffice because a client proxy is injected instead of a contextual instance of the bean.

  • A bean with a pseudo-scope (@Dependent and @Singleton ) is created when injected.

Lazy Instantiation Example

  1. @Singleton // => pseudo-scope
  2. class AmazingService {
  3. String ping() {
  4. return "amazing";
  5. }
  6. }
  7. @ApplicationScoped // => normal scope
  8. class CoolService {
  9. String ping() {
  10. return "cool";
  11. }
  12. }
  13. @Path("/ping")
  14. public class PingResource {
  15. @Inject
  16. AmazingService s1; (1)
  17. @Inject
  18. CoolService s2; (2)
  19. @GET
  20. public String ping() {
  21. return s1.ping() + s2.ping(); (3)
  22. }
  23. }
1Injection triggers the instantiation of AmazingService.
2Injection itself does not result in the instantiation of CoolService. A client proxy is injected.
3The first invocation upon the injected proxy triggers the instantiation of CoolService.

5.1.2. Startup Event

However, if you really need to instantiate a bean eagerly you can:

  • Declare an observer of the StartupEvent - the scope of the bean does not matter in this case:

    1. @ApplicationScoped
    2. class CoolService {
    3. void startup(@Observes StartupEvent event) { (1)
    4. }
    5. }
    1A CoolService is created during startup to service the observer method invocation.
  • Use the bean in an observer of the StartupEvent - normal scoped beans must be used as described in Lazy By Default:

    1. @Dependent
    2. class MyBeanStarter {
    3. void startup(@Observes StartupEvent event, AmazingService amazing, CoolService cool) { (1)
    4. cool.toString(); (2)
    5. }
    6. }
    1The AmazingService is created during injection.
    2The CoolService is a normal scoped bean so we have to invoke a method upon the injected proxy to force the instantiation.
  • Annotate the bean with @io.quarkus.runtime.Startup as described in Startup annotation:

    1. @Startup (1)
    2. @ApplicationScoped
    3. public class EagerAppBean {
    4. private final String name;
    5. EagerAppBean(NameGenerator generator) { (2)
    6. this.name = generator.createName();
    7. }
    8. }
    1. For each bean annotated with @Startup a synthetic observer of StartupEvent is generated. The default priority is used.

    2. The bean constructor is called when the application starts and the resulting contextual instance is stored in the application context.

Quarkus users are encouraged to always prefer the @Observes StartupEvent to @Initialized(ApplicationScoped.class) as explained in the Application Initialization and Termination guide.

5.2. Request Context Lifecycle

The request context is also active:

  • during notification of a synchronous observer method.

The request context is destroyed:

  • after the observer notification completes for an event, if it was not already active when the notification started.
An event with qualifier @Initialized(RequestScoped.class) is fired when the request context is initialized for an observer notification. Moreover, the events with qualifiers @BeforeDestroyed(RequestScoped.class) and @Destroyed(RequestScoped.class) are fired when the request context is destroyed.

5.3. Qualified Injected Fields

In CDI, if you declare a field injection point you need to use @Inject and optionally a set of qualifiers.

  1. @Inject
  2. @ConfigProperty(name = "cool")
  3. String coolProperty;

In Quarkus, you can skip the @Inject annotation completely if the injected field declares at least one qualifier.

  1. @ConfigProperty(name = "cool")
  2. String coolProperty;
With the notable exception of one special case discussed below, @Inject is still required for constructor and method injection.

5.4. Simplified Constructor Injection

In CDI, a normal scoped bean must always declare a no-args constructor (this constructor is normally generated by the compiler unless you declare any other constructor). However, this requirement complicates constructor injection - you need to provide a dummy no-args constructor to make things work in CDI.

  1. @ApplicationScoped
  2. public class MyCoolService {
  3. private SimpleProcessor processor;
  4. MyCoolService() { // dummy constructor needed
  5. }
  6. @Inject // constructor injection
  7. MyCoolService(SimpleProcessor processor) {
  8. this.processor = processor;
  9. }
  10. }

There is no need to declare dummy constructors for normal scoped bean in Quarkus - they are generated automatically. Also if there’s only one constructor there is no need for @Inject.

  1. @ApplicationScoped
  2. public class MyCoolService {
  3. private SimpleProcessor processor;
  4. MyCoolService(SimpleProcessor processor) {
  5. this.processor = processor;
  6. }
  7. }
We don’t generate a no-args constructor automatically if a bean class extends a class that does not declare a no-args constructor.

5.5. Removing Unused Beans

The container attempts to remove all unused beans during build by default. This optimization can be disabled by setting quarkus.arc.remove-unused-beans to none or false.

An unused bean:

  • is not a built-in bean or an interceptor,

  • is not eligible for injection to any injection point,

  • is not excluded by any extension,

  • does not have a name,

  • does not declare an observer,

  • does not declare any producer which is eligible for injection to any injection point,

  • is not directly eligible for injection into any javax.enterprise.inject.Instance or javax.inject.Provider injection point

This optimization applies to all forms of bean declarations: bean class, producer method, producer field.

Users can instruct the container to not remove any of their specific beans (even if they satisfy all the rules specified above) by annotating them with io.quarkus.arc.Unremovable. This annotation can be placed on the types, producer methods, and producer fields.

Since this is not always possible, there is an option to achieve the same via application.properties. The quarkus.arc.unremovable-types property accepts a list of string values that are used to match beans based on their name or package.

Table 2. Value Examples

Value

Description

org.acme.Foo

Match the fully qualified name of the bean class

org.acme.

Match beans where the package of the bean class is org.acme

org.acme.*

Match beans where the package of the bean class starts with org.acme

Bar

Match the simple name of the bean class

Example application.properties

  1. quarkus.arc.unremovable-types=org.acme.Foo,org.acme.*,Bar

Furthermore, extensions can eliminate possible false positives by producing UnremovableBeanBuildItem.

Finally, Quarkus provides a middle ground for the bean removal optimization where application beans are never removed whether or not they are unused, while the optimization proceeds normally for non application classes. To use this mode, set quarkus.arc.remove-unused-beans to fwk or framework.

When using the dev mode (running ./mvnw clean compile quarkus:dev), you can see more information about which beans are being removed by enabling additional logging via the following line in your application.properties.

  1. quarkus.log.category."io.quarkus.arc.processor".level=DEBUG

5.6. Default Beans

Quarkus adds a capability that CDI currently does not support which is to conditionally declare a bean if no other bean with equal types and qualifiers was declared by any available means (bean class, producer, synthetic bean, …​) This is done using the @io.quarkus.arc.DefaultBean annotation and is best explained with an example.

Say there is a Quarkus extension that among other things declares a few CDI beans like the following code does:

  1. @Dependent
  2. public class TracerConfiguration {
  3. @Produces
  4. public Tracer tracer(Reporter reporter, Configuration configuration) {
  5. return new Tracer(reporter, configuration);
  6. }
  7. @Produces
  8. @DefaultBean
  9. public Configuration configuration() {
  10. // create a Configuration
  11. }
  12. @Produces
  13. @DefaultBean
  14. public Reporter reporter(){
  15. // create a Reporter
  16. }
  17. }

The idea is that the extension auto-configures things for the user, eliminating a lot of boilerplate - we can just @Inject a Tracer wherever it is needed. Now imagine that in our application we would like to utilize the configured Tracer, but we need to customize it a little, for example by providing a custom Reporter. The only thing that would be needed in our application would be something like the following:

  1. @Dependent
  2. public class CustomTracerConfiguration {
  3. @Produces
  4. public Reporter reporter(){
  5. // create a custom Reporter
  6. }
  7. }

@DefaultBean allows extensions (or any other code for that matter) to provide defaults while backing off if beans of that type are supplied in any way Quarkus supports.

5.7. Enabling Beans for Quarkus Build Profile

Quarkus adds a capability that CDI currently does not support which is to conditionally enable a bean when a Quarkus build time profile is enabled, via the @io.quarkus.arc.profile.IfBuildProfile and @io.quarkus.arc.profile.UnlessBuildProfile annotations. When used in conjunction with @io.quarkus.arc.DefaultBean, these annotations allow for the creation of different bean configurations for different build profiles.

Imagine for instance that an application contains a bean named Tracer, which needs to be do nothing when in tests or dev-mode, but works in its normal capacity for the production artifact. An elegant way to create such beans is the following:

  1. @Dependent
  2. public class TracerConfiguration {
  3. @Produces
  4. @IfBuildProfile("prod")
  5. public Tracer realTracer(Reporter reporter, Configuration configuration) {
  6. return new RealTracer(reporter, configuration);
  7. }
  8. @Produces
  9. @DefaultBean
  10. public Tracer noopTracer() {
  11. return new NoopTracer();
  12. }
  13. }

If instead, it is required that the Tracer bean also works in dev-mode and only default to doing nothing for tests, then @UnlessBuildProfile would be ideal. The code would look like:

  1. @Dependent
  2. public class TracerConfiguration {
  3. @Produces
  4. @UnlessBuildProfile("test") // this will be enabled for both prod and dev build time profiles
  5. public Tracer realTracer(Reporter reporter, Configuration configuration) {
  6. return new RealTracer(reporter, configuration);
  7. }
  8. @Produces
  9. @DefaultBean
  10. public Tracer noopTracer() {
  11. return new NoopTracer();
  12. }
  13. }
The runtime profile has absolutely no effect on the bean resolution using @IfBuildProfile and @UnlessBuildProfile.

5.8. Enabling Beans for Quarkus Build Properties

Quarkus adds a capability that CDI currently does not support which is to conditionally enable a bean when a Quarkus build time property has a specific value, via the @io.quarkus.arc.properties.IfBuildProperty annotation. When used in conjunction with @io.quarkus.arc.DefaultBean, this annotation allow for the creation of different bean configurations for different build properties.

The scenario we mentioned above with Tracer could also be implemented in the following way:

  1. @Dependent
  2. public class TracerConfiguration {
  3. @Produces
  4. @IfBuildProperty(name = "some.tracer.enabled", stringValue = "true")
  5. public Tracer realTracer(Reporter reporter, Configuration configuration) {
  6. return new RealTracer(reporter, configuration);
  7. }
  8. @Produces
  9. @DefaultBean
  10. public Tracer noopTracer() {
  11. return new NoopTracer();
  12. }
  13. }
Properties set at runtime have absolutely no effect on the bean resolution using @IfBuildProperty.

5.9. Declaring Selected Alternatives

In CDI, an alternative bean may be selected either globally for an application by means of @Priority, or for a bean archive using a beans.xml descriptor. Quarkus has a simplified bean discovery and the content of beans.xml is ignored.

The disadvantage of @Priority is that it has @Target({ TYPE, PARAMETER }) and so it cannot be used for producer methods and fields. To address this problem and to simplify the code Quarkus provides the io.quarkus.arc.AlternativePriority annotation. It’s basically a shortcut for @Alternative plus @Priority. Additionally, it can be used for producers.

However, it is also possible to select alternatives for an application using the unified configuration. The quarkus.arc.selected-alternatives property accepts a list of string values that are used to match alternative beans. If any value matches then the priority of Integer#MAX_VALUE is used for the relevant bean. The priority declared via @Priority or @AlternativePriority is overridden.

Table 3. Value Examples

Value

Description

org.acme.Foo

Match the fully qualified name of the bean class or the bean class of the bean that declares the producer

org.acme.

Match beans where the package of the bean class is org.acme

org.acme.*

Match beans where the package of the bean class starts with org.acme

Bar

Match the simple name of the bean class or the bean class of the bean that declares the producer

Example application.properties

  1. quarkus.arc.selected-alternatives=org.acme.Foo,org.acme.*,Bar

5.10. Simplified Producer Method Declaration

In CDI, a producer method must be always annotated with @Produces.

  1. class Producers {
  2. @Inject
  3. @ConfigProperty(name = "cool")
  4. String coolProperty;
  5. @Produces
  6. @ApplicationScoped
  7. MyService produceService() {
  8. return new MyService(coolProperty);
  9. }
  10. }

In Quarkus, you can skip the @Produces annotation completely if the producer method is annotated with a scope annotation, a stereotype or a qualifier.

  1. class Producers {
  2. @ConfigProperty(name = "cool")
  3. String coolProperty;
  4. @ApplicationScoped
  5. MyService produceService() {
  6. return new MyService(coolProperty);
  7. }
  8. }

5.11. Interception of Static Methods

The Interceptors specification is clear that around-invoke methods must not be declared static. However, this restriction was driven mostly by technical limitations. And since Quarkus is a build-time oriented stack that allows for additional class transformations, those limitations don’t apply anymore. It’s possible to annotate a non-private static method with an interceptor binding:

  1. class Services {
  2. @Logged (1)
  3. static BigDecimal computePrice(long amout) { (2)
  4. BigDecimal price;
  5. // Perform computations...
  6. return price;
  7. }
  8. }
1Logged is an interceptor binding.
2Each method invocation is intercepted if there is an interceptor associated with Logged.

5.11.1. Limitations

  • Only method-level bindings are considered for backward compatibility reasons (otherwise static methods of bean classes that declare class-level bindings would be suddenly intercepted)

  • Private static methods are never intercepted

  • InvocationContext#getTarget() returns null for obvious reasons; therefore not all existing interceptors may behave correctly when intercepting static methods

    Interceptors can use InvocationContext.getMethod() to detect static methods and adjust the behavior accordingly.

5.12. Ability to handle ‘final’ classes and methods

In normal CDI, classes that are marked as final and / or have final methods are not eligible for proxy creation, which in turn means that interceptors and normal scoped beans don’t work properly. This situation is very common when trying to use CDI with alternative JVM languages like Kotlin where classes and methods are final by default.

Quarkus however, can overcome these limitations when quarkus.arc.transform-unproxyable-classes is set to true (which is the default value).

5.13. Container-managed Concurrency

There is no standard concurrency control mechanism for CDI beans. Nevertheless, a bean instance can be shared and accessed concurrently from multiple threads. In that case it should be thread-safe. You can use standard Java constructs (volatile, synchronized, ReadWriteLock, etc.) or let the container control the concurrent access. Quarkus provides @io.quarkus.arc.Lock and a built-in interceptor for this interceptor binding. Each interceptor instance associated with a contextual instance of an intercepted bean holds a separate ReadWriteLock with non-fair ordering policy.

io.quarkus.arc.Lock is a regular interceptor binding and as such can be used for any bean with any scope. However, it is especially useful for “shared” scopes, e.g. @Singleton and @ApplicationScoped.

Container-managed Concurrency Example

  1. import io.quarkus.arc.Lock;
  2. @Lock (1)
  3. @ApplicationScoped
  4. class SharedService {
  5. void addAmount(BigDecimal amout) {
  6. // ...changes some internal state of the bean
  7. }
  8. @Lock(value = Lock.Type.READ, time = 1, unit = TimeUnit.SECONDS) (2) (3)
  9. BigDecimal getAmount() {
  10. // ...it is safe to read the value concurrently
  11. }
  12. }
1@Lock (which maps to @Lock(Lock.Type.WRITE)) declared on the class instructs the container to lock the bean instance for any invocation of any business method, i.e. the client has “exclusive access” and no concurrent invocations will be allowed.
2@Lock(Lock.Type.READ) overrides the value specified at class level. It means that any number of clients can invoke the method concurrently, unless the bean instance is locked by @Lock(Lock.Type.WRITE).
3You can also specify the “wait time”. If it’s not possible to acquire the lock in the given time a LockException is thrown.

6. Build Time Extension Points

6.1. Portable Extensions

Quarkus incorporates build-time optimizations in order to provide instant startup and low memory footprint. The downside of this approach is that CDI Portable Extensions cannot be supported. Nevertheless, most of the functionality can be achieved using Quarkus extensions.

6.2. Additional Bean Defining Annotations

As described in Bean Discovery bean classes that don’t have a bean defining annotation are not discovered. However, BeanDefiningAnnotationBuildItem can be used to extend the set of default bean defining annotations (@Dependent, @Singleton, @ApplicationScoped, @RequestScoped and @Stereotype annotations):

  1. @BuildStep
  2. BeanDefiningAnnotationBuildItem additionalBeanDefiningAnnotation() {
  3. return new BeanDefiningAnnotationBuildItem(DotName.createSimple("javax.ws.rs.Path")));
  4. }
Bean registrations that are result of a BeanDefiningAnnotationBuildItem are unremovable by default. See also Removing Unused Beans.

6.3. Resource Annotations

ResourceAnnotationBuildItem is used to specify resource annotations that make it possible to resolve non-CDI injection points, such as Java EE resources.

An integrator must also provide a corresponding io.quarkus.arc.ResourceReferenceProvider implementation.
  1. @BuildStep
  2. void setupResourceInjection(BuildProducer<ResourceAnnotationBuildItem> resourceAnnotations, BuildProducer<GeneratedResourceBuildItem> resources) {
  3. resources.produce(new GeneratedResourceBuildItem("META-INF/services/io.quarkus.arc.ResourceReferenceProvider",
  4. JPAResourceReferenceProvider.class.getName().getBytes()));
  5. resourceAnnotations.produce(new ResourceAnnotationBuildItem(DotName.createSimple(PersistenceContext.class.getName())));
  6. }

6.4. Additional Beans

AdditionalBeanBuildItem is used to specify additional bean classes to be analyzed during discovery. Additional bean classes are transparently added to the application index processed by the container.

  1. @BuildStep
  2. List<AdditionalBeanBuildItem> additionalBeans() {
  3. return Arrays.asList(
  4. new AdditionalBeanBuildItem(SmallRyeHealthReporter.class),
  5. new AdditionalBeanBuildItem(HealthServlet.class));
  6. }
A bean registration that is a result of an AdditionalBeanBuildItem is removable by default. See also Removing Unused Beans.

6.5. Synthetic Beans

Sometimes it is very useful to be able to register a synthetic bean. Bean attributes of a synthetic bean are not derived from a java class, method or field. Instead, the attributes are specified by an extension. In CDI, this could be achieved using the AfterBeanDiscovery.addBean() methods. In Quarkus, there are three ways to register a synthetic bean.

6.5.1. BeanRegistrarBuildItem

A build step can produce a BeanRegistrarBuildItem and leverage the io.quarkus.arc.processor.BeanConfigurator API to build a synthetic bean definition.

BeanRegistrarBuildItem Example

  1. @BuildStep
  2. BeanRegistrarBuildItem syntheticBean() {
  3. return new BeanRegistrarBuildItem(new BeanRegistrar() {
  4. @Override
  5. public void register(RegistrationContext registrationContext) {
  6. registrationContext.configure(String.class).types(String.class).qualifiers(new MyQualifierLiteral()).creator(mc -> mc.returnValue(mc.load("foo"))).done();
  7. }
  8. }));
  9. }
The output of a BeanConfigurator is recorded as bytecode. Therefore there are some limitations in how a synthetic bean instance is created. See also BeanConfigurator.creator() methods.
You can easily filter all class-based beans via the convenient BeanStream returned from the RegistrationContext.beans() method.

6.5.2. BeanRegistrationPhaseBuildItem

If a build step needs to produce other build items during the registration it should use the BeanRegistrationPhaseBuildItem. The reason is that injected objects are only valid during a @BuildStep method invocation.

BeanRegistrationPhaseBuildItem Example

  1. @BuildStep
  2. void syntheticBean(BeanRegistrationPhaseBuildItem beanRegistrationPhase,
  3. BuildProducer<MyBuildItem> myBuildItem,
  4. BuildProducer<BeanConfiguratorBuildItem> beanConfigurators) {
  5. beanConfigurators.produce(new BeanConfiguratorBuildItem(beanRegistrationPhase.getContext().configure(String.class).types(String.class).qualifiers(new MyQualifierLiteral()).creator(mc -> mc.returnValue(mc.load("foo")))));
  6. myBuildItem.produce(new MyBuildItem());
  7. }
See the BeanRegistrationPhaseBuildItem javadoc for more information.

6.5.3. SyntheticBeanBuildItem

Finally, a build step can produce a SyntheticBeanBuildItem to register a synthetic bean whose instance can be easily produced through a recorder. The extended BeanConfigurator accepts either a io.quarkus.runtime.RuntimeValue or a java.util.function.Supplier.

SyntheticBeanBuildItem Example

  1. @BuildStep
  2. @Record(STATIC_INIT)
  3. SyntheticBeanBuildItem syntheticBean(TestRecorder recorder) {
  4. return SyntheticBeanBuildItem.configure(Foo.class).scope(Singleton.class)
  5. .runtimeValue(recorder.createFoo())
  6. .done();
  7. }

6.6. Annotation Transformations

A very common task is to override the annotations found on the bean classes. For example you might want to add an interceptor binding to a specific bean class. Here is how to do it - use the AnnotationsTransformerBuildItem:

  1. @BuildStep
  2. AnnotationsTransformerBuildItem transform() {
  3. return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
  4. public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) {
  5. return kind == org.jboss.jandex.AnnotationTarget.Kind.CLASS;
  6. }
  7. public void transform(TransformationContext context) {
  8. if (context.getTarget().asClass().name().toString().equals("com.foo.Bar")) {
  9. context.transform().add(MyInterceptorBinding.class).done();
  10. }
  11. }
  12. });
  13. }

6.7. Additional Interceptor Bindings

In rare cases it might be handy to programmatically register an existing annotation as interceptor binding. This is similar to what pure CDI achieves through BeforeBeanDiscovery#addInterceptorBinding(). Though here we are going to use InterceptorBindingRegistrarBuildItem to get it done. Note that you can register multiple annotations in one go:

  1. @BuildStep
  2. InterceptorBindingRegistrarBuildItem addInterceptorBindings() {
  3. InterceptorBindingRegistrarBuildItem additionalBindingsRegistrar = new InterceptorBindingRegistrarBuildItem(new InterceptorBindingRegistrar() {
  4. @Override
  5. public Collection<DotName> registerAdditionalBindings() {
  6. Collection<DotName> result = new HashSet<>();
  7. result.add(DotName.createSimple(MyAnnotation.class.getName()));
  8. result.add(DotName.createSimple(MyOtherAnnotation.class.getName()));
  9. return result;
  10. }
  11. });
  12. return additionalBindingsRegistrar;
  13. }

6.8. Injection Point Transformation

Every now and then it is handy to be able to change qualifiers of an injection point programmatically. You can do just that with InjectionPointTransformerBuildItem. The following sample shows how to apply transformation to injection points with type Foo that contain qualifier MyQualifier:

  1. @BuildStep
  2. InjectionPointTransformerBuildItem transformer() {
  3. return new InjectionPointTransformerBuildItem(new InjectionPointsTransformer() {
  4. public boolean appliesTo(Type requiredType) {
  5. return requiredType.name().equals(DotName.createSimple(Foo.class.getName()));
  6. }
  7. public void transform(TransformationContext context) {
  8. if (context.getQualifiers().stream()
  9. .anyMatch(a -> a.name().equals(DotName.createSimple(MyQualifier.class.getName())))) {
  10. context.transform()
  11. .removeAll()
  12. .add(DotName.createSimple(MyOtherQualifier.class.getName()))
  13. .done();
  14. }
  15. }
  16. });
  17. }

6.9. Observer Transformation

Any observer method definition can be vetoed or transformed using an ObserverTransformerBuildItem. The attributes that can be transformed include:

  1. @BuildStep
  2. ObserverTransformerBuildItem transformer() {
  3. return new ObserverTransformerBuildItem(new ObserverTransformer() {
  4. public boolean appliesTo(Type observedType, Set<AnnotationInstance> qualifiers) {
  5. return observedType.name.equals(DotName.createSimple(MyEvent.class.getName()));
  6. }
  7. public void transform(TransformationContext context) {
  8. // Veto all observers of MyEvent
  9. context.veto();
  10. }
  11. });
  12. }

6.10. Bean Deployment Validation

Once the bean deployment is ready an extension can perform additional validations and inspect the found beans, observers and injection points. Register a BeanDeploymentValidatorBuildItem:

  1. @BuildStep
  2. BeanDeploymentValidatorBuildItem beanDeploymentValidator() {
  3. return new BeanDeploymentValidatorBuildItem(new BeanDeploymentValidator() {
  4. public void validate(ValidationContext validationContext) {
  5. for (InjectionPointInfo injectionPoint : validationContext.get(Key.INJECTION_POINTS)) {
  6. System.out.println("Injection point: " + injectionPoint);
  7. }
  8. }
  9. });
  10. }
You can easily filter all registered beans via the convenient BeanStream returned from the ValidationContext.beans() method.

If an extension needs to produce other build items during the “validation” phase it should use the ValidationPhaseBuildItem instead. The reason is that injected objects are only valid during a @BuildStep method invocation.

  1. @BuildStep
  2. void validate(ValidationPhaseBuildItem validationPhase,
  3. BuildProducer<MyBuildItem> myBuildItem,
  4. BuildProducer<ValidationErrorBuildItem> errors) {
  5. if (someCondition) {
  6. errors.produce(new ValidationErrorBuildItem(new IllegalStateException()));
  7. myBuildItem.produce(new MyBuildItem());
  8. }
  9. }
See ValidationPhaseBuildItem javadoc for more information.

6.11. Custom Contexts

An extension can register a custom InjectableContext implementation by means of a ContextRegistrarBuildItem:

  1. @BuildStep
  2. ContextRegistrarBuildItem customContext() {
  3. return new ContextRegistrarBuildItem(new ContextRegistrar() {
  4. public void register(RegistrationContext registrationContext) {
  5. registrationContext.configure(CustomScoped.class).normal().contextClass(MyCustomContext.class).done();
  6. }
  7. });
  8. }

If an extension needs to produce other build items during the “context registration” phase it should use the ContextRegistrationPhaseBuildItem instead. The reason is that injected objects are only valid during a @BuildStep method invocation.

  1. @BuildStep
  2. void addContext(ContextRegistrationPhaseBuildItem contextRegistrationPhase,
  3. BuildProducer<MyBuildItem> myBuildItem,
  4. BuildProducer<ContextConfiguratorBuildItem> contexts) {
  5. contexts.produce(new ContextConfiguratorBuildItem(contextRegistrationPhase.getContext().configure(CustomScoped.class).normal().contextClass(MyCustomContext.class)));
  6. myBuildItem.produce(new MyBuildItem());
  7. }
See ContextRegistrationPhaseBuildItem javadoc for more information.

6.12. Available Build Time Metadata

Any of the above extensions that operates with BuildExtension.BuildContext can leverage certain build time metadata that are generated during build. The built-in keys located in io.quarkus.arc.processor.BuildExtension.Key are:

  • ANNOTATION_STORE

    • Contains an AnnotationStore that keeps information about all AnnotationTarget annotations after application of annotation transformers
  • INJECTION_POINTS

    • Collection<InjectionPointInfo> containing all injection points
  • BEANS

    • Collection<BeanInfo> containing all beans
  • REMOVED_BEANS

  • OBSERVERS

    • Collection<ObserverInfo> containing all observers
  • SCOPES

    • Collection<ScopeInfo> containing all scopes, including custom ones
  • QUALIFIERS

    • Map<DotName, ClassInfo> containing all qualifiers
  • INTERCEPTOR_BINDINGS

    • Map<DotName, ClassInfo> containing all interceptor bindings
  • STEREOTYPES

    • Map<DotName, ClassInfo> containing all stereotypes

To get hold of these, simply query the extension context object for given key. Note that these metadata are made available as build proceeds which means that extensions can only leverage metadata that were build before they are invoked. If your extension attempts to retrieve metadata that wasn’t yet produced, null will be returned. Here is a summary of which extensions can access which metadata:

  • AnnotationsTransformer

    • Shouldn’t rely on any metadata as this is one of the first CDI extensions invoked
  • ContextRegistrar

    • Has access to ANNOTATION_STORE
  • InjectionPointsTransformer

    • Has access to ANNOTATION_STORE, QUALIFIERS, INTERCEPTOR_BINDINGS, STEREOTYPES
  • ObserverTransformer

    • Has access to ANNOTATION_STORE, QUALIFIERS, INTERCEPTOR_BINDINGS, STEREOTYPES
  • BeanRegistrar

    • Has access to ANNOTATION_STORE, QUALIFIERS, INTERCEPTOR_BINDINGS, STEREOTYPES, BEANS
  • BeanDeploymentValidator

    • Has access to all build metadata

7. Development Mode

In the development mode, two special endpoints are registered automatically to provide some basic debug info in the JSON format:

  • HTTP GET /quarkus/arc/beans - returns the list of all beans

    • You can use query params to filter the output:

      • scope - include beans with scope that ends with the given value, i.e. [http://localhost:8080/quarkus/arc/beans?scope=ApplicationScoped](http://localhost:8080/quarkus/arc/beans?scope=ApplicationScoped)

      • beanClass - include beans with bean class that starts with the given value, i.e. [http://localhost:8080/quarkus/arc/beans?beanClass=org.acme.Foo](http://localhost:8080/quarkus/arc/beans?beanClass=org.acme.Foo)

      • kind - include beans of the specified kind (CLASS, PRODUCER_FIELD, PRODUCER_METHOD, INTERCEPTOR or SYNTHETIC), i.e. [http://localhost:8080/quarkus/arc/beans?kind=PRODUCER_METHOD](http://localhost:8080/quarkus/arc/beans?kind=PRODUCER_METHOD)

  • HTTP GET /quarkus/arc/observers - returns the list of all observer methods
These endpoints are only available in the development mode, i.e. when you run your application via mvn quarkus:dev (or ./gradlew quarkusDev).

8. ArC Configuration Reference