Annotation Interface Autowired


@Target(FIELD) @Retention(RUNTIME) @Inherited public @interface Autowired
Annotation that informs a BeanContext that a dependent field needs its object reference injected, which is also known as autowired.

Examples

1) Simple autowiring

After an instance of MyClass1 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) Qualified (named objects) autowiring

In rare instances where multiple instances of the same bean type exist and a given autowired field needs a specific instance of the bean, named beans can be used. This involves adding beans to the context using a name and then using the qualifier option in the Autowired annotation to specified which bean to auotwire.
 
      public class MyClass1 {

          @Autowired(qualifier = "name1")
          private MyClass2 myClass2a;

          @Autowired(qualifier = "name2")
          private MyClass2 myClass2b;
      }
 
 
In your application startup code, you'd write something like this:
 
      public MyKosApp {

          public init() {
              // . . .
              // Create the two named instances:
              ctx.add("name1", new MyClass2(optionsA));
              ctx.add("name2", new MyClass2(optionsB));
              // . . .
          }
      }
 
 

3) AutowiredList and ListenerList

Annotating an AutowiredList (or any subclass, such as ListenerList) field with Autowired will cause BeanContext to add all instances of the generic list type to the list. This not only includes any beans of the type in the context, but also includes any beans from child and parent contexts as well as any beans connected to the context. This is used extensively in KOS to manage listeners as any bean that implements a listener interface will be added to the matching ListenerList regardless of where it resides in the parent/child graph (so long as it is a parent / descendant, it will not work around "corners" in a graph). When contexts are destroyed, their beans are automatically removed from the lists, providing hassle free listener lists with no user cleanup.
 
      public class MyClass1 {
          @Autowired
          private ListenerList<MyListenerInterface> listeners;
      }
 
 

4) Ignoring "ready"

BeanContext will automatically notify Ready/ReadyListener beans when all their dependencies are ready. Consider a service that needs to initialize itself once all of it's dependencies are satisfied (such as scanning files in a directory). This service isn't ready to service any requests until this initialization is complete. By utilizing Ready/ReadyListener interfaces, BeanContext will automtically notify the service when all of its dependencies are ready and wait until the service itself becomes ready before continuing with additional beans. However, this can lead to issues if two services depend on each other as neither can be the first to be initialized. In the case where one of the services doesn't need the reference during the initialization phase, it can use the ignoreReady option of the Autowired annotation to break this dependency.
 
      public class MyClass1 {
          @Autowired(ignoreReady = true)
          private MyClass2 myClass2;
      }
 
 

5) Optional autowiring

Setting the required flag false allows this autowire to fail without generating an error or throwing an exception. This can be useful in cases where some beans may only exist due to conditional configurations.
 
      public class MyClass1 {
          @Autowired(required = false)
          private MyClass2 myClass2;
      }
 
 
Since:
1
Version:
1
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    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 phase
      The phase to evaluate this annotation in.
      Returns:
      the name of the phase when this bean should be injected
      Since:
      1
      Default:
      ""
    • qualifier

      String qualifier
      The qualifier that matches a bean with the same name.
      Returns:
      the name of the bean associated with this object
      Since:
      1
      Default:
      ""
    • ignoreReady

      boolean ignoreReady
      Set 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
      Since:
      1
      Default:
      false
    • required

      boolean required
      Setting this flag false allows this autowire to fail without throwing an error.
      Returns:
      true if the autowire requires a non-null value
      Since:
      1
      Default:
      true