Dependency Injection

Use injection annotations to create a graph

Introduction

The goal is to combine imperative and factories instance creation to generate a SEP. A SEPConfig class is written by the user, but in contrast to imperative declaration only one node needs to be imperatively added, all other nodes can be added to the graph using injection.

Injecting

Use the @inject annotation to inject a node instance into another node. The injected instance will be automatically included in the execution graph. If a NodeFactory cannot be located for the target type of the injection then the default constructor will be used to instantiate the node. If a default constructor cannot be found then a constructor bypass strategy is employed. An injection annotation follows the form:

@Inject
public <Type> injectedInstance;

Properties

It is also possible to set properties of the injected instance, the properties can be static or variable based as described below. The properties are passed to the injected factory in a map. If no factory is registered for the instance then the properties are injected directly into a member field of the injected instance. All injected properties will be set in the generated SEP

For field injection primitive types and Strings are supported.

Static properties

This example shows setting a value of 150 in the field limit of an injected instance. There is no factory registered so the field will be set to the ca

   @Config(key = "limit", value = "150")

Variable properties

The value of a config element can be looked up from a variable in the current class. An example setting the field filter in the inject class from the variable filterName in this class.

Mix injection and imperative strategies freely, declare multiple nodes in SEPConfig and multiple injections in any node.

Example

The following example injects a FilteredDataHandler into a child class (InjectingDataProcessor). Static and variable properties are set using the @Config and @ConfigVariable annotations. Constructor bypass is used to invoke a default constructor during graph building.

Node classes

SEPConfig

The SEPConfig file adds a single node to the graph, InjectingDataProcessor. All config is passed from the

Running fluxtion command

For this example the command line to invoke Fluxtion ESC will be similar to the one shown below.

Generated SEP

Note the injected instance also has properties injected, see line 11 where the limit field is set to 150. The filter field of FilteredDataHandler controls the filter value of the generated event handler method, dataEvent(DataEvent event). On line 29 the value of of the injection config variable has been realized in the generated SEP, the original value is passed on line 5 of the SEPConfig above.

Generated png

Generated SEP demonstrating injected nodes

Last updated

Was this helpful?