mavenosgiosgi-bundleapache-felixdeclarative-services

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


I'm Migrating from Felix SCR Annotations to R6 OSGI Declarative Services but the Service is not listed inside karaf .As per below code SampleServiceImpl should list is karaf .But it is not listing . Is there any other configuration i have to in 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 ");
        }
    }

This is the pom.xml I am using .

<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>

Solution

  • @Reference (policy = ReferencePolicy.DYNAMIC, service = AgentService.class , bind = "bindAgentService",unbind ="unbindAgentService")
    

    You list 2 methods, bindAgentService and unbindAgentService, which do not appear in your class. You also apply the @Reference annotation to a field. What do you want? Field injection? Method injection? Both? If you only want field injection, remove the bind and unbind elements in the annotation.