<dependency>
<groupId>org.osgi.framework</groupId>
<artifactId>osgi.core</artifactId>
<version>1.8.0</version>
</dependency>
public interface MyService {
void doSomething();
}
public class MyServiceImpl implements MyService {
@Override
public void doSomething() {
System.out.println("Doing something...");
}
}
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Main implements BundleActivator {
private BundleContext context;
@Override
public void start(BundleContext context) {
this.context = context;
System.out.println("OSGi bundle started...");
ServiceReference<MyService> serviceReference = context.getServiceReference(MyService.class);
MyService myService = context.getService(serviceReference);
myService.doSomething();
}
@Override
public void stop(BundleContext context) {
System.out.println("OSGi bundle stopped...");
}
}
Bundle-Name: MyBundle
Bundle-SymbolicName: com.example.mybundle
Bundle-Version: 1.0.0
Export-Package: com.example.service
Provide-Capability: osgi.service;objectClass=com.example.service.MyService