Quarkus - Introduction to Contexts and Dependency Injection

In this guide we’re going to describe the basic principles of the Quarkus programming model that is based on the Contexts and Dependency Injection for Java 2.0 specification.

1. Q: OK. Let’s start simple. What is a bean?

A: Well, a bean is a container-managed object that supports a set of basic services, such as injection of dependencies, lifecycle callbacks and interceptors.

2. Q: Wait a minute. What does “container-managed” mean?

A: Simply put, you don’t control the lifecycle of the object instance directly. Instead, you can affect the lifecycle through declarative means, such as annotations, configuration, etc. The container is the environment where your application runs. It creates and destroys the instances of beans, associates the instances with a designated context, and injects them into other beans.

3. Q: What is it good for?

A: An application developer can focus on the business logic rather than finding out “where and how” to obtain a fully initialized component with all of its dependencies.

You’ve probably heard of the inversion of control (IoC) programming principle. Dependency injection is one of the implementation techniques of IoC.

4. Q: What does a bean look like?

A: There are several kinds of beans. The most common ones are class-based beans:

Simple Bean Example

  1. import javax.inject.Inject;
  2. import javax.enterprise.context.ApplicationScoped;
  3. import org.eclipse.microprofile.metrics.annotation.Counted;
  4. @ApplicationScoped (1)
  5. public class Translator {
  6. @Inject
  7. Dictionary dictionary; (2)
  8. @Counted (3)
  9. String translate(String sentence) {
  10. // ...
  11. }
  12. }
1This is a scope annotation. It tells the container which context to associate the bean instance with. In this particular case, a single bean instance is created for the application and used by all other beans that inject Translator.
2This is a field injection point. It tells the container that Translator depends on the Dictionary bean. If there is no matching bean the build fails.
3This is an interceptor binding annotation. In this case, the annotation comes from the MicroProfile Metrics. The relevant interceptor intercepts the invocation and updates the relevant metrics. We will about interceptors later.

5. Q: Nice. How does the dependency resolution work? I see no names or identifiers.

A: That’s a good question. In CDI the process of matching a bean to an injection point is type-safe. Each bean declares a set of bean types. In our example above, the Translator bean has two bean types: Translator and java.lang.Object. Subsequently, a bean is assignable to an injection point if the bean has a bean type that matches the required type and has all the required qualifiers. We’ll talk about qualifiers later. For now, it’s enough to know that the bean above is assignable to an injection point of type Translator and java.lang.Object.

6. Q: Hm, wait a minute. What happens if multiple beans declare the same type?

A: There is a simple rule: exactly one bean must be assignable to an injection point, otherwise the build fails. If none is assignable the build fails with UnsatisfiedResolutionException. If multiple are assignable the build fails with AmbiguousResolutionException. This is very useful because your application fails fast whenever the container is not able to find an unambiguous dependency for any injection point.

Your can use programmatic lookup via javax.enterprise.inject.Instance to resolve ambiguities at runtime and even iterate over all beans implementing a given type:

  1. public class Translator {
  2. @Inject
  3. Instance<Dictionary> dictionaries; (1)
  4. String translate(String sentence) {
  5. for (Dictionary dict : dictionaries) { (2)
  6. // …
  7. }
  8. }
  9. }
1This injection point will not result in an ambiguous dependency even if there are multiple beans that implement the Dictionary type.
2javax.enterprise.inject.Instance extends Iterable.

7. Q: Can I use setter and constructor injection?

A: Yes, you can. In fact, in CDI the “setter injection” is superseded by more powerful initializer methods. Intializers may accept multiple parameters and don’t have to follow the JavaBean naming conventions.

Initialized and Constructor Injection Example

  1. @ApplicationScoped
  2. public class Translator {
  3. private final TranslatorHelper helper
  4. Translator(TranslatorHelper helper) { (1)
  5. this.helper = helper;
  6. }
  7. @Inject (2)
  8. void setDeps(Dictionary dic, LocalizationService locService) { (3)
  9. / ...
  10. }
  11. }
1This is a constructor injection. In fact, this code would not work in regular CDI implementations where a bean with a normal scope must always declare a no-args constructor and the bean constructor must be annotated with @Inject. However, in Quarkus we detect the absence of no-args constructor and “add” it directly in the bytecode. It’s also not necessary to add @Inject if there is only one constructor present.
2An initializer method must be annotated with @Inject.
3An initializer may accept multiple parameters - each one is an injection point.

8. Q: You talked about some qualifiers?

A: Qualifiers are annotations that help the container to distinguish beans that implement the same type. As we already said a bean is assignable to an injection point if it has all the required qualifiers. If you declare no qualifier at an injection point the @Default qualifier is assumed.

