三步学会用spring开发OSGI——(第二步:工程篇)

来源:互联网 发布:linux shell和终端 编辑:程序博客网 时间:2024/05/16 07:10

在上面已经配置了sts及virgo的环境,并且能够成功的运行virgo服务器了。接下来我们来用sts建几个工程。

我们模拟的是一个注册的例子,在我们实际的案例中,有的时候会把数据写入到数据库,写入到文件或者写入到内存中,已方便不同的操作。也许这个例子不能完全说明问题,但是对于说明如何通过sts来建立工程来说已经足够了。

我们会建立4个Bundle,一个是通过页面进行注册的Bundle,一个是录入接口的Bundle,一个是将文件写入到数据库的Bundle(当然只是简单的实现并没有真正写入数据库),还有一个是写入文件的Bundle。

接口工程创建

新建vigro插件工程

输入工程名,下一步,下一步直到完成,其中选择默认的就可以,不需要做其它的修改。

因为是接口工程,所以不需要其它额外的配置,只需要将要导出的目录导出即可。

先创建接口IStore,用来标识接口存储登录信息,接口内容如下:

View Code
public interface IStore {        void store(LoginBean loginBean);    }

其中需要用到JavaBean,命名为LoginBean,这个是用来模拟存储登录信息,代码如下:

View Code
public class LoginBean {        private String name;    private String pass;    private String repass;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPass() {        return pass;    }    public void setPass(String pass) {        this.pass = pass;    }    public String getRepass() {        return repass;    }    public void setRepass(String repass) {        this.repass = repass;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

然后双击META-INF下的MANIFEST.MF文件,设置导出的目录,也就是其它模块可以访问的目录或者说接口

选择Runtime标签,在里边加上刚设置的目录

然后看MAINIFEST.MF中的内容:

View Code
Manifest-Version: 1.0Bundle-Version: 1.0.0Bundle-Name: RegisterBundle-ManifestVersion: 2Bundle-Description: TGYTBundle-SymbolicName: com.tgyt.test.registerExport-Package: com.tgyt.test.register

数据库存储工程创建

这个工程的目的是将数据存储到数据库中,当然只是一个示意的工程,没有将数据真正存入。

创建方法同上,修改工程名,然后在import package中导入上边创建的接口

在这里需要先对上边的工程进行引用,在新建的工程上点击右键,选择工程引用,引用上边建立好的工程

然后添加对工程的正式引用,可以直接引用bundle

在工程中新增类StoreDB,这个用于将注册的信息存储到数据库中,当然这部分可以换成真正的实现类,现在只是几段输出

View Code
public class StoreDB implements IStore {    /* (non-Javadoc)     * @see com.tgyt.register.store.IStore#store(com.tgyt.register.store.LoginBean)     */    @Override    public void store(LoginBean loginBean) {        System.out.println("将下面的信息存入到数据库中……");        System.out.println(loginBean.getName());        System.out.println(loginBean.getAge());    }}

因为这个是实现的bundle,所以需要将实现的配置暴露出来,发布成其它工程能引用的spring服务

在META-INF目录下新建目录spring,这个是osgi容器自动扫描的目录,每次部署应用时osgi会自动扫描下边的*.xml文件

我们在这里添加两个文件,一个用来部署spring应用文件,一个用来对外发布接口,先看appContext.xml,这个用来注入接口

View Code
<?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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <bean name="storeDB" class="com.tgyt.test.register.db.StoreDB"/></beans>

然后看osgi-context.xml,这个用来发布服务

View Code
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/osgi"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"    xsi:schemaLocation="http://www.springframework.org/schema/osgi          http://www.springframework.org/schema/osgi/spring-osgi.xsd        http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd">    <service id="osgiStoreDB" ref="storeDB" interface="com.tgyt.test.register.IStore"/></beans:beans>

ref指定引用的bean的名称

interface指定引用的接口,这两项必须输入

文件存储工程创建

同上,只是配置文件略有修改,修改成存储文件的内容。

测试工程创建

根据上边的步骤建立测试工程,并将上面的三个工程全部关联。

然后在MANIFEST.MF配置中引用三个工程的Bundle

 

新建类TestInput,输入的内容如下:

View Code
public class TestInput {        private IStore iStore;    public IStore getiStore() {        return iStore;    }    public void setiStore(IStore iStore) {        this.iStore = iStore;    }        public void store(){        System.out.println("进入注册程序……");        LoginBean loginBean = new LoginBean();        loginBean.setName("zhansan");        loginBean.setAge(21);        iStore.store(loginBean);        System.out.println("结束注册……");    }}

在META-INF目录下新建目录spring,然后加入文件appContext.xml和osgi-context.xml,两个文件的内容分别是:

appContext.xml:注入测试类

View Code
<?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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <bean name="testInput" class="com.tgyt.test.register.inputtest.TestInput"        init-method="store">        <property name="iStore" ref="osgistore" />    </bean></beans>

在这里为了测试,加入了init-method,也就是在程序部署进去的时候自动加载store这个方法,也就是我们上边要在控制台上输出的内容。

osgi-context.xml的内容是为了引用其它的osgi服务,内容如下:

View Code
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/osgi"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"    xsi:schemaLocation="http://www.springframework.org/schema/osgi          http://www.springframework.org/schema/osgi/spring-osgi.xsd        http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd">    <reference id="osgistore" bean-name="storeFile" interface="com.tgyt.test.register.IStore" /></beans:beans>

注意:如果有多个实现,bean-name必须指定。

好了,工程建立完毕,测试一下。启动virgo服务器。

启动完成后,在服务器上点击右键,选择Add and Remove,先将接口和两个实现的Bundle加入

加入后,完成,这时候控制台会显示:

对应的这几个bundle就会被加载到virgo中。然后我们加入测试工程,如果顺利的话,应该打印出对应的提示信息:

加入后,新加入的bundle自动启动,控制台上打印出:

这个时候如果想把应用改为存储到数据库中,做如下的步骤,首先移除工程

然后修改osgi-context.xml中的bean-name="storeDB",改成另一个bundle暴露出来的接口,然后再将工程添加进去。

控制台上打印出另一个实现中的内容

这样我们就实现了热部署,在有重启应用的情况下,我们可以做把注册信息转成webservice服务、存储到ldap上等实现并且可以动态的切换实现的内容。

遇到的问题

1、测试工程引用时只能调用一个引用

就是在测试工程引用服务的过程中,每次都是指定一个接口的引用。在我的示例里边每次都指向的是数据存储的工程。

经过查找是在引用的时候没有指定"bean-name"属性,需要将

View Code
<bean id="(1)messageServiceBean" scope="bundle" class="com.xyz.MessageServiceImpl"/><!-- service exporter --><osgi:service id="messageServiceExporter" ref="(1)messageServiceBean" interface="com.xyz.MessageService"/><osgi:reference id="messageService" interface="com.xyz.MessageService"   bean-name="(1)messageServiceBean"/>

标1的部分指定为同一个名称,这样在容器内才能找到对应的引用。

2、控制台不打印System.out的信息

默认情况下控制台是不打印SysOut和SysErr的信息的,而是打印到日志文件中,这样我们调试起来非常麻烦,我们需要修改配置文件org.eclipse.virgo.medic.properties

这个文件在virgo的解压目录下的config下,我们需要将下面两项都改成false

View Code
log.wrapSysOut=falselog.wrapSysErr=false

这样控制台能正常的打印信息了。