Introduction
Since 3.1.0 release, Apache CXF supports CDI-based deployment inside OSGi container which uses Pax CDI (Contexts and Dependency Injection for OSGi) implementation (https://github.com/ops4j/org.ops4j.pax.cdi). Apache CXF provides the cxf-jaxrs-cdi feature (for easy deployment into OSGi containers such as Apache Karaf) as well as cxf-integration-cdi module includes necessary OSGi bundle metadata to be used by Pax CDI.
Installation
For the installation steps, Apache Karaf 3.0.3 OSGi container is going to be used as it is the most widely used OSGi container. All commands shown below are intended to be executed from Apache Karaf shell. The first step is to install http and Pax CDI features, which is CDI-version dependent.
For CDI 1.1, please use pax-cdi-1.1-web-weld feature:
feature:install http pax-cdi-1.1-web-weld
For CDI 1.2, please use pax-cdi-1.2-web-weld feature:
feature:install http pax-cdi-1.2-web-weld
Next, Apache CXF 3.1.0+ features should be installed:
feature:repo-add cxf 3.1.0 feature:install cxf/3.1.0 cxf-jaxrs-cdi/3.1.0
Bundle Metadata
In order for the bundle to be recognized as web CDI one and use Apache CXF, it should provide special bundle manifest instructions (f.e. using maven-bundle-plugin plugin).
</instructions> ... <Import-Package> javax.servlet;version="[2.6,4)", org.apache.cxf.jaxrs;version="[3.1,4)", org.apache.cxf.cdi;version="[3.1,4)", * </Import-Package> <Require-Capability> org.ops4j.pax.cdi.extension; filter:="(&(extension=cxf-integration-cdi))", osgi.extender; filter:="(osgi.extender=pax.cdi)" </Require-Capability> <Web-ContextPath>...</Web-ContextPath> <_wab>src/main/webapp</_wab> </instructions>