A qualifier type is a Java annotation defined as @Retention(RUNTIME) and annotated with the @javax.inject.Qualifier meta-annotation:

Qualifier Example

  1. @Qualifier
  2. @Retention(RUNTIME)
  3. @Target({METHOD, FIELD, PARAMETER, TYPE})
  4. public @interface Superior {}

The qualifiers of a bean are declared by annotating the bean class or producer method or field with the qualifier types:

Bean With Custom Qualifier Example

  1. @Superior (1)
  2. @ApplicationScoped
  3. public class SuperiorTranslator extends Translator {
  4. String translate(String sentence) {
  5. // ...
  6. }
  7. }
1@Superior is a qualifier annotation.

This bean would be assignable to @Inject @Superior Translator and @Inject @Superior SuperiorTranslator but not to @Inject Translator. The reason is that @Inject Translator is automatically transformed to @Inject @Default Translator during typesafe resolution. And since our SuperiorTranslator does not declare @Default only the original Translator bean is assignable.

9. Q: Looks good. What is the bean scope?

The scope of a bean determines the lifecycle of its instances, i.e. when and where an instance should be created and destroyed.

Every bean has exactly one scope.

10. Q: What scopes can I actually use in my Quarkus application?

A: You can use all the built-in scopes mentioned by the specification except for javax.enterprise.context.ConversationScoped.

AnnotationDescription

@javax.enterprise.context.ApplicationScoped

A single bean instance is used for the application and shared among all injection points. The instance is created lazily, i.e. once a method is invoked upon the client proxy.

@javax.inject.Singleton

Just like @ApplicationScoped except that no client proxy is used. The instance is created when an injection point that resolves to a @Singleton bean is being injected.

@javax.enterprise.context.RequestScoped

The bean instance is associated with the current request (usually an HTTP request).

@javax.enterprise.context.Dependent

This is a pseudo-scope. The instances are not shared and every injection point spawns a new instance of the dependent bean. The lifecycle of dependent bean is bound to the bean injecting it - it will be created and destroyed along with the bean injecting it.

@javax.enterprise.context.SessionScoped

This scope is backed by an javax.servlet.http.HttpSession object. It’s only available if the quarkus-undertow extension is used.

There can be other custom scopes provided by Quarkus extensions. For example, quarkus-narayana-jta provides javax.transaction.TransactionScoped.

11. Q: I don’t undestand the concept of client proxies.

Indeed, the client proxies could be hard to grasp but they provide some useful functionality. A client proxy is basically an object that delegates all method invocations to a target bean instance. It’s a container construct that implements io.quarkus.arc.ClientProxy and extends the bean class.

Generated Client Proxy Example

  1. @ApplicationScoped
  2. class Translator {
  3. String translate(String sentence) {
  4. // ...
  5. }
  6. }
  7. // The client proxy class is generated and looks like...
  8. class Translator_ClientProxy extends Translator { (1)
  9. String translate(String sentence) {
  10. // Find the correct translator instance...
  11. Translator translator = getTranslatorInstanceFromTheApplicationContext();
  12. // And delegate the method invocation...
  13. return translator.translate(sentence);
  14. }
  15. }
1The Translator_ClientProxy instance is always injected instead of a direct reference to a contextual instance of the Translator bean.

Client proxies allow for:

  • Lazy instantiation - the instance is created once a method is invoked upon the proxy.

  • Ability to inject a bean with “narrower” scope to a bean with “wider” scope; i.e. you can inject a @RequestScoped bean into an @ApplicationScoped bean.

  • Circular dependencies in the dependency graph. Having circular dependencies is often an indication that a redesign should be considered, but sometimes it’s inevitable.

  • In rare cases it’s practical to destroy the beans manually. A direct injected reference would lead to a stale bean instance.

12. Q: OK. You said that there are several kinds of beans?

A: Yes. In general, we distinguish:

  1. Class beans

  2. Producer methods

  3. Producer fields

  4. Synthetic beans

Synthetic beans are usually provided by extensions. Therefore, we not going to cover them in this guide.

Producer methods and fields are useful if you need additional control over instantiation of a bean. They are also useful when integrating third-party libraries where you don’t control the class source and may not add additional annotations etc.

Producers Example

  1. @ApplicationScoped
  2. public class Producers {
  3. @Produces (1)
  4. double pi = Math.PI; (2)
  5. @Produces (3)
  6. List<String> names() {
  7. List<String> names = new ArrayList<>();
  8. names.add("Andy");
  9. names.add("Adalbert");
  10. names.add("Joachim");
  11. return names; (4)
  12. }
  13. }
  14. @ApplicationScoped
  15. public class Consumer {
  16. @Inject
  17. double pi;
  18. @Inject
  19. List<String> names;
  20. // ...
  21. }
