从 Felix SCR 迁移到 OSGI Declarative Services 时,Karaf 中未列出服务

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

我正在从 Felix SCR Annotations 迁移到 R6 OSGI Declarative Services,但是该服务没有在 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 元素。