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

2024-06-20 17:32

本文主要是介绍三步学会用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)*/@Overridepublic 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://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.xsdhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://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.xsdhttp://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

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

这篇关于三步学会用spring开发OSGI——(第二步:工程篇)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1078785

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-