Java Reference
In-Depth Information
When we deploy the final bundle to Equinox when we're finished, the XML context files loaded in
the META-INF directory will be loaded. It is through the magic of OSGi extender models that this works.
OSGi enables deployed bundles to scan other deployed bundles and react to qualities of those bundles.
In particular, this is sort of like what Spring does when it scans a package for classes with annotations.
Here, Spring Dynamic Modules scans our deployed bundles and loads an ApplicationContext (actually,
the specific type of the Application Context is OsgiBundleXmlApplicationContext ) into memory based
on an event, or a “ trigger .” There are two ways to trigger this behavior. The first is to explicitly specify in
the META-INF/MANIFEST.MF file the attribute Spring-Context , which allows you to override the default
location it consults. Otherwise, by default, Spring Dynamic Modules will look for the XML file in the
META-INF/spring directory of a bundle. Typically, you'll split your OSGi-specific Spring configuration
and your plain-vanilla Spring configuration into two different files, of the form: modulename -context.xml
and modulename -osgi-context.xml .
The Java code is like that of any other standard Spring bean. In fact, for all intents and purposes,
this code has no knowledge of OSGi. It does have knowledge of Spring itself, though there's no reason it
needs to. The @Autowired field-injection and InitializingBean interface are just for convenience and
brevity's sake.
package com.apress.springenterpriserecipes.osgi.springdmhelloworld.impl;
import com.apress.springenterpriserecipes.osgi.helloworld.service.GreeterService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.Locale;
public class SpringDMGreeterClient implements InitializingBean {
@Autowired
private GreeterService greeterService;
public void afterPropertiesSet() throws Exception {
for (String name : Arrays.asList("Mario", "Fumiko", "Makani"))
System.out.println(greeterService.greet(Locale.FRENCH.toString(), name));
}
}
Let's explore the Spring XML files. The first one, src/main/resources/META-INF/spring/bundle-
context.xml , a standard Spring XML application context, should not be very surprising. It contains just
one bean definition.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="springDMGreeterClient"
class="com.apress.springenterpriserecipes.osgi.
springdmhelloworld.impl.SpringDMGreeterClient" />
</beans>
Search WWH ::




Custom Search