Virgo and Maven integrated development environment construction (3)

             3.MP3、Picture

                     Let's first look at the picture search implementation.

                     In the pom, the packaging rules can be inherited from the application class bundle packaging rules. In addition to the packaging rules, you also need to add spring dependencies and api package dependencies.

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>org.springframework.spring-library</artifactId>
			<type>libd</type>
                        <version>3.5.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.phantom.demo</groupId>
			<artifactId>org.phantom.demo.api</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>

                     After adding dependencies, you can write code. Let's first look at PictureSearchBean. We let the picture object have two properties, picture title and picture link. The class is very simple, as the data carrier and the entity object passed during bundle communication.

package org.phantom.demo.search.picture;

import org.phantom.demo.api.SearchBean;

public class PictureSearchBean implements SearchBean {

	private String title;
	private String url;
	public PictureSearchBean() {
	}
	public PictureSearchBean(String title, String url) {
		this.title = title;
		this.url = url;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

}

                       Next, let's look at the implementation code of the picture search business, which is also very simple. For simplicity, we simulate some local data and show the meaning.

package org.phantom.demo.search.picture;

import java.util.ArrayList;
import java.util.List;

import org.phantom.demo.api.SearchBean;
import org.phantom.demo.api.SearchHandler;
import org.springframework.stereotype.Service;


@Service("pictureSearch")
public class PictureSearchHandler implements SearchHandler {

	private static List<PictureSearchBean> beans = new ArrayList<PictureSearchBean>();
	
	static{
		beans.add(new PictureSearchBean("aaaaa", "aaaaaa"));
		beans.add(new PictureSearchBean("bbbbb", "vvvvvv"));
		beans.add(new PictureSearchBean("ccccc", "aaaaaa"));
		beans.add(new PictureSearchBean("daaddd", "aaaaaa"));
		beans.add(new PictureSearchBean("ddddd", "aaaaaa"));
	}
	
	
	public List<? extends SearchBean> doSearch(String key) {
		List<PictureSearchBean> temp = new ArrayList<PictureSearchBean>();
		for (PictureSearchBean b : beans) {
			if(b.getTitle().contains(key))
				temp.add(b);
		}
		return temp;
	}

}

                       Traverse the data through the key and find the return that meets the requirements. A very simple service. Next, let's see how to publish this service. In ordinary OSGI, it is not difficult to publish the service, and it is ok to write a few lines of code.

bundleContext.registerService(SearchHandler.class,new PictureSearchHandler(),null);

                        In Virgo (referred to as Spring-DM here), it is actually registered in this way, but this line of code does not need to be written by you. Leave the registration of the service to spring. The first step is to publish this class as Spring beans.

@Service("pictureSearch")

                         Then, take a look at the spring configuration file.META-INF/spring/applicationContext.xml (default location)

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd">
		
	<context:component-scan  base-package="org.phantom.demo.search.picture"/>
	<osgi:service ref="pictureSearch" interface="org.phantom.demo.api.SearchHandler"/>
  </beans>

                        Explain this configuration file roughly. Import spring-osgi-schema

xmlns:osgi="http://www.springframework.org/schema/osgi"

                         Then open the annotation scanning, add the bean to the spring management, the focus is the following sentence.

<osgi:service ref="pictureSearch" interface="org.phantom.demo.api.SearchHandler"/>

                         In this way, a bean is published as an OSGI service. osgi:service has some other properties, which are not introduced here. The purpose of this article is to introduce how we use Virgo in development and how to integrate with Maven for development. Others The content, I believe, when you see this, you must have a certain ability to learn. It's OK to check it yourself.

                          Take another look at the OSGI description of Picture.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: picutre search module
Bundle-SymbolicName: org.phantom.demo.search.picture
Bundle-Version: 1.0.0.SNAPSHOT
Excluded-Imports: org.phantom.demo.search.picture
Import-Template: org.springframework.*;version="[3.0.5,4)"
Import-Package: org.springframework.context.config;version="[3.0.5,4)",
 org.eclipse.gemini.blueprint.config;version="[1.0.0,2)"

                         There is no api package in our template.mf, but it must depend on the api package. Let's leave it to the bundlor plugin to save us a little time. You can open the final generated MANIFEST.MF and take a look

                       We develop another implementation in the same way. MP3

                           Paste some key code and configuration. In fact, it is basically the same as Picture.

package org.phantom.demo.search.mp3;

import org.phantom.demo.api.SearchBean;

public class Mp3SearchBean implements SearchBean{

	private String name;
	private String singer;
	
	public Mp3SearchBean() {
	}
	public Mp3SearchBean(String name, String singer) {
		this.name = name;
		this.singer = singer;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSinger() {
		return singer;
	}
	public void setSinger(String singer) {
		this.singer = singer;
	}
}

 

package org.phantom.demo.search.mp3;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import org.phantom.demo.api.SearchBean;
import org.phantom.demo.api.SearchHandler;

@Service("mp3Search")
public class Mp3SearchHandler implements SearchHandler{

	private static List<Mp3SearchBean> beans = new ArrayList<Mp3SearchBean>();
	
	static{
		beans.add(new Mp3SearchBean("aaaaa", "aaaaaa"));
		beans.add(new Mp3SearchBean("bbbbb", "vvvvvv"));
		beans.add(new Mp3SearchBean("ccccc", "aaaaaa"));
		beans.add(new Mp3SearchBean("daaddd", "aaaaaa"));
		beans.add(new Mp3SearchBean("ddddd", "aaaaaa"));
	}
	
	
	public List<? extends SearchBean> doSearch(String key) {
		List<Mp3SearchBean> temp = new ArrayList<Mp3SearchBean>();
		for (Mp3SearchBean b : beans) {
			if(b.getName().contains(key))
				temp.add(b);
		}
		return temp;
	}

}

 

<?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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd">
	<context:component-scan  base-package="org.phantom.demo.search.mp3"/>
	<osgi:service ref="mp3Search" interface="org.phantom.demo.api.SearchHandler"/>
  </beans>

 

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: mp3 search module
Bundle-SymbolicName: org.phantom.demo.search.mp3
Bundle-Version: 1.0.0.SNAPSHOT
Excluded-Imports: org.phantom.demo.search.mp3
Import-Template: org.springframework.*;version="[3.0.5,4)"
Import-Package: org.springframework.context.config;version="[3.0.5,4)",
 org.eclipse.gemini.blueprint.config;version="[1.0.0,2)"

                         You can add spring and api dependencies to pom.


 

 


 
 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326932070&siteId=291194637