Java Reference

Autowiring

Overview

This page describes the @Autowired annotation, which denotes which object fields need injecting into dependent beans. It takes two optional parameters:

  • phase: the name of the "phase" that this autowired field is evaluated in

  • qualifier: the name of the desired bean to be instantiated

The "Simple autowiring" section describes using @Autowired without any parameters.

The "Phased autowiring" section describes using @Autowired with the phase parameter.

The "BeanContext: add(name, bean) method" section describes using @Autowired with the qualifier parameter.

The phase parameter is discussed in more detail on the Ready Processing page.

Bean Wiring

The process of "wiring" beans together means that beans that depend on other beans have those references automatically injected. All beans added to the BeanContext can be automatically wired with their dependent objects.

API Components

This article discusses the single @Autowired annotation.

Autowired

The following defines the @Autowired annotation. It has two optional parameters, "phase" and "qualifier"".

API: @Autowired annotation
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Autowired {

    // The phase to evaluate this annotation in:
    String phase() default "";

    // The bean with the specified qualifier:
    String qualifier() default "";
}

Example Code

Simple autowiring

Many Java classes depend on other objects. This is happens when one class refers to another class, typically through a field. For example, the following code snippet shows that MyClass1 references (depends on) MyClass2 (line 7) and MyClass3 (line 10).

MyClass1 class
public class MyClass1 {

    private int id;
    private String name;

    @Autowired
    private MyClass2 myClass2;

    @Autowired
    private MyClass3 myClass3;

    // . . .
}

How are the myClass2 and myClass3 fields instantiated?

Without dependency injection, the MyClass1 itself would create these objects, typically in a constructor:

MyClass1 don’t do this
public MyClass1() {
    myClass2 = new MyClass2();  // DON'T DO THIS!
}

However, as we’ve already discussed, the best way do instantiate these dependent objects is using dependency injection. To tell the BeanContext that it should inject MyClass2 and MyClass3 objects into MyClass1, simply add the @Autowired annotation above each field definition (lines 6 and 9).

After MyClass1 is created, the MyClass2 and MyClass3 references are automatically inserted into the MyClass1 respective fields because they are both annotated with @Autowired. The id and name fields are not automatically inserted because they are not annotated.

Phased autowiring

Due to the asynchronous nature of the hardware associated with a drink dispensing machine, it’s common to want to inject sets of beans in phases. In other words, set #1 is injected first, then set #2, etc. KOS provides a way to do that using the @Autowired "phase" parameter.

Autowiring "Phase"

A phase allows you to control the order of the autowiring process. It allows you to code things like:

"I want beans related to the file system to autowire first, followed by the network beans, then the database beans, then the hardware beans, and then finally the UI beans."

This gives you the ability, for example, to keep UI beans from trying to access any databases before they’re ready.

Going back to Figure 2, in the MyClass1 code, let’s assume we want to guarantee that the MyClass2 field is injected before MyClass3. There are two steps to make this happen.

First, add the phase parameters to the MyClass1 code:

MyClass1 class
public class MyClass1 {

    private int id;
    private String name;

    @Autowired(phase = "first-phase")
    private MyClass2 myClass2;

    @Autowired(phase = "second-phase")
    private MyClass3 myClass3;

    // . . .
}

Second, modify your application code (Figure 1) to inject objects in the desired order:

MyApp class
/**
 * This is my one and only application class, where everything starts.
 */
public class MyApp extends SystemApplication<BaseAppConfig> {

    // . . . Same as code in Figure 2 . . .

    // 4) Inject all @Autowired fields in the desired order:
    ctx.update("first-phase");
    ctx.update("second-phase");
    ctx.update("third-phase");
    ctx.update(null);

    // . . .
}

Line 9 injects all fields annotated with @Autowired(phase = "first-phase"), which is MyClass2 in our example.

Line 10 injects all fields annotated with @Autowired(phase = "second-phase"), which is MyClass3 in our example.

Line 11 injects all fields annotated with @Autowired(phase = "third-phase"), which is nothing in our example.

Line 12 injects all other fields in our application that are not part of "first-phase","second-phase", or "third-phase".

Summary

Now that we have a basic understanding of beans (Java objects), and how they are wired together (beans get automatically injected into dependent objects), we turn our attention to the focus of this section: the BeanContext.

Previous
Next
On this page
Java Development
Seamlessly transition from Legacy+ systems to Freestyle microdosing and advanced distributed dispense systems.
UI Development
Using KOS SDKs, integrating Consumer and Non-consumer facing UIs becomes seamless, giving you less hassle and more time to create.
Video Library
Meet some of our development team, as they lead you through the tools, features, and tips and tricks of various KOS tools.
Resources
Familiarize yourself with KOS terminology, our reference materials, and explore additional resources that complement your KOS journey.
Copyright © 2024 TCCC. All rights reserved.