Annotation Interface Autowired
Annotation that informs the BeanContext that a dependent field needs its object
reference injected, which is also known as autowired.
Examples
1) Simple autowiring
After an instance ofMyClass1
is created, the myClass2
and
myClass3
objects are automatically inserted into their respective fields
by the kOS bean context.
public class MyClass1 {
@Autowired
private MyClass2 myClass2;
@Autowired
private MyClass3 myClass3;
}
2) Phased autowiring
Due to the asynchronous nature of the hardware associated with embedded devices, it's common to want to inject sets of beans in phases. In other words, set #1 is injected first, then set #2, then set #3, etc. kOS provides a way to do this using the "phase" parameter.
In this example, the myClass2
object is injected during "Phase-X",
while the myClass3
object is injected during "Phase-Y".
public class MyClass1 {
@Autowired(phase = "Phase-X")
private MyClass2 myClass2;
@Autowired(phase = "Phase-Y")
private MyClass3 myClass3;
}
In your application startup code, you'd write something like this:
public MyKosApp {
@Autowired
private BeanContext ctx;
public init() {
// . . .
// Inject all beans associated with the "Phase-X" phase:
ctx.update("Phase-X");
// (possibly perform other operations)
// Inject all beans associated with the "Phase-Y" phase:
ctx.update("Phase-Y");
// . . .
}
}
More practically speaking, you might want to autowire filesystem beans first,
followed by network beans, then database beans, then hardware beans, and finally
the user interface beans.
3) Qualified (named objects) autowiring
Sometimes, like when you have multiple instances of the same class, you need another way to identify each. For such occasions, use thequalifier
parameter, which allows you to differentiate between the two instances.
public class MyClass1 {
@Autowired(qualifier = "Pump-1")
private MyClass2 myClass2a;
@Autowired(qualifier = "Pump-2")
private MyClass2 myClass2b;
}
In your application startup code, you'd write something like this:
public MyKosApp {
public init() {
// . . .
// Create the two instances:
MyClass2 myClass2a = new MyClass2(optionsA);
MyClass2 myClass2b = new MyClass2(optionsB);
// Add them to the context, being sure to name them:
ctx.add("Pump-1", myClass2a);
ctx.add("Pump-2", myClass2b);
// . . .
}
}
4) Ignoring "ready"
Use theignoreReady
flag when you don't need to wait for the autowired bean
to become ready before marking the referencing bean ready. This can be used to
resolve circular dependencies. The referencing bean can use isReady() or
whenReady() to detect when the referenced bean becomes ready without
blocking startup.
public class MyClass1 {
@Autowired(ignoreReady = true)
private MyClass2 myClass2;
}
5) Optional autowiring
Setting therequired
flag false
allows this autowire to fail
without generating an error or throwing an exception.
public class MyClass1 {
@Autowired(required = false)
private MyClass2 myClass2;
}
- Since:
- 1.0
- Version:
- 2022-08-30
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionboolean
Set this flag true when you do not want to wait for the autowired bean to become ready before marking the referencing bean ready.The phase to evaluate this annotation in.The qualifier that matches a bean with the same name.boolean
Setting this flag false allows this autowire to fail without throwing an error.
-
Element Details
-
phase
String phaseThe phase to evaluate this annotation in.- Returns:
- the name of the phase when this bean should be injected
- Default:
- ""
-
qualifier
String qualifierThe qualifier that matches a bean with the same name.- Returns:
- the name of the bean associated with this object
- Default:
- ""
-
ignoreReady
boolean ignoreReadySet this flag true when you do not want to wait for the autowired bean to become ready before marking the referencing bean ready.- Returns:
true
if this autowire is not necessary for the containing bean to become ready
- Default:
- false
-
required
boolean requiredSetting this flag false allows this autowire to fail without throwing an error.- Returns:
true
if the autowire requires a non-null value
- Default:
- true
-