public interface GreetingService { String greet(String name); } public class EnglishGreetingService implements GreetingService { @Override public String greet(String name) { return "Hello, " + name + "!"; } } public class ChineseGreetingService implements GreetingService { @Override public String greet(String name) { } } <component name="EnglishGreetingService" class="com.example.EnglishGreetingService"> <property name="language" value="en"/> </component> <component name="ChineseGreetingService" class="com.example.ChineseGreetingService"> <property name="language" value="zh"/> </component> import org.osgi.framework.BundleContext; public class GreetingClient { private BundleContext bundleContext; public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } public void greet() { ServiceReference<GreetingService> serviceReference = bundleContext.getServiceReference(GreetingService.class); if (serviceReference != null) { GreetingService greetingService = bundleContext.getService(serviceReference); String greeting = greetingService.greet("Alice"); System.out.println(greeting); } } }


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