Introduction: as a new de facto industry standard, OSGi has received extensive attention, its basic concept of service-oriented (interface) and the ability of dynamic module deployment, is the goal that enterprise application has been pursuing for a long time. Spring is a well-known lightweight Java EE Development Framework characterized by interface-oriented programming and non-intrusive dependency injection. Combining OSGi and Spring can take advantage of both of them to better meet the needs of enterprise-class application development. The spring development organization released the first version of the integration of OSGi and spring in 2008: SPRING-DM. Through a simple example, this article describes how to develop OSGi and Spring based architectures using SPRING-DM
A simple Web application
We write a simple Web application compute.html: Calculates the sum or product of two numbers. As shown in the following:
Figure 1. A simple example
A simple example. bmp
To demonstrate the dynamic deployment capabilities of OSGi bundles, we wrote two service bundles, one of which computes two numbers of the and (called the Add Bundle), and the other calculates the product of two numbers (called the multiply bundle). When we click on the "Compute" button, if the add bundle is deployed at this point, the page will return two digits, otherwise if the multiply bundle is deployed, the page will return a product of two numbers.
Development environment Preparation
- Download Eclipse 3.4
- Get all the OSGi, Equinox, and Spring plugins as shown in:
Figure 2. List of related plugins
- Open Eclipse, set target platform for the above plugin collection
Basic module Design
The application consists of two levels: the service layer and the Web tier. The Web tier is based on the SPRING-MVC implementation and contains bundles that handle Web access (only one in this case). The service layer contains bundles that handle digital computations, in this case a compute interface bundle that declares the service interface and two bundle:add bundles and multiply bundles that implement the service interface. The basic module structure is as follows:
Figure 3. Basic framework
Module Program Implementation
Step 1: Implement Service Layer
After the implementation of the three OSGi bundles for the service layer is complete, as shown:
Figure 4. Service Layer
Service layer. bmp
Where Com.zxn.example.service.compute is the bundle that declares the service interface. Com.zxn.example.service.compute.add and com.zxn.example.service.compute.multiply are the two bundles that implement the service interface.
- Com.zxn.example.service.compute
Declares an Compute interface that contains an interface method Computenums (), as shown in:
Figure 5. Service Layer Interface Bundle
- Com.zxn.example.service.compute.add
The basic program structure for bundle Com.zxn.example.service.compute.add is as follows:
Figure 6. Interface Implementation Bundle:add
In the Add bundle, add a class that implements the Compute interface, as shown in:
Figure 7. Interface implementation code: ADD class
Notice that we built a spring directory under Meta-inf and added a computeadd-context.xml file. When the system starts, Spring uses the XML file to create a bean instance and outputs the bean as an OSGi service, as shown in:
Figure 8. Spring declaration file: computeadd-context.xml
In the XML file,Osgi:service is the token for the SPRING-DM output OSGi service, where the interface attribute indicates the service interface implemented by that service.
- Com.zxn.example.service.compute.multiply
Follow the same method as the add bundle to implement the multiply bundle, as shown in:
Figure 9. Interface implementation code: Multiply class
Interface implementation code-multiply class. bmp
Similarly, add an computemultiply-context.xml output OSGi service, as shown in:
Figure 10. Spring declaration file: computemultiply-context.xml
Step 2: Implement Web Layer
The WEB layer contains only one bundle:com.zxn.example.web, built with Spring-mvc and OSGi, with the basic program structure as shown:
Figure 11. WEB Layer Program Structure
- Computecontroler.java
The Java class implements a org.springframework.web.servlet.mvc.Controller
,
servlet that is the core of this Web application and is responsible for accepting and processing Web requests. This class invokes the Computeserviceref method to implement the business logic. The key method for this class is HandleRequest (...), as shown in:
Figure 12. Core Servlet class
- Computeserviceref.java
The JAVA class is responsible for referencing the deployed service bundle to complete the final calculation, where Computeservice is injected by Spring based on the service actually deployed in OSGi. In this case, the actual deployed service might be the add bundle or the multiply bundle.
It is important to note that the dynamic characteristics of SPRING-DM are shown here. The dynamic deployment capabilities of OSGi make Spring dynamic service injection possible.
Figure 13. Service consumption category
- Httpcontextresgistry.java
The JAVA class is responsible for configuring and registering the HTTP service in an OSGi environment, whose key method is the Init () method that is called when the bean is initialized.
Figure 14. Registering an HTTP service in an OSGi environment
In the Init method, line sixth of Gethttpservice (...) invokes the servicetracker of OSGi to obtain a reference to the HTTP service registered in the OSGi environment, as shown in:
Figure 15. Using Servicetracker to get the HTTP service
- Computeweb-context.xml
The XML file is primarily used to configure the Httpcontextresgistry bean class and to import references to the Compute service interface. The tag osgi:reference is used to declare the service interface to be imported, and its interface attribute indicates the definition of the interface, in this case the Com.zxn.example.service.compute.Compute interface.
Figure 16. Spring declaration file: Import Service Interface
- Computeweb-dispatcher.xml
The XML file is used to configure the Computecontroler Bean class.
Figure 17. Spring declaration file: Configuring the Core servlet class
Back to top of page
Run the program
In the past, the development of the Java EE application usually requires the integration of the application server runtime into the development environment in order to debug the program. OSGi-based applications can be completely detached from the application server, making program development and debugging easy, and debugging and running directly in Eclipse. We will run the program in Eclipse as shown in:
Figure 18. Running an OSGi program
As you can see, we also chose to deploy the add bundle and the multiply bundle, using the OSGi console to look at the following:
Figure 19. View the deployed OSGi bundle
When implementing bundles with multiple service interfaces are deployed at the same time in an OSGi environment, OSGi chooses a default bundle to provide the service. In this example, Spring will inject the add bundle by default. We access the Compute.html page via the Web:
Figure 20. Visit page
After you click the Compute button, the results page looks like this:
Figure 21. Access results
As you can see, the add bundle provides the compute service. Below we stop the add bundle service by ordering :
Figure 22. Stop Add bundle
Figure 23. Add bundle status changed to Resolved
Revisit the compute.html page, resulting in the product of two numbers. As you can see, it's multiply Bundl.
Provides a compute service. As shown in the following:
Figure 24. Visit the page again
Summary
As the two standard and framework of current vitality, OSGi and Spring have been initially fused together. The combination of both provides tremendous flexibility and dynamic deployment capabilities for developing enterprise-class WEB applications. This article describes how to develop a WEB application based on OSGi and Spring, using a simple example, and illustrates the technical key points involved in the development process.
OSGi and spring combine to develop Web engineering