当前位置: 首页 > news >正文

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

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

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

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

接口工程创建

新建vigro插件工程

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

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

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

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

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

复制代码
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中的内容:

复制代码
Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: Register
Bundle-ManifestVersion: 2
Bundle-Description: TGYT
Bundle-SymbolicName: com.tgyt.test.register
Export-Package: com.tgyt.test.register
复制代码

数据库存储工程创建

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

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

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

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

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

复制代码
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,这个用来注入接口

复制代码
<?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,这个用来发布服务

复制代码
<?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,输入的内容如下:

复制代码
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:注入测试类

复制代码
<?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服务,内容如下:

复制代码
<?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"属性,需要将

<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

log.wrapSysOut=false
log.wrapSysErr=false

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

相关文章:

  • 个推基于Consul的配置管理
  • Android精品开源整理
  • ECS应用管理最佳实践
  • 大数据分析专题:利用向外扩展技术深入挖掘商业价值(1)
  • Apache搭建简单的图片访问服务器
  • Bootloader之uBoot简介
  • 分布式熔断降级平台aegis
  • 建议
  • dubbo分布式事务
  • HDOJ(HDU) 2178 猜数字(题意有点难理解、、、)
  • Grafana 6.0正式发布!新增查询工作流,全新独立Gauge面板
  • 腾讯视频嵌入手机端网站demo - 就像微信文章中一样一样的
  • java2019面试题北京
  • ruby安装mysql2,pg模块
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • [译]前端离线指南(上)
  • Create React App 使用
  • go append函数以及写入
  • js中的正则表达式入门
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • vue-router 实现分析
  • webpack项目中使用grunt监听文件变动自动打包编译
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 大主子表关联的性能优化方法
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 基于组件的设计工作流与界面抽象
  • 通信类
  • #100天计划# 2013年9月29日
  • #stm32整理(一)flash读写
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (NO.00004)iOS实现打砖块游戏(十二):伸缩自如,我是如意金箍棒(上)!
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (论文阅读30/100)Convolutional Pose Machines
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • .NET : 在VS2008中计算代码度量值
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .net core 依赖注入的基本用发
  • .net 无限分类
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .net和php怎么连接,php和apache之间如何连接
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • /proc/vmstat 详解
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录
  • @Responsebody与@RequestBody
  • [ vulhub漏洞复现篇 ] JBOSS AS 5.x/6.x反序列化远程代码执行漏洞CVE-2017-12149
  • [383] 赎金信 js