1The container analyses the field annotations to build a bean metadata. The type is used to build the set of bean types. In this case, it will be double and java.lang.Object. No scope annotation is declared and so it’s defaulted to @Dependent.
2The container will read this field when creating the bean instance.
3The container analyses the method annotations to build a bean metadata. The return type is used to build the set of bean types. In this case, it will be List<String>, Collection<String>, Iterable<String> and java.lang.Object. No scope annotation is declared and so it’s defaulted to @Dependent.
4The container will call this method when creating the bean instance.

There’s more about producers. You can declare qualifiers, inject dependencies into the producer methods parameters, etc. You can read more about producers for example in the Weld docs.

13. Q: OK, injection looks cool. What other services are provided?

13.1. Lifecycle Callbacks

A bean class may declare lifecycle @PostConstruct and @PreDestroy callbacks:

Lifecycle Callbacks Example

  1. import javax.annotation.PostConstruct;
  2. import javax.annotation.PreDestroy;
  3. @ApplicationScoped
  4. public class Translator {
  5. @PostConstruct (1)
  6. void init() {
  7. // ...
  8. }
  9. @PreDestroy (2)
  10. void destroy() {
  11. // ...
  12. }
  13. }
1This callback is invoked before the bean instance is put into service. It is safe to perform some initialization here.
2This callback is invoked before the bean instance is destroyed. It is safe to perform some cleanup tasks here.
It’s a good practice to keep the logic in the callbacks “without side effects”, i.e. you should avoid calling other beans inside the callbacks.

13.2. Interceptors

Interceptors are used to separate cross-cutting concerns from business logic. There is a separate specification - Java Interceptors - that defines the basic programming model and semantics.

Simple Interceptor Example

  1. import javax.interceptor.Interceptor;
  2. import javax.annotation.Priority;
  3. @Logged (1)
  4. @Priority(2020) (2)
  5. @Interceptor (3)
  6. public class LoggingInterceptor {
  7. @Inject (4)
  8. Logger logger;
  9. @AroundInvoke (5)
  10. Objec logInvocation(InvocationContext context) {
  11. // ...log before
  12. Objec ret = context.proceed(); (6)
  13. // ...log after
  14. return ret;
  15. }
  16. }
1This is an interceptor binding annotation that is used to bind our interceptor to a bean. Simply annotate a bean class with @Logged.
2Priority enables the interceptor and affects the interceptor ordering. Interceptors with smaller priority values are called first.
3Marks an interceptor component.
4An interceptor instance may be the target of dependency injection.
5AroundInvoke denotes a method that interposes on business methods.
6Proceed to the next interceptor in the interceptor chain or invoke the intercepted business method.
Instances of interceptors are dependent objects of the bean instance they intercept, i.e. a new interceptor instance is created for each intercepted bean.

13.3. Events and Observers

Beans may also produce and consume events to interact in a completely decoupled fashion. Any Java object can serve as an event payload. The optional qualifiers act as topic selectors.

Simple Event Example

  1. class TaskCompleted {
  2. // ...
  3. }
  4. @ApplicationScoped
  5. class ComplicatedService {
  6. @Inject
  7. Event<Task> event; (1)
  8. void doSomething() {
  9. // ...
  10. event.fire(new TaskCompleted()); (2)
  11. }
  12. }
  13. @ApplicationScoped
  14. class Logger {
  15. void onTaskCompleted(@Observes TaskCompleted task) { (3)
  16. // ...log the task
  17. }
  18. }
1javax.enterprise.event.Event is used to fire events.
2Fire the event synchronously.
3This method is notified when a TaskCompleted event is fired.
For more info about events/observers visit Weld docs.

14. Conclusion

In this guide, we’ve covered some of the basic topics of the Quarkus programming model that is based on the Contexts and Dependency Injection for Java 2.0 specification. However, a full CDI implementation is not used under the hood. Quarkus only implements a subset of the CDI features - see also the list of supported features and the list of limitations. On the other hand, there are quite a few non-standard features and Quarkus-specific APIs. We believe that our efforts will drive the innovation of the CDI specification towards the build-time oriented developer stacks in the future.

If you wish to learn more about Quarkus-specific features and limitations there is a Quarkus CDI Reference Guide. We also recommend you to read the CDI specification and the Weld documentation (Weld is a CDI Reference Implementation) to get acquainted with more complex topics.