Virgo与Maven整合开发环境搭建(三)

来源:互联网 发布:编写windows应用程序 编辑:程序博客网 时间:2024/06/05 03:09

  3.MP3、Picture

                     先来看picture搜索实现.

                     pom中,打包规则可以继承自应用类bundle打包规则。除了打包规则,还需要加入spring的依赖和api包的依赖。

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>org.springframework</groupId>  
  3.     <artifactId>org.springframework.spring-library</artifactId>  
  4.     <type>libd</type>  
  5.                       <version>3.5.0.RELEASE</version>  
  6. </dependency>  
  7. <dependency>  
  8.     <groupId>org.phantom.demo</groupId>  
  9.     <artifactId>org.phantom.demo.api</artifactId>  
  10.     <version>1.0.0-SNAPSHOT</version>  
  11. </dependency>  

                     添加完依赖,可以进行代码编写了.先来看PictureSearchBean,我们让picture对象拥有两个属性,图片标题和图片链接.类很简单,作为数据载体及bundle通讯时传递的实体对象.

Java代码  收藏代码
  1. package org.phantom.demo.search.picture;  
  2.   
  3. import org.phantom.demo.api.SearchBean;  
  4.   
  5. public class PictureSearchBean implements SearchBean {  
  6.   
  7.     private String title;  
  8.     private String url;  
  9.     public PictureSearchBean() {  
  10.     }  
  11.     public PictureSearchBean(String title, String url) {  
  12.         this.title = title;  
  13.         this.url = url;  
  14.     }  
  15.     public String getTitle() {  
  16.         return title;  
  17.     }  
  18.     public void setTitle(String title) {  
  19.         this.title = title;  
  20.     }  
  21.     public String getUrl() {  
  22.         return url;  
  23.     }  
  24.     public void setUrl(String url) {  
  25.         this.url = url;  
  26.     }  
  27.   
  28. }  

                       接下来看picture搜索业务的实现代码,同样也很简单.我们为了简便,模拟一些本地数据,表明意思即可.

Java代码  收藏代码
  1. package org.phantom.demo.search.picture;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.phantom.demo.api.SearchBean;  
  7. import org.phantom.demo.api.SearchHandler;  
  8. import org.springframework.stereotype.Service;  
  9.   
  10.   
  11. @Service("pictureSearch")  
  12. public class PictureSearchHandler implements SearchHandler {  
  13.   
  14.     private static List<PictureSearchBean> beans = new ArrayList<PictureSearchBean>();  
  15.       
  16.     static{  
  17.         beans.add(new PictureSearchBean("aaaaa""aaaaaa"));  
  18.         beans.add(new PictureSearchBean("bbbbb""vvvvvv"));  
  19.         beans.add(new PictureSearchBean("ccccc""aaaaaa"));  
  20.         beans.add(new PictureSearchBean("daaddd""aaaaaa"));  
  21.         beans.add(new PictureSearchBean("ddddd""aaaaaa"));  
  22.     }  
  23.       
  24.       
  25.     public List<? extends SearchBean> doSearch(String key) {  
  26.         List<PictureSearchBean> temp = new ArrayList<PictureSearchBean>();  
  27.         for (PictureSearchBean b : beans) {  
  28.             if(b.getTitle().contains(key))  
  29.                 temp.add(b);  
  30.         }  
  31.         return temp;  
  32.     }  
  33.   
  34. }  

                       通过key遍历数据,找到符合要求的返回.一个很简单的service.接下来看如何将这个服务发布.再普通OSGI中,发布该服务也不是很难,编写几行代码就ok了.

Java代码  收藏代码
  1. bundleContext.registerService(SearchHandler.class,new PictureSearchHandler(),null);  

                        在Virgo中(这里看作Spring-DM),其实也是这样注册的,只不过这行代码不需要你亲自写.把注册service的事情完全交给spring去做.第一步,将这个类发布成Spring bean.

Java代码  收藏代码
  1. @Service("pictureSearch")  

                         然后,来看一下spring配置文件.META-INF/spring/applicationContext.xml(默认位置)

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:osgi="http://www.springframework.org/schema/osgi"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.         http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd">  
  11.           
  12.     <context:component-scan  base-package="org.phantom.demo.search.picture"/>  
  13.     <osgi:service ref="pictureSearch" interface="org.phantom.demo.api.SearchHandler"/>  
  14.   </beans>  

                        大致解释一下这个配置文件.将spring-osgi-schema导入

Xml代码  收藏代码
  1. xmlns:osgi="http://www.springframework.org/schema/osgi"  

                         然后打开注解扫描,将bean加入到spring的管理中,重点就是下面一句.

