Build a service-oriented contact management application using osgi

Source: Internet
Author: User
Tags list of attributes

[51cto] This article is "Hello, osgi

The eighth part of the series. In the previous article
Used
Spring DM creates hello
The following step is to import and export the service suite. To put it simply, build a service-oriented contact management application.

51cto editing recommendations:
Osgi introduction and Practice

Import and Export Service suite

The contact management application contains two suites. The first suite is contactdao, which is used to talk to the database and
The contactdao object is exported to the osgi service. The second package is the previously developed helloworld.
Applications
To import the contactdao object (that is, the exported osgi service ).

Next let's first create contactdao
Kit starts. For ease of use, we do not add real database interaction logic to the suite; instead, each method only writes its method name to the eclipse console.

First, create the com. javaworld. sample. osgi. Spring. Contact. Contact. Java class
Ontactdao transmits data to the helloworld suite. The program is shown in code list 3. (Contact. Java
Is a simple class, indicating a contact record in the database .)

Code List 3. Contact. Java

Package com. javaworld. sample. osgi. Spring. contact;
Public class contact {
Int contactid;
String firstname;
String lastname;
Public int getcontactid (){
Return contactid;
}
Public void setcontactid (INT contactid ){
This. contactid = contactid;
}
}

Next, we will create the contactdao. Java interface, as shown in code list 4.

Code list 4. contactdao Interface

Package com. javaworld. sample. osgi. Spring. contact;
Public interface contactdao {
Public list getcontactlist ();
Public contact getcontact (INT contactid );
Public void insertcontact (contact );
Public void updatecontact (contact );
Public void deletecontact (INT contactid );
}

Contactdao is a simple crud interface that defines methods for creating, updating, retrieving, and deleting operations.

Now, create the contactdao. Java class implementation, as shown in code list 5.

Code List 5. contactdaoimpl. Java

Package com. javaworld. sample. osgi. Spring. Contact. impl;
Public class contactdaoimpl implements contactdao {
Public contact getcontact (INT contactid ){
System. Out. println ("Inside contactdaoimpl. getcontact ()");
Return NULL;
}
// Do nothing implementation of all other methods defined in contactdao
}

