三步学会用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五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Java面试题:通过实例说明内连接、左外连接和右外连接的区别

在 SQL 中,连接(JOIN)用于在多个表之间组合行。最常用的连接类型是内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。它们的主要区别在于它们如何处理表之间的匹配和不匹配行。下面是每种连接的详细说明和示例。 表示例 假设有两个表:Customers 和 Orders。 Customers CustomerIDCus