Java Reference
In-Depth Information
Solution
You can use Spring Dynamic Modules configuration schema to export a service. The service will be
made available to other beans as well as other OSGi components.
Approach
The approach is similar to that of the other configurations. We will create a bundle and deploy it. Create
a Spring XML configuration ( src/main/resources/META-INF/spring/bundle-context.xml ) for our regular
Spring beans, just as we did in the previous recipe.
<?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:context=" http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:annotation-config/>
<bean id="greeterService"
class="com.apress.springenterpriserecipes.osgi.helloworld.
service.GreeterServiceImpl"/>
</beans>
Here, we declare a bean named greeterService that we will reference.
In a separate file ( src/main/resources/META-INF/spring/bundle-osgi-context.xml ), we will export
the service using the Spring Dynamic Modules configuration schema. Here, we'll use the osgi:service
element to export the bean as an OSGi service, classified by the interface we specify. Note that we could,
technically, have specified a concrete class for the value of interface , though it's not recommended. In
our example, we want our service to advertise that it supports multiple interfaces, so we'll specify both
of them.
<?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"
xmlns:util=" http://www.springframework.org/schema/util"
xmlns:context=" http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/beans/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
Search WWH ::




Custom Search