Xml代码  收藏代码
  1. <osgi:service ref="pictureSearch" interface="org.phantom.demo.api.SearchHandler"/>  

                         这样就将一个bean发布成OSGI的service了.osgi:service还有一些其他属性,这里不过多介绍.这篇文章的目的是向大家介绍我们在开发中如何使用Virgo,如何与Maven集成开发.其他的内容,我相信,当你看到这里的时候,肯定具有一定的学习能力.自己去查一下就OK.

                          再来看一下Picture的OSGI描述.

Java代码  收藏代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: picutre search module  
  4. Bundle-SymbolicName: org.phantom.demo.search.picture  
  5. Bundle-Version: 1.0.0.SNAPSHOT  
  6. Excluded-Imports: org.phantom.demo.search.picture  
  7. Import-Template: org.springframework.*;version="[3.0.5,4)"  
  8. Import-Package: org.springframework.context.config;version="[3.0.5,4)",  
  9.  org.eclipse.gemini.blueprint.config;version="[1.0.0,2)"  

                         我们template.mf中没有写任何api包的东西,但是它肯定是依赖api包的.这就交给bundlor插件去做吧,给我们节省一点时间.可以打开最后生成的MANIFEST.MF看一下

                       我们按照同样的方式,开发另一个实现.MP3

                           再贴一些关键代码和配置.其实基本和Picture一模一样.

Java代码  收藏代码
  1. package org.phantom.demo.search.mp3;  
  2.   
  3. import org.phantom.demo.api.SearchBean;  
  4.   
  5. public class Mp3SearchBean implements SearchBean{  
  6.   
  7.     private String name;  
  8.     private String singer;  
  9.       
  10.     public Mp3SearchBean() {  
  11.     }  
  12.     public Mp3SearchBean(String name, String singer) {  
  13.         this.name = name;  
  14.         this.singer = singer;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.     public String getSinger() {  
  23.         return singer;  
  24.     }  
  25.     public void setSinger(String singer) {  
  26.         this.singer = singer;  
  27.     }  
  28. }  

 

Java代码  收藏代码
  1. package org.phantom.demo.search.mp3;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.springframework.stereotype.Service;  
  7.   
  8. import org.phantom.demo.api.SearchBean;  
  9. import org.phantom.demo.api.SearchHandler;  
  10.   
  11. @Service("mp3Search")  
  12. public class Mp3SearchHandler implements SearchHandler{  
  13.   
  14.     private static List<Mp3SearchBean> beans = new ArrayList<Mp3SearchBean>();  
  15.       
  16.     static{  
  17.         beans.add(new Mp3SearchBean("aaaaa""aaaaaa"));  
  18.         beans.add(new Mp3SearchBean("bbbbb""vvvvvv"));  
  19.         beans.add(new Mp3SearchBean("ccccc""aaaaaa"));  
  20.         beans.add(new Mp3SearchBean("daaddd""aaaaaa"));  
  21.         beans.add(new Mp3SearchBean("ddddd""aaaaaa"));  
  22.     }  
  23.       
  24.       
  25.     public List<? extends SearchBean> doSearch(String key) {  
  26.         List<Mp3SearchBean> temp = new ArrayList<Mp3SearchBean>();  
  27.         for (Mp3SearchBean b : beans) {  
  28.             if(b.getName().contains(key))  
  29.                 temp.add(b);  
  30.         }  
  31.         return temp;  
  32.     }  
  33.   
  34. }  

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:osgi="http://www.springframework.org/schema/osgi"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.         http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd">  
  11.     <context:component-scan  base-package="org.phantom.demo.search.mp3"/>  
  12.     <osgi:service ref="mp3Search" interface="org.phantom.demo.api.SearchHandler"/>  
  13.   </beans>  

 

Xml代码  收藏代码
  1. Manifest-Version: 1.0  
  2. Bundle-ManifestVersion: 2  
  3. Bundle-Name: mp3 search module  
  4. Bundle-SymbolicName: org.phantom.demo.search.mp3  
  5. Bundle-Version: 1.0.0.SNAPSHOT  
  6. Excluded-Imports: org.phantom.demo.search.mp3  
  7. Import-Template: org.springframework.*;version="[3.0.5,4)"  
  8. Import-Package: org.springframework.context.config;version="[3.0.5,4)",  
  9.  org.eclipse.gemini.blueprint.config;version="[1.0.0,2)"  

                         pom中加入spring和api的依赖即可.

0 0
原创粉丝点击