Annotation Interface Optional


@Qualifier @Documented @Target({METHOD,FIELD,PARAMETER}) @Retention(RUNTIME) public @interface Optional
This annotation can be applied to methods, fields, and parameters to mark them as optional for the dependency injection. Typically, if the injector is unable to find a value to inject, then injection will fail. However if a value cannot be found and if this annotation is specified, then:
  • for parameters: a null value will be injected;
  • for methods: the method calls will be skipped;
  • for fields: the values will not be injected.

Note that null is as an acceptable value and is not the same as a value not being found. For example, the IEclipseContext-based supplier distinguishes between a value being set to null (i.e., context.set(SOMEKEY, null)) and the value not found (i.e., context.remove(SOMEKEY)).

Example usage:
   public class Car {
     @Inject @Optional void setOverdrive(OverdriveMode mode);
     ...
   }
 
Since:
1.3