Red Hat Training

A Red Hat training course is available for Red Hat Fuse

1.4. Dependency Injection Frameworks

Overview

Red Hat JBoss Fuse offers a choice between the following built-in dependency injection frameworks:

Blueprint or Spring?

When trying to decide between the Blueprint and Spring dependency injection frameworks, bear in mind that Blueprint offers one major advantage over Spring: when new dependencies are introduced in Blueprint through XML schema namespaces, Blueprint has the capability to resolve these dependencies automatically at run time. In contrast, when packaging your project as an OSGi bundle, Spring requires you to add new dependencies explicitly to the maven-bundle-plugin configuration.

Bean registries

A fundamental capability of the dependency injection frameworks is the ability to create Java bean instances. Every Java bean created in a dependency injection framework is added to a bean registry by default. The bean registry is a map that enables you to look up a bean's object reference using the bean ID. This makes it possible to reference bean instances within the framework's XML configuration file and to reference bean instances from your Java code.
For example, when defining Apache Camel routes, you can use the bean() and beanRef() DSL commands to access the bean registry of the underlying dependency injection framework (or frameworks).

Spring XML

Spring is fundamentally a dependency injection framework, but it also includes a suite of services and APIs that enable it to act as a fully-fledged container. A Spring XML configuration file can be used in the following ways:
  • An injection framework—Spring is a classic injection framework, enabling you to instantiate Java objects using the bean element and to wire beans together, either explicitly or automatically. For details, see The IoC Container from the Spring Reference Manual.
  • A generic XML configuration file—Spring has an extensibility mechanism that makes it possible to use third-party XML configuration schemas in a Spring XML file. Spring uses the schema namespace as a hook for finding an extension: it searches the classpath for a JAR file that implements that particular namespace extension. In this way, it is possible to embed the following XML configurations inside a Spring XML file:
    • Apache Camel configuration—usually introduced by the camelContext element in the schema namespace, http://camel.apache.org/schema/spring.
    • Apache CXF configuration—uses several different schema namespaces, depending on whether you are configuring the Bus, http://cxf.apache.org/core, a JAX-WS binding, http://cxf.apache.org/jaxws, a JAX-RS binding, http://cxf.apache.org/jaxrs, or a Simple binding, http://cxf.apache.org/simple.
    • Apache ActiveMQ configuration—usually introduced by the broker element in the schema namespace, http://activemq.apache.org/schema/core.
    Note
    When packaging your project as an OSGi bundle, the Spring XML extensibility mechanism can introduce additional dependencies. Because the Maven bundle plug-in does not have the ability to scan the Spring XML file and automatically discover the dependencies introduced by schema namespaces, it is generally necessary to add the additional dependencies explicitly to the maven-bundle-plugin configuration (by specifying the required Java packages).
  • An OSGi toolkit—Spring also has features (provided by Spring Dynamic Modules) to simplify integrating your application with the OSGi container. In particular, Spring DM provides XML elements that make it easy to export and consume OSGi services. For details, see The Service Registry from the Spring DM Reference Manual.
  • A provider of container services—Spring also supports typical container services, such as security, persistence, and transactions. Before using such services, however, you should compare what is available from the JBoss Fuse container itself. In some cases, the JBoss Fuse container already layers a service on top of Spring (as with the transaction service, for example). In other cases, the JBoss Fuse container might provide an alternative implementation of the same service.

Spring XML file location

In your Maven project, Spring XML files must be placed in the following location:
InstallDir/src/main/resources/META-INF/spring/*.xml

Spring XML sample

The following example shows the bare outline of a Spring XML file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       >

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- Define Camel routes here -->
    ...
  </camelContext>

</beans>
You can use a Spring XML file like this to configure Apache ActiveMQ, Apache CXF, and Apache Camel applications. For example, the preceding example includes a camelContext element, which could be used to define Apache Camel routes. For a more detailed example of Spring XML, see the section called “Blueprint XML configuration”.

Blueprint XML

Blueprint is a dependency injection framework defined in the OSGi specification. Historically, Blueprint was originally sponsored by Spring and was based loosely on Spring DM. Consequently, the functionality offered by Blueprint is quite similar to Spring XML, but Blueprint is a more lightweight framework and it has been specially tailored for the OSGi container.
  • An injection framework—Blueprint is a classic injection framework, enabling you to instantiate Java objects using the bean element and to wire beans together, either explicitly or automatically. For details, see Deploying into the Container.
  • A generic XML configuration file—Blueprint has an extensibility mechanism that makes it possible to use third-party XML configuration schemas in a Blueprint XML file. Blueprint uses the schema namespace as a hook for finding an extension: it searches the classpath for a JAR file that implements that particular namespace extension. In this way, it is possible to embed the following XML configurations inside a Blueprint XML file:
    • Apache Camel configuration—usually introduced by the camelContext element in the schema namespace, http://camel.apache.org/schema/blueprint.
    • Apache CXF configuration—uses several different schema namespaces, depending on whether you are configuring the Bus, http://cxf.apache.org/blueprint/core, a JAX-WS binding, http://cxf.apache.org/blueprint/jaxws, a JAX-RS binding, http://cxf.apache.org/blueprint/jaxrs, or a Simple binding, http://cxf.apache.org/blueprint/simple.
    • Apache ActiveMQ configuration—usually introduced by the broker element in the schema namespace, http://activemq.apache.org/schema/core.
    Note
    When packaging your project as an OSGi bundle, the Blueprint XML extensibility mechanism can introduce additional dependencies, through the schema namespaces. Blueprint automatically resolves the dependencies implied by the schema namespaces at run time.
  • An OSGi toolkit—Blueprint also has features to simplify integrating your application with the OSGi container. In particular, Blueprint provides XML elements that make it easy to export and consume OSGi services. For details, see Deploying into the Container.

Blueprint XML file location

In your Maven project, Blueprint XML files must be placed in the following location:
InstallDir/src/main/resources/OSGI-INF/blueprint/*.xml

Blueprint XML sample

The following example shows the bare outline of a Blueprint XML file:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    >

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <!-- Define Camel routes here -->
    ...
  </camelContext>

</blueprint>
You can use a Blueprint XML file like this to configure Apache ActiveMQ, Apache CXF, and Apache Camel applications. For example, the preceding example includes a camelContext element, which could be used to define Apache Camel routes. For a more detailed example of Blueprint XML, see Example 2.2, “Configuring the Port Number in Blueprint XML”.
Note
The schema namespace used for Apache Camel in Blueprint, http://camel.apache.org/schema/blueprint, is different from the namespace used for Apache Camel in Spring XML. The two schemas are almost identical, however.