Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 6 of 7
The Spring configuration for the HelloWorld bundle is divided into two files: helloworld.xml and helloworld-osgi.xml. Let's start with helloworld-osgi.xml, shown in Listing 9.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<osgi:reference id="contactDAOService" interface="com.javaworld.sample.osgi.spring.contact.ContactDAO"/>
</beans>
The helloworld-osgi.xml file declares one reference element, which is used to retrieve the OSGi service and make it available as a Spring bean in
the HelloWorld bundle. The reference element has two attributes, id and interface, as previously mentioned. The value of the id attribute is used by Spring DM while adding an OSGi service as a Spring bean in the application context. In this case, we
have said that Spring DM should make the service available as a contactDAOService in the application context of the HelloWorld bundle.
The second attribute is interface. Spring DM will use the value of this attribute to find a service matching the given interface. In our sample code we have
said that we want a service that implements the com.javaworld.sample.osgi.spring.contact.ContactDAO interface.
Spring DM calls BundleContext.getServiceReference()to find a service that implements the com.javaworld.sample.osgi.spring.contact.ContactDAO interface. If more than one service in the OSGi framework matches that requirement, then the one with the highest ranking
is returned. You can also use a filter attribute to precisely define the service you want.
Next, we change the helloworld.xml file so that it will inject the contactDAOService object into our hello bean, as shown in Listing 10.
<?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="hello" class="com.javaworld.osgi.spring.HelloWorld"
init-method="start" destroy-method="stop" >
<property name="contactDAO" ref="contactDAOService"/>
</bean>
</beans>
Once the ContactDAOService is injected into the application context of your bundle, you can use it as you would any other Spring bean. In our sample
code we are injecting the service as a contactDAO property in a HelloWorld bean.
Having executed your bundles in the Eclipse IDE, you should get a "Hello Spring World!! Inside ContactDAOImpl.getContactList()" message in your console when you start up the HelloWorld bundle. Under the hood, once the Spring extender bundle is started it sees that there are two Spring-powered bundles. In response, it will first create an application context
for the ContactDAO bundle. At the same time, it finds the contactdao-osgi.xml file and exports ContactDAO as an OSGi service in the common registry. Next, it attempts to create an application context for the HelloWorld bundle. Seeing that it has one reference element, extender calls the BundleContext.getService("com.javaworld.sample.osgi.spring.contact.ContactDAO") method, in order to find the class service implementing the com.javaworld.sample.osgi.spring.contact.ContactDAO interface.
In our sample code (in Listing 5) ContactDAOImpl is the only service implementing this interface, so extender returns an object of ContactDAOImpl. Once that object is returned, Spring DM injects it into the HelloWorld bean as a contactDAO property.
Technology downloads
More
Archived Discussions (Read only)