osgi之blueprint.xml注入bean

来源:互联网 发布:淘宝用户名被冻结 编辑:程序博客网 时间:2024/06/04 19:19

摘要 在osgi中服务的使用有多种方式,如使用传统的注册式服务,就是我们之前中example不断使用的使用方式,还是osgi中的声明式服务,还有著名的ipojo等等,但在这里值得花一番笔墨来讲解的应该是blueprint,说到blueprint,务必需要提及一下spring,在个人印象中,spring框架几乎成为了java这么多框架中最为有名的一个,传统上提出的ssh,几乎就只剩下spring一个还非常强力的存活着,而且本身提供的功能也越来越丰富,子项目中springboot也快成为业界微服务一大选择,而在osgi中,spring最初也有所涉及,为spring dm,blueprint产生的规范便是起源于spring dm,随后好像spring dm没有继续发展,但是blueprint却是一直存在目前,Blueprint规范主要有两个实现:Aries blueprint和Gemini blueprint,它们分别来自Apache和Eclipse两个开源组织,具体的地址如下:http://aries.apache.org/documentation/tutorials/blueprinthelloworldtutorial.html

一. blueprint.xml运用

在osgi环境下,直接在resource目录下新建OSGI-INF.blueprint目录,在此目录下新建blueprint.xml即可,这个xml名字随便取,如图

这里写图片描述

二 . 注入bean

在这里,我会把基于注解配置,也顺便提一下,我们先从xml配置开始,如下

<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-timeout="0">    <!--import service-->    <!--         引入进来的服务接口        filter: 过滤条件    -->    <reference id="daoManagentSvr" interface="com.yf.af.data.DaoManagement" />    <reference id="dataSource" interface="javax.sql.DataSource" filter="(dataSourceName=yfdb)" />    <bean id="messageQueue" class="com.yf.af.comm.model.MessageQueue"/>    <!--        scope:单例        init-method:模块加载时执行的方法        destroy-method: 模块退出时执行的方法    -->    <bean id="advServiceBean" class="com.yf.af.comm.service.impl.AdvServiceImpl" scope="singleton" init-method="init" destroy-method="destroy">        <property name="messageQueue" ref="messageQueue" />        <property name="daoManagement" ref="daoManagentSvr" />        <property name="dataSource" ref="dataSource" />    </bean>    <!-- Export-Service-->    <!--放出去的接口服务-->    <service id="advService" ref="advServiceBean" interface="com.yf.af.comm.service.AdvService"/></blueprint>

配置格式和spring .xml很类似,一些的重要的概念在注释里面说了

哦,对了,顺便提一下,在AdvServiceImpl实现里面怎么调用property ,我们就拿daoManagement这个属性写一下

    private DaoManagement daoManagement;    public void setDaoManagement(DaoManagement daoManagement) {        this.daoManagement = daoManagement;    }

这样,实现里就可以使用daoManagement里面的方法了
基于xml配置的就差不多这些了

在来看一下基于注解的

所需jar

            <dependency>                <groupId>javax.inject</groupId>                <artifactId>javax.inject</artifactId>                <version>1</version>                <optional>true</optional>            </dependency>            <dependency>                <groupId>org.apache.aries.blueprint</groupId>                <artifactId>blueprint-maven-plugin-annotation</artifactId>                <version>1.1.0</version>                <optional>true</optional>            </dependency>            <dependency>                <groupId>org.ops4j.pax.cdi</groupId>                <artifactId>pax-cdi-api</artifactId>                <version>1.0.0.RC2</version>                <optional>true</optional>           </dependency>

上面只是针对注解的,其他的jar省略

@Singleton@Named("advServiceBean")@OsgiServiceProviderpublic class AdvServiceImpl implements AdvService {    @Inject    @OsgiService    private DaoManagement daoManagement;    @Inject    @OsgiService(filter = "(dataSourceName=yfdb)")    private javax.sql.DataSource dataSource;    @PostConstruct    public void init(){       // 模块加载时运行    }    @PreDestroy    public void destroy(){       // 模块退出时运行    }  }

上面的配置大致说一下

@Inject 按类型注入bean
@OsgiService 创建对OSGi服务的引用。这里可以使用过滤器。
@OsgiServiceProvider 给指定的接口发布bean作为OSGi服务
@Named 命名bean

注解配置可以参考一下 http://aries.apache.org/modules/blueprint-maven-plugin.html

使用注解,当你项目打包编译时,它会自己自动生成配置文件,如下图
只不过我是在另外一个类,做的测试,在这里只是说明可以在这里查看

这里写图片描述

好了,先说到这里,摘要部分的说明是http://blog.csdn.net/u012734441/article/details/51818300 复制的

原创粉丝点击