OSGi principles and Best Practices: Chapter I Introduction to the OSGi Framework (5) SPRING-DM

Source: Internet
Author: User

OSGi principles and Best Practices: Chapter I Introduction to the OSGi Framework (5) SPRING-DMpublished by Valrayhas been browsed 8,409 times a total of 3 reviews have been 2 favorites 2013-08-16 21:29 Top (1) Step (0) OSGi principles and Best Practices 1.3 spring-dm1.3.1 Introduction

SPRING-DM refers to Spring Dynamic Modules. The main purpose of SPRING-DM is to easily combine the spring framework with the OSGI framework so that applications using spring can be easily and simply deployed in an OSGI environment, leveraging the services provided by the OSGi framework to make the application more modular. Specific information can be viewed in http://static. springframework.org/osgi/docs/1.2.0-rc1/reference/html/.

The 1.3.2 environment is built to download new SPRING-DM packages from the Spring website. Currently the new version is 1.2.0 RC1, in Http://www.springsource.com/download/community?project=Spring%20Dynamic%20Modules&version = 1.2.0.rc1 on this page. It offers a download of with-dependencies and no dependecies two packages. We recommend that you download this package from With-dependencies, which contains the other packages that SPRING-DM relies on. After downloading this SPRING-DM package, we extract the Dist and LIB directories in the compressed package to a directory on the hard disk, such as extracting into the SPRING-DM directory under the C packing directory. Then we will see the content shown in the C:\Spring-dm\dist directory in 1-52. See the content shown in 1-53 in C:\spring-dm\lib.

"Figure 1-52 Dist content in SPRING-DM"

The following SPRING-DM bundles are imported into Eclipse.

We first import the SPRING-DM package. Open Eclipse, select Import ..., in the Import dialog box, select Plug-ins and fragments (see Figure 1-54).

"Figure 1-53 the contents of Lib in Spring-dm"

"Figure 1-54 Import Plugin" Then make the settings shown in 1-55 in the Import Plugin-ins and Fragments dialog box.

Figure 1-55 Setting the directory to import plugins We want to set plug-in location, first set to C:\spring-dm\dist, import SPRIMG-DM package. After clicking Next, there is a Plugin interface that lets us choose to import (see Figure 1-56).

"Figure 1-56 Select plugins to import" We can import three bundles of core, extender, Io. When you're done, you'll see a 1-57 display in your Eclipse's workspace.

"Figure 1-57 Post-import display"

We select these three bundles directly in the run configurations, and we will find that three bundles are installed states, and if we start the bundle, we will get an error because we are not joining the bundles that the three bundles depend on. These bundles, too, are in the C:\spring-dm\lib directory. We use the same way as before to import the required bundles in lib. The bundles to be imported are com.springsource.org.aopalliance, Org. SPRINGFRAMEWORK.AOP, Org.springframework.beans, Org.springframe Work.context, Org.springframework.context.support, Org.springframework.core.

At this point, the Eclipse workspace should see a 1-58 display.

"Figure 1-58 Importing additional required plugins"

Select all the currently imported bundles in the run configurations, and then select Org.apache.commons.logging in Target Platform to run the bundle. As you can see, all bundles are in active state (see figure 1-59).

The building of the environment is completed here. Here's a SPRING-DM-based application.

"Figure 1-59 Post-boot Bundle status" 1.3.3 HelloWorld

Let's look at a simple example. In this example, you will show how to present a spring bean as a service and how to inject the service of OSGi into Spring beans. In this example, there are two bundles, the HelloWorld bundle and the Timeservice bundle, respectively. Example code see source SPRING-DM in the HelloWorld Engineering and Timeservice Engineering.

Neither of the two projects is bundleactivator. There is no need to do anything when the Bundle starts and stops. First look at the Timeservice project (see Figure 1-60).

"Figure 1-60 Timeservice Project"

As you can see, we have defined the implementation of the Timeservice interface and the Timeservice (Timeserviceimpl) in the Timeservice project. In this case, it is not a good idea to define and implement the interface definition should be placed in a separate Bundle, here simply to signal, choose a simple approach. The code for the Timeservice interface and the Timeserviceimpl class is simple and we no longer introduce it. The big difference in this project is that there is a spring directory under the Meta-inf directory. There are two files in this directory, namely Timeservice.xml and Timeservice-osgi.xml. Let's take a look at the contents of these two files separately.

Timeservice.xml

<?XML version="1.0"Encoding="UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans"  xmlns:xsi< Span class= "pun" >= "http://www.w3.org/2001/XMLSchema-instance"  xsi:schemalocation=>  <bean name= "Timeservice"  class=>  </bean> </BEANS>        

A spring bean is defined in this XML file. There is no difference between this and the usual spring configuration file.

Timeservie-osgi.xml

<?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=> <osgi:service  Id= "Osgitimeservice"  ref = "Timeservice"  interface = "Org.osgichina.demo.timeservice.TimeService" >  </osgi:service> </beans>  

Here, we see a new label "", the meaning of this tag is to define an OSGi service. The service implements an interface that is "Org.osgichina.demo.timeservice.TimeService", and the service references the object "Timeservice", This is the Spring Bean defined in the previous configuration file that we saw just now. With such a configuration, the spring bean can be published as an OSGi service. Isn't it simple?

Let's take a look at the HelloWorld project (see Figure 1-61).

"Figure 1-61 HelloWorld Project"

As you can see, there is only helloworldimpl such a Java file in the HelloWorld file, and a spring directory is hidden under Meta-inf, with Helloworld-osgi.xml and Helloworld.xml in the directory. File. Let's simply look at the HelloWorld code.

Public Class Helloworldimpl{ Private TimeserviceTimeservice; Public TimeserviceGettimeservice() { ReturnTimeservice; } Public voidSettimeservice(TimeserviceTimeservice) { This.Timeservice=Timeservice; } Public voidStart(){ System.Out.println ( "started at"  + Timeservice. Getcurrenttime } public  Stop () { system.< Span class= "KWD" >out. Println ( "stopped at"  + Timeservice. Getcurrenttime } }       /span>                

As you can see, HelloWorld is a simple bean that has the output of the log when you call start and stop.

Next look at the Helloworld.xml file.

<?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< Span class= "pun" >= "Org.osgichina.demo.helloworld.HelloWorldImpl"  init-method= "start"  = "stop"  >  <property name=< Span class= "ATV" > "Timeservice"  ref=/> </bean> span class= "tag" ></BEANS>              

As you can see, this configuration file is the same as the normal spring configuration file, defining a bean, as well as the Init and Destory methods. and injected an object to the Timeservice attribute. Next, let's take a look at helloworld-osgi.xml.

<?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= xsi:schemalocation=< Span class= "ATV" > "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:referenceid= "Osgitimeservice"  = "Org.osgichina.demo.timeservice.TimeService" / > </BEANS>         

Note that there is also a new label "Osgi:reference", which is a reference to the OSGi service. The interface name is defined here because it is used to represent the referenced service.

Publish a spring bean as a service and how to reference an OSGi service as a property of a bean. Here, there may be a question that if timeservice is not injected into the spring bean, but rather gets the service in the code, what should I do? Very simply, we just have to get this service through Bundlecontext. The name of the service is the name of the interface, in other words, for the timeservice in this example, we can get the service with the following code:

   servicereference SF = Context. (timeservice.. Getname timeservice ts =  (timeservicecontext.getservicesf      

Here, the example has been completed. Here is a brief introduction to the implementation behind this. In fact, the Extender of spring is the bundle that helped us with the release and injection of the service. This bundle will look for all the XML in the Spring directory under Meta-inf to complete the configuration work. Of course, we can also be in MANIFEST. MF file, configure the configuration used in the SPRING-DM. The spring catalog is a default convention. To complete this example, let's complete an example of a SPRING-DM used in a Web scenario.

1.3.4 Web version HelloWorld

The previous section has seen an example of SPRING-DM's HelloWorld. Here, take a look at the Web version of HelloWorld. The main thing here is to introduce the other two bundles in SPRING-DM, one is Org.springframework.osgi.web and the other is Org.springframework.osgi.web.extender. As you will recall, in the example in the previous section, we used the Bundle Org.springframework.osgi.extender. Here's Org.springframework.osgi.web.extender this bundle is a extender used in the Web environment.

In fact, there are many ways to construct a Web version of HelloWorld. One way is to complete a WEB application by embedding Httpserver in the previous HelloWorld example. This section does not use this scenario, we use SPRING-DM's webextender to deploy a WEB program.

There are two scenarios in Eclipse to create our project: One is to create a standard Web project, so there is an inconvenient place to debug the problem, the other way is to create a plug-in project, and this project is packaged with a WEB application structure of the jar package. Below, let's do this example.

1.3.4.1 Environment We built an environment when we finished the HelloWorld of SPRING-DM. Now, we should introduce several bundles on the basis of that environment, namely ORG.SPRINGFRAMEWORK.OSGI.CATALINA.OSGI and Org.springframework. Osgi.catalina.start.osgi, Org.springframework.osgi.servlet-api.osgi, Org.springframework.osgi.web, Org.springframework.osgi.web.extender. The following two bundles are imported from the Dist directory, and the first three are imported from the Lib directory. After importing these bundles, our Eclipse should look like 1-62.

"Figure 1-62 Display after adding a new plugin"

Next, we need to modify the Run configurations to set the start level of the Org.springframework.osgi.web.extender, which needs to be modified to be more than the other Bundl E large. The other is to choose the two bundles of javax.servlet and org.apache.commons.logging in Target Platform. As shown in 1-63.

Then click "Run" to see a display similar to 1-64 shown in Eclipse's Console.

"Figure 1-63 Bundles of the running configuration"

"Figure 1-64 Post-run display"

This time, you can also see that port 8080 is already in the LISTEN state. Our WEB environment is ready to be finished and we will develop it below.

1.3.4.2 Web Application Development

The WEB application development steps are as follows.

> First step, create plug-in project.

We created a plug-in project called HelloWorldWeb.

**> * * Step two, add the Web-inf directory and index.html file, and include the Web. xml file in the Web-inf directory. In this way, our project should look like 1-65.

"Figure 1-65 HelloWorldWeb display" **>** The third step, introduce the package (see Figure 1-66).

"The package introduced in Figure 1-66"

> Fourth Step, create Helloworldservlet. Code please see the source in the HELLOWORLD-SPRINGDM directory of the HelloWorldWeb project. Also, we are going to introduce the timeservice in the previous HelloWorld example.

> Fifth Step, edit index.html.

Here, we can do whatever you like in the index.html.

> Sixth Step, edit Web. Xml.

This web. XML is basically a configuration in a common website, and in this XML, we add Spring's ApplicationContext configuration:

<context-param> <param-name>contextClass</param-name> <param-value>org.springframework.osgi.web.context.support. OsgiBundleXmlWebApplicationContext</param-value> </context-param>

The complete content can refer to the source code in the HELLOWORLD-SPRINGDM directory under the HelloWorldWeb project of Web. Xml.

> Seventh Step, add Applicationcontext.xml. We add the Applicationcontext.xml file under the Web-inf directory, which is configured to introduce Timeservice, as follows:

<!--引入OSGi Service的定义--> <osgi:reference id="osgiTimeService" interface="org.osgichina.demo.timeservice.TimeService"/>

> Eighth Step, configure Run configurations.

We're going to add three bundles to the Target Platform in Run configurations, javax.servlet.jsp, Org.apache.jasper, and Org.apache.commons.el, respectively.

> nineth Step, start, run.

We will see a message similar to the following entered in the Console:

    信息: Successfully deployed bundle [HelloWorldWeb Plug-in (HelloWorldWeb)] at [/HelloWorldWeb] on server org.apache.catalina.core.StandardService/1.0

At this point, we can test our app on the browser.

Open the browser and enter http://localhost:8080/HelloWorldWeb/.

You will see the display shown in 1-67.

"Figure 1-67 Browser results display"

Then, enter Http://localhost:8080/HelloWorldWeb/hello.

You will see the content shown in 1-68.

"Figure 1-68 Browser results display"

Well, our Web app is working, and it's easy to debug our programs in Eclipse's integrated environment.

OSGi principles and Best Practices: Chapter I Introduction to the OSGi Framework (5) SPRING-DM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.