Appendix A. Extensions

This appendix describes extensions to the core functionality that are shipped with the 1.0 distribution, but are not guaranteed to have backwards compatibility across point releases. We anticipate these features migrating into the core specification in future releases.

A.1. Annotation-Based Injection

The org.springframework.osgi.extensions.annotation bundle that ships with Spring Dynamic Modules provides early access to annotation-based support for injecting references to OSGi services. JDK 1.5 or above is required to use this feature.

Bean class (setter) methods may be annotated with org.springframework.osgi.extensions.annotation.ServiceReference. By default the property type of the annotated property is used to look up a service with a matching service interface in the OSGi service registry and inject the result. For example, given the configuration:

<bean id="annotationDriven" class="MyAnnotationDrivenBeanClass"/>

and the class declaration:

public class MyAnnotationDrivenBeanClass {

  public void setMessageService(MessageService aService) { ... }


then a service lookup for services implementing the MessageService interface will be performed, and the best match (using the same algorithm as documented for the reference element) will be injected.

The ServiceReference annotation class has a number of attributes that can be used to control the service lookup (for example, to specify a filter string) that mirror the options provided by the reference element. See the javadoc for more information.

A.1.1. Enabling/Disabling Annotation Processing

By default, as Spring-OSGi is JDK 1.4 compatible, annotation injection is disabled. There are currently two ways for enabling it:

  • through Spring DM extender (see Section 6.2, “Extender Configuration Options”).

    This is an extender wide configuration which means all bundles started by the extender will have annotation injection applied to them.

  • by specifying a dedicated bean post processor

    By specifying the Spring DM annotation extension processor, one can enable per-bundle annotation injection. To do that, add the following to your bundle configuration:

    <!-- annotation processor -->
    <bean class="org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor"/>

As a reminder, for the annotations to work, the containing bundle needs to import org.springframework.osgi.extensions.annotation package, which is available in the spring-osgi-annotation bundle.