Apache
Home » Documentation » Apache Felix Subproject Documentation » Apache Felix Dependency Manager

Dependency Manager - Service Dependency

A service dependency allows you to depend on a service, either by type or by using an additional filter condition. You can even depend on an existing service directly by providing a reference to it.

@ServiceDependency

Annotates a method or a field for injecting a Service Dependency on it. When applied on a class field, optional unavailable dependencies are injected with a NullObject.

Annotation attributes:

Usage Example:

Here, the MyComponent component is injected with a dependency over a "MyDependency" service.

@Component
class MyComponent {
     @ServiceDependency(timeout=15000)
     MyDependency dependency;
     // ...
}

Usage example of a Service whose dependency filter is configured from ConfigAdmin, using a "named" dependency (please check Here for more informations about "named" dependencies):

/**
  * A Service whose service dependency "otherService" filter is configured from ConfigAdmin
  */
@Service
class X {
    private Dictionary m_config;

     /**
       * Initialize our service from config ... and store the config for later usage (from our init method)
       */
     @ConfigurationDependency(pid="MyPid")
     void configure(Dictionary conf) {
           m_config = config;
     }

     /**
      * All unnamed dependencies are injected: we can now configure other named
      * dependencies, using the already injected configuration.
      * The returned Map will be used to configure our "otherService" Dependency.
      */
     @Init
     Map init() {
         return new HashMap() ;
     }

     /**
      * This named dependency filter/required flag will be configured by our init method (see above).
      */
     @ServiceDependency(name="otherService")
         void bindOtherService(OtherService other) {
     }

     /**
      * All dependencies are injected and our service is now ready to be published.
      * Notice that you can also use the publisher service attribute if you need
      * to take control on service exposition.
      */
     @Start
     void start() {
     }
 }
Rev. 1665812 by marrs on Wed, 11 Mar 2015 09:02:04 +0000
Apache Felix, Felix, Apache, the Apache feather logo, and the Apache Felix project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.