使用Felix的Declarative Services需要解决的依赖

3 篇文章 0 订阅
1 篇文章 0 订阅

在Felix官网的Apache Felix OSGi Tutorial下提供了Felix的入门例子,其中的第9个展示了如何使用Declarative Services,源代码如下:

package tutorial.example9;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.felix.examples.dictionaryservice.DictionaryService;
import org.apache.felix.examples.spellcheckservice.SpellCheckService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;

@Component
public class SpellCheckServiceImpl implements SpellCheckService
{
  
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.AT_LEAST_ONE)
    private volatile List<DictionaryService> m_svcObjList;
  
    public String[] check( String passage )
    {
        if ( ( passage == null ) || ( passage.length() == 0 ) )
        {
            return null;
        }

        List<String> errorList = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer( passage, " ,.!?;:" );
 
        final List<DictionaryService> localServices = m_svcObjList;
        
        while ( st.hasMoreTokens() )
        {
            String word = st.nextToken();
            boolean correct = false;
            
            for(final DictionaryService dictionary : localServices) {
                if ( dictionary.checkWord( word ) )
                {
                    correct = true;
                }
            }

            if ( !correct )
            {
                errorList.add( word );
            }
        }

        if ( errorList.size() == 0 )
        {
            return null;
        }
        return errorList.toArray( new String[errorList.size()] );
    }
}

例子很简单,就是通过声明式服务引入其他Bundle的服务,并利用这些服务来进行拼写检查。但因为原文没有说明依赖的包,所以在调试时花费了很长时间。Declarative Services在编译和运行时需要以下包的支持:

1、OSGi Compendium

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.compendium</artifactId>
    <version>5.0.0</version>
    <scope>provided</scope>
</dependency>

2、Apache Felix Declarative Services

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.scr</artifactId>
    <version>2.1.16</version>
</dependency>

3、OSGi Utilities Promise

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.util.promise</artifactId>
    <version>1.1.1</version>
</dependency>

4、OSGi Utilities Function

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.util.function</artifactId>
    <version>1.1.0</version>
</dependency>

OSGi Compendium是在编译是需要的,否则org.osgi.service.component.*会找不到。而后面三个是在运行环境中需要的,Felix需要Apache Felix Declarative Services来解析声明的服务,Apache Felix Declarative Services又依赖OSGi Utilities PromiseOSGi Utilities Function。如果缺少了这三个包的任何一个,在运行时会报类似于以下的错误:

Unable to resolve com.example.test [7](R 7.0):
    missing requirement [com.example.test [7](R 7.0)] osgi.extender; (&(osgi.extender=osgi.component)(version>=1.3.0)(!(version>=2.0.0)))
  Unresolved requirements:
    [[com.example.test [7](R 7.0)] osgi.extender; (&(osgi.extender=osgi.component)(version>=1.3.0)(!(version>=2.0.0)))]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值