Java Reference
In-Depth Information
12-6. Customizing Spring Dynamic Modules
Problem
Spring Dynamic Modules is a powerful tool, and it provides reasonable defaults (and results) for very
little investment. The main work of Spring Dynamic Modules is done through extenders, which
sometimes require customization.
Solution
Spring Dynamic Modules provides strong support for fragments, part of the OSGi specification, to
override key infrastructure beans Spring uses in service of the extenders.
Approach
Extenders provide a way to let a bundle control the loading process of another bundle. This magic is
most clearly exemplified in Spring Dynamic Modules' ability to auto-load Spring XML application
contexts detected in the META-INF/spring/ folder, or those configured by the Spring-Context MANIFEST.MF
attribute. Spring provides two extenders: one for web-based bundles ( spring-osgi-web-extender ,
whose trigger is being deployed in a bundle ending in .WAR ) and one for normal bundles ( spring-osgi-
extender ).
Customizing these extenders is key to customizing Spring Dynamic Modules' behavior at runtime.
Fragments permit the injection of resources, classes, and functionality into a bundle on the same
ClassLoader as the host bundle. Fragments do not, however, let you remove functionality or override
MANIFEST.MF values that are already established. Additionally, a fragment cannot contain its own
Activator . The OSGi runtime knows a bundle is a fragment if it encounters a Fragment-Host header in
the MANIFEST.MF file. The Fragment-Host attribute in turn is the Symbolic Name of another bundle. In this
case, because we are interested in configuring the two bundles with the extenders, we will reference one
of org.springframework.bundle.osgi.extender or org.springframework.bundle.osgi.web.extender .
Spring will check bundles that are configured this way and look for Spring XML application contexts
inside the META-INF/spring/extender folder. These Spring XML application contexts are loaded and any
beans with the same names as well known beans used by Spring will be given priority and used instead.
Let's go through some examples of where this might be useful.
You Want Spring To Process OSGi Annotations on Beans
Spring provides the ability to inject OSGi services using annotations. This functionality is available as an
extension, and requires a little configuration to enable it. The annotation serves the same function as the
reference element discussed earlier.
@ServiceReference
public void setGreeterService(GreeterService greeterService) {
// …
}
 
Search WWH ::




Custom Search