簡體   English   中英

從 Felix SCR 遷移到 OSGI 聲明式服務時,服務未在 Karaf 中列出

[英]Services are not listed in Karaf while migrating from Felix SCR to OSGI Declarative Services

我正在從 Felix SCR 注釋遷移到 R6 OSGI 聲明式服務,但該服務未在 karaf 中列出。根據下面的代碼SampleServiceImpl應該列出的是 karaf。但它沒有列出。 我必須在 pom.xml 中進行任何其他配置嗎?

package com.sample.test;


import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;

    @Component (configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true, service = SampleService.class)
    public class SampleServiceImpl implements SampleService
    {
    
     @Reference (policy = ReferencePolicy.DYNAMIC, service = AgentService.class , bind = "bindAgentService",unbind ="unbindAgentService")
    private AgentService agentService;
    
     
     @Activate
        protected void activate() {
            System.out.println("activate ");
        }
    
        @Deactivate
        protected void deactivate() {
            System.out.println("de-activate ");
        }
    }

這是我正在使用的 pom.xml。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <groupId>com.test.sample</groupId>
  <artifactId>compile</artifactId>
  <version>1.0.0</version>

  
  <dependencies>      
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>osgi.cmpn</artifactId>
       <version>6.0.0</version>
       <scope>provided</scope>
    </dependency>
  </dependencies>

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
      </plugin>
    </plugins>   
  </build>
</project>
@Reference (policy = ReferencePolicy.DYNAMIC, service = AgentService.class , bind = "bindAgentService",unbind ="unbindAgentService")

您列出了 2 個方法, bindAgentServiceunbindAgentService ,它們沒有出現在您的 class 中。 您還可以將@Reference注釋應用於字段。 你想要什么? 現場注入? 方法注入? 兩個都? 如果您只想進行字段注入,請刪除注釋中的bindunbind元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM