<dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.12.0</version> <scope>provided</scope> </dependency> package com.example.helloworld; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component public class HelloWorld { @Reference private GreetingService greetingService; public void sayHello() { System.out.println(greetingService.getGreeting() + " World!"); } } package com.example.helloworld; public interface GreetingService { String getGreeting(); } package com.example.helloworld; import org.osgi.service.component.annotations.Component; @Component(service = GreetingService.class) public class GreetingServiceImpl implements GreetingService { @Override public String getGreeting() { return "Hello"; } } package com.example.helloworld; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; public class Main implements BundleActivator { private ServiceReference<HelloWorld> serviceReference; @Override public void start(BundleContext context) throws Exception { serviceReference = context.getServiceReference(HelloWorld.class); HelloWorld helloWorld = context.getService(serviceReference); helloWorld.sayHello(); } @Override public void stop(BundleContext context) throws Exception { context.ungetService(serviceReference); } }


上一篇:
下一篇:
切换中文