Contactdaoimpl. Java provides a "do
The implementation of nothing (return NULL. All we need to do is write the method name to system. out using this class.

Please note that both contact and contactdao must be public classes (to use the contactdao service, other suites need to access them) and
Com. javaworld. sample. osgi. Spring. Contact package. However, the actual implementation class
Contactdaoimpl. Java (for the contactdao suite is an internal class) located in
Com. javaworld. sample. osgi. Spring. Contact. impl package.

Next, we will modify the manifest. MF file of the contactdao suite to export
Com. javaworld. sample. osgi. Spring. Contact suite.
The suite has accessed it. We only need to add a line of code in manifest. MF: Export-package:
Com. javaworld. sample. osgi. Spring. Contact

Spring configuration of spring DM

Next, we will create the spring configuration file. The recommended method in spring DM is to divide the configuration into two files, one for defining spring
Bean, the other is used to output spring as the osgi service. Next, we will divide the configuration of the example application into two files. The first step is
Create a META-INF file in the contactdao-service.xml/spring folder, as shown in code listing 6.

Code List 6. Spring context (contex) 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 = "contactdaoservice"
Class = "com. javaworld. sample. osgi. Spring. Contact. impl. contactdaoimpl">
</Bean>
</Beans>

This simple spring context file defines contactdaoservice, pointing
Com. javaworld. sample. osgi. Spring. Contact. impl. contactdaoimpl class.

Next, we will create the META-INF/spring/contactdao-osgi.xml file
The contactdaoservice object is exported as an osgi object:

Code listing 7. contactdao-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 = "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: service id = "contactdaoosgiservice" ref = "contactdaoservice"
Interface = "com. javaworld. sample. osgi. Spring. Contact. contactdao">
</Osgi: Service>
</Beans>

The contactdao-osgi.xml contains only one <service> element
Export to the osgi service in the public register. The service <service> [element] must have at least two attributes: one is the ID attribute, including one and the other
The value of the spring bean name is equal, and the other is the interface property. Its value should be equal to the interface Name (the Service under the interface name will be exported ). (<
Service> complete list of attributes supported by the element, see spring DM Reference Guide ).

Now our contactdao is ready. The next step is to extend helloworld so that it can use the new service.

Helloworld as a consumer

If you want this simple helloworld application to act as a consumer, you must grant it the permission to do so. The first step is to change the suite's
In the manifest. MF file, add an import-package statement as follows:

 
 
  1. Import-Package: com.javaworld.sample.osgi.spring.contact 

Now, the helloworld suite will be able to access
Class exported by com. javaworld. sample. osgi. Spring. Contact package.

Next we will modify the helloworld. Java class, as shown in code listing 8.

Code List 8. Changes to helloworld. Java

Public class helloworld {
Contactdao;
Public contactdao getcontactdao (){
Return contactdao;
}
Public void setcontactdao (contactdao ){
This. contactdao = contactdao;
}
Public void start () throws exception {
System. Out. println ("Hello spring world !! ");
System. Out. println (contactdao. getcontactlist ());
}
Public void stop () throws exception {
System. Out. println ("Goodbye spring world !! ");
}
}

In code listing 8, we first add contactdao as the Java Bean attribute, including all the related getter and setter
Method. Next, we modify the START () method of the class to call getcontactlist () of the contactdao service ()
Method and output "Hello spring world !!" Message.

Spring configuration file

The spring configuration file of helloworld is divided into two files: helloworld. xml and
Helloworld-osgi.xml. Let's start with the helloworld-osgi.xml, as shown in listing 9.

Code List 9. Spring configuration-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 = "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>

This helloworld-osgi.xml file declares a reference element used to index the osgi service and use it as a spring bean in
Available in helloworld suite. As mentioned earlier, the referenced element contains two attributes: ID and interface. When you add an osgi service as an application
Spring DM uses the ID attribute value when using spring bean. In this case, we have pointed out that spring DM should make this service
Contactdaoservice is available in the application context of the helloworld suite.

The second attribute is interface. Spring DM uses the value of this attribute to find services that match the specified interface. In the sample code, we have already said that we want an implementation
Com. javaworld. sample. osgi. Spring. Contact. contactdao interface service.

Spring DM calls bundlecontext. getservicereference () to find and implement
Com. javaworld. sample. osgi. Spring. Contact. contactdao interface service. If
In the framework, if more than one service matches the requirement, the Service with the highest level will be returned. In addition, you can use the filter attribute to precisely define the services you want.

Next, we will modify the helloworld. xml file so that it can inject the contactdaoservice object to our hello
Bean, as shown in code 10.

Code List 10. Spring configuration-helloworld. 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"
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 contactdaoservice is injected into the application context of the suite, you can use it as any other spring
Bean. In the sample code, we inject the service as the contactdao attribute of helloworld bean.

Helloworld import Service

Execute your suite in Eclipse IDE. When you start helloworld suite, "Hello Spring" should be displayed in your console
World !! Inside contactdaoimpl. getcontactlist () "message. In the background, once spring Extender
The suite is started and it will see two suites provided by spring. As a response, it will first create an application context for the contactdao suite. At the same time, it looks
Contactdao-osgi.xml file and export contactdao as the osgi service in the public register. Next, it will try
The helloworld suite creates an application context. Extender calls
Bundlecontext. getservice ("com. javaworld. sample. osgi. Spring. Contact. contactdao ")
The method is used to find class services that implement the com. javaworld. sample. osgi. Spring. Contact. contactdao interface.

In the sample code (see Code List 5), contactdaoimpl is the only service that implements this interface. Therefore, extender returns
An object of contactdaoimpl. Once this object is returned, spring DM injects it
In helloworld bean.

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.