Java Reference

CtxEventListener

Introduction

By implementing the CtxEventListener interface, an object can be notified:

  • when all autowiring is complete, and/or

  • when a particular autowiring phase has completed

Overview

Our BeanContext allows for partial autowiring by using the optional "phase" property of the @Autowired annotation. This is particularly useful for initial system startup, where services need to be started in a specific sequence, and cannot necessarily be fully autowired until later steps in the process.

When using @Autowired(phase = "myPhase"), a call to ctx.update("myPhase") will cause those fields tagged with this phase to be autowired. When all beans in the context have been processed for this phase, any beans that implement this interface will be notified that all autowiring for the phase is complete, allowing those beans to do some work.

Definition

The two methods defined in the CtxEventListener interface are shown below. As you can see, both methods are optional: you can override either or both.

API: CtxEventListener interface
public interface CtxEventListener {

    default void onCtxAutowiringCompleted() {
    }

    default void onCtxPhaseCompleted(String phaseName) {
    }
}

Examples

Let’s see how this work in an example class:

Example using CtxEventListener
public class MyClass1 implements CtxEventListener {

    @Autowired
    private MyClass2 myClass2;

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

    @Override
    public void onCtxPhaseCompleted(String phaseName) {

        switch (phaseName) {
            case "Phase-2":
                // All beans autowired in phase "two" are ready:
                // which means that myClass3 is now instantiated.
                break;
            default:
                // This is a phase this class is not concerned with.
                break;
        }
    }

    @Override
    public void onCtxAutowiringCompleted() {
        // This bean is now fully wired:
        // both myClass2 and myClass3 have been injected.
    }
}

Examining the example, we find that the MyClass1 class implements the CtxEventListener interface and overrides both methods.

The KOS BeanContext calls onCtxPhaseCompleted() when each phase has finished its autowiring. This method looks at which phase has completed and decides if it has any interest or not. If so, it performs whatever work it needs to. If not, the method simply returns.

Likewise, the BeanContext calls onCtxAutowiringCompleted() when all beans have had all dependencies injected. These object then have the opportunity to perform additional work, knowing that all beans are wired.

Next Up

This page showed us how to know when a single bean, a set of beans, or all beans have been injected into all dependent objects. The next page deals with taking this concept a bit further: How can we determine if and when a dependent bean is "ready" (fully operational)? The Ready Processing components describe how to solve this issue.

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.