Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring-DM to develop Web Application based on OSGi and Spring Architecture

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Spring-DM to develop Web applications based on OSGi and Spring architecture. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

A simple Web application

Let's write a simple Web application compute.html: calculate the sum or product of two numbers. As shown in the following figure:

Figure 1. A simple example

To demonstrate the dynamic deployment capabilities of OSGi bundle, we write two service bundle, one of which calculates the sum of two numbers (called add bundle) and the other calculates the product of two numbers (called multiply bundle). When we click the "Compute" button, if add bundle is deployed at this time, the page will return the sum of two numbers, otherwise if multiply bundle is deployed at this time, the page will return the product of two numbers. Development environment preparation

1. Download Eclipse 3.4

two。 Get all the plug-ins for OSGi, Equinox, and Spring, as shown below:

1. Open Eclipse and set target platform to the above plug-in collection

Basic module design

The application mainly consists of two layers: service layer and Web layer. The Web layer is based on the Spring-MVC implementation and contains bundle that handles Web access (in this case there is only one). The service layer contains a bundle that handles digital computing. In this example, it contains a compute interface bundle that declares the service interface and two bundle that implement the service interface: add bundle and multiply bundle. The basic module structure is shown in the following figure:

Figure 3. Basic framework

Module program realization

Step 1: implement Service Layer

After the three OSGi bundle implementations of the service layer are completed, the following figure shows:

Figure 4. Service layer

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 two bundle that implement the service interface.

1.com.zxn.example.service.compute

Declare a Compute interface that contains an interface method computeNums (), as shown in the following figure:

Figure 5. Service layer interface bundle

1.com.zxn.example.service.compute.add

The basic program structure of bundle com.zxn.example.service.compute.add is shown in the following figure:

Figure 6. Bundle: add is implemented with API.

In the add bundle, add an Add class to implement the Compute interface, as shown in the following figure:

Figure 7. API implementation code: Add class

Notice that we created a spring directory under META-INF and added a computeAdd-context.xml file. When the system starts, Spring creates an instance of bean with the xml file and outputs the bean as an OSGi service, as shown in the following figure:

Figure 8. Spring declaration file: computeAdd-context.xml

In the xml file, osgi: service is the tag of the Spring-DM output OSGi service, where the interface attribute indicates the service interface that the service implements.

1.com.zxn.example.service.compute.multiply

Implement multiply bundle in the same way as add bundle, as shown in the following figure:

Figure 9. API implementation code: Multiply class

Similarly, add a computeMultiply-context.xml output OSGi service, as shown in the following figure:

Figure 10. Spring declaration file: computeMultiply-context.xml

Step 2: implementing Web Layer

The Web layer contains only one bundle:com.zxn.example.web and is built with Spring-MVC and OSGi. The basic program structure is shown in the following figure.

Figure 11. Web Layer program structure

ComputeControler.java

This JAVA class implements org.springframework.web.servlet.mvc.Controller, which is the core servlet of this web application, and is responsible for receiving and processing web requests. This class calls ComputeServiceRef's methods to implement the business logic. The key method of this class is handleRequest (…) As shown in the following figure:

Figure 12. Core servlet class

ComputeServiceRef.java

This JAVA class is responsible for referencing the deployed service bundle to complete the final calculation, where the computeService is injected by the Spring based on the actual deployed service in the OSGi. In this case, the actual deployed service may be add bundle or multiply bundle.

It is important to note that the dynamic nature of Spring-DM is reflected here. The dynamic deployment capability of OSGi makes the dynamic service injection of Spring possible.

Figure 13. Service consumption category

HTTPContextResgistry.java

This JAVA class is responsible for configuring and registering the HTTP service in the OSGi environment, and its key method is the init () method that is called when the bean is initialized.

Figure 14. Register the HTTP service in the OSGi environment

In the init method, the getHTTPService (…) in the sixth line Call OSGi's ServiceTracker to get a reference to the HTTP service registered in the OSGi environment, as shown in the following figure:

Figure 15. Use ServiceTracker to get HTTP service

ComputeWeb-context.xml

This xml file is mainly 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 that interface, in this case, the com.zxn.example.service.compute.Compute interface.

Figure 16. Spring declaration file: import service interface

ComputeWeb-Dispatcher.xml

This xml file is used to configure the ComputeControler bean class.

Figure 17. Spring declaration file: configure the core servlet class

Run the program

In the past, it is very troublesome to integrate the runtime of the application server into the development environment to debug the J2EE application. The application based on OSGi can be run without the application server, which makes it very easy to develop and debug the program, just debug it in Eclipse. We run the program in Eclipse, as shown in the following figure:

Figure 18. Run the OSGi program

As you can see from the figure above, we chose to deploy both add bundle and multiply bundle. Using OSGi console, we can see as follows:

Figure 19. View the deployed OSGi bundle

When an implementation bundle with multiple service interfaces is deployed at the same time in an OSGi environment, OSGi selects a default bundle to provide services. In this case, Spring injects add bundle by default. We visit the compute.html page through web:

Figure 20. Visit the page

After clicking the Compute button, the result page is as follows:

Figure 21. Access result

As you can see, it is add bundle that provides computing services. Let's stop the service of add bundle with the command < stop 76 >:

Figure 22. Stop add bundle

Figure 23. Add bundle status changes to RESOLVED

Revisit the compute.html page and the result is the product of two numbers. As you can see, it's multiply bundl.

Computing services are provided. As shown in the following figure:

Figure 24. Visit the page again

Thank you for reading! This is the end of this article on "how to use Spring-DM to develop Web applications based on OSGi and Spring architecture". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report