3.MP3、Picture
先来看picture搜索实现.
pom中,打包规则可以继承自应用类bundle打包规则。除了打包规则,还需要加入spring的依赖和api包的依赖。
<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>
添加完依赖,可以进行代码编写了.先来看PictureSearchBean,我们让picture对象拥有两个属性,图片标题和图片链接.类很简单,作为数据载体及bundle通讯时传递的实体对象.
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;
}
}
接下来看picture搜索业务的实现代码,同样也很简单.我们为了简便,模拟一些本地数据,表明意思即可.
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;
}
}
通过key遍历数据,找到符合要求的返回.一个很简单的service.接下来看如何将这个服务发布.再普通OSGI中,发布该服务也不是很难,编写几行代码就ok了.
bundleContext.registerService(SearchHandler.class,new PictureSearchHandler(),null);
在Virgo中(这里看作Spring-DM),其实也是这样注册的,只不过这行代码不需要你亲自写.把注册service的事情完全交给spring去做.第一步,将这个类发布成Spring bean.
@Service("pictureSearch")
然后,来看一下spring配置文件.META-INF/spring/applicationContext.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: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>
大致解释一下这个配置文件.将spring-osgi-schema导入
xmlns:osgi="http://www.springframework.org/schema/osgi"
然后打开注解扫描,将bean加入到spring的管理中,重点就是下面一句.
<osgi:service ref="pictureSearch" interface="org.phantom.demo.api.SearchHandler"/>
这样就将一个bean发布成OSGI的service了.osgi:service还有一些其他属性,这里不过多介绍.这篇文章的目的是向大家介绍我们在开发中如何使用Virgo,如何与Maven集成开发.其他的内容,我相信,当你看到这里的时候,肯定具有一定的学习能力.自己去查一下就OK.
再来看一下Picture的OSGI描述.
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)"
我们template.mf中没有写任何api包的东西,但是它肯定是依赖api包的.这就交给bundlor插件去做吧,给我们节省一点时间.可以打开最后生成的MANIFEST.MF看一下
我们按照同样的方式,开发另一个实现.MP3
再贴一些关键代码和配置.其实基本和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)"
pom中加入spring和api的依赖即可.