第十一章:使用Virgo开发OSGi应用程序-工程篇

来源:互联网 发布:linux网络命令大全 编辑:程序博客网 时间:2024/06/16 10:25

1.工程说明

我们模拟一个注册程序来体验一下OSGi工程的开发,这里一共会用到4个bundler,分别是:

com.osgi.test //测试工程

com.osgi.test.register //注册接口

com.osgi.test.register.db //写入数据库的实现

com.osgi.test.register.file //写入文件的实现


2.创建接口工程-com.osgi.test.register

2.1新建插件工程







后面全部用默认的配置,下一步、下一步直到完成即可

2.2编写注册接口

项目结构



User.java代码

package com.osgi.test.register;public class User {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;}}

RegisterService.java代码

package com.osgi.test.register;public interface RegisterService {public void register(User user);}

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



3.创建数据库实现工程-com.osgi.test.register.db

创建插件工程方法同上


3.1引用接口

在这里需要先对上边的工程进行引用,在新建的工程上点击右键->Properties,选择工程引用,引用上边建立好的工程,这个步骤一定不能少,而且必须先操作,否则在MF文件中引用的时候会报错,这时需要先remove掉导入的package再重新引入



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



把自己暴露出来,给后面的测试工程使用



3.2编写实现代码

RegisterDB.java

package com.osgi.test.register.db;import com.osgi.test.register.RegisterService;import com.osgi.test.register.User;public class RegisterDB implements RegisterService {@Overridepublic void register(User user) {System.out.println("将下面的信息存入到数据库中……");        System.out.println(user.getName());        System.out.println(user.getAge());}}

因为这个是实现的bundle,所以需要将实现的配置暴露出来,发布成其它工程能引用的spring服务
在META-INF目录下新建目录spring,这个是osgi容器自动扫描的目录,每次部署应用时osgi会自动扫描下边的*.xml文件
我们在这里添加两个文件,一个用来部署spring应用文件,一个用来对外发布接口

beans.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="registerDB" class="com.osgi.test.register.db.RegisterDB"/></beans>

osgi.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="osgiRegisterDB" ref="registerDB" interface="com.osgi.test.register.RegisterService"/></beans:beans>

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

目录结构


4.创建写文件实现工程-com.osgi.test.register.file

创建插件工程方法同上,只是配置文件略有不同

项目结构



RegisterFile.java

package com.osgi.test.register.file;import com.osgi.test.register.RegisterService;import com.osgi.test.register.User;public class RegisterFile implements RegisterService {@Overridepublic void register(User user) {System.out.println("将下面的信息写入到文件中……");        System.out.println(user.getName());        System.out.println(user.getAge());}}

beans.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="registerFile" class="com.osgi.test.register.file.RegisterFile"/></beans>

osgi.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="osgiRegisterFile" ref="registerFile" interface="com.osgi.test.register.RegisterService"/></beans:beans>

5.创建测试工程-com.osgi.test

创建插件工程方法同上

5.1引用接口




5.2编写测试代码

项目结构



TestInput.java

package com.osgi.test;import com.osgi.test.register.RegisterService;import com.osgi.test.register.User;public class TestInput {private RegisterService registerService;public void register() {System.out.println("进入注册程序……");        User user = new User();        user.setName("zhansan");        user.setAge(21);        registerService.register(user);        System.out.println("结束注册……");}public RegisterService getRegisterService() {return registerService;}public void setRegisterService(RegisterService registerService) {this.registerService = registerService;}}

osgi.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="osgiRegisterDB" bean-name="registerDB" interface="com.osgi.test.register.RegisterService" />    <reference id="osgiRegisterFile" bean-name="registerFile" interface="com.osgi.test.register.RegisterService" /></beans:beans>

beans.xml,加入了init-method属性,让项目部署的时候自动调用方法来测试效果,这里引用的是osgiRegisterFile,可自行替换看效果

<?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.osgi.test.TestInput" init-method="register"><property name="registerService" ref="osgiRegisterFile" /></bean></beans>

6.启动Virgo来测试

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



点击Finish,控制台打印信息



然后再加入测试工程,控制台打印



7.控制台不打印测试信息

需要修改org.eclipse.virgo.medic.properties文件,这个文件在virgo的解压目录下的configuration下,将下面两个属性的值改为false即可
log.wrapSysOut=false
log.wrapSysErr=false



0 0