karaf在组件工厂示例时不显示任何错误

我从这个链接尝试过组件工厂示例。 接口:

package com.java.examplefactoryservice; public interface ExampleFactoryService { public void start(); public void stop(); } 

工厂提供者:

 package com.java.examplecomponentfactoryserviceprovider; import java.util.Map; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import com.java.examplefactoryservice.ExampleFactoryService; @Component(name = "ExampleComponentFactoryServiceProvider", factory = "example.factory.provider") public class ExampleComponentFactoryServiceProvider implements ExampleFactoryService { @Activate public void activate(Map properties) { System.out.println("Actiavted!!!"); } @Override public void start() { System.out.println("Started !!!!"); } @Override public void stop() { System.out.println("Stopped!!!"); } } 

厂长:

 package com.java.examplecomponentfatorymanager; import java.util.Map; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Reference; import org.osgi.service.component.ComponentFactory; import org.osgi.service.component.ComponentInstance; import com.java.examplefactoryservice.ExampleFactoryService; @Component(name = "ExampleComponentFactoryManager", enabled = true, immediate = true) public class ExampleComponentFactoryManager { @Reference(target = "(component.factory=example.factory.provider)", bind = "setFactory", unbind = "unsetFactory") private ComponentFactory factory; private ComponentInstance instance; private ExampleFactoryService service; @Activate public void activate(Map properties) { System.out.println("activate in manager !!!!"); instance = factory.newInstance(null); service = (ExampleFactoryService) instance.getInstance(); System.out.println("service = " + service.toString()); } public void setFactory(final ComponentFactory factory) { this.factory = factory; System.out.println("setfactory called"); } public void unsetFactory(ComponentFactory factory) { this.factory = null; System.out.println("Unset factory called"); } } 

编译好的一切都安装在karaf 2.3.10中。 工厂注册成功。 但没有显示任何内容。

卡拉夫输出:

 [ 71] [Active ] [ ] [ 80] osgi.cmpn (4.3.1.201210102024) [ 79] [Active ] [ ] [ 80] testI (0.0.1.SNAPSHOT) [ 80] [Active ] [ ] [ 80] testImpl (0.0.3.SNAPSHOT) [ 110] [Active ] [ ] [ 80] Apache Felix Declarative Services (1.6.0) [ 140] [Active ] [ ] [ 80] ExampleFactoryService (0.0.1.SNAPSHOT) [ 152] [Active ] [ ] [ 80] ExampleComponentFactoryServiceProvider (0.0.1.SNAPSHOT) [ 158] [Active ] [ ] [ 80] ExampleComponentFatoryManager (0.0.1.SNAPSHOT) 

scr:列表输出

 [9 ] [FACTORY ] ExampleComponentFactoryServiceProvider 

SCR:细节

 scr:deactivate scr:details karaf@root> scr:details ExampleComponentFactoryServiceProvider Component Details Name : ExampleComponentFactoryServiceProvider State : FACTORY Properties : service.vendor=The Apache Software Foundation component.factory=example.factory.provider component.name=ExampleComponentFactoryServiceProvider References 

的pom.xml

  4.0.0 ExampleComponentFatoryManager ExampleComponentFatoryManager 0.0.1-SNAPSHOT  src   org.apache.maven.plugins maven-compiler-plugin 2.3.2  1.6 1.6    org.apache.felix maven-bundle-plugin true 2.3.5    *, javax.servlet*;version="[2.5,4)"      org.apache.felix maven-scr-plugin 1.14.0   generate-scr-scrdescriptor  scr         org.apache.felix org.apache.felix.scr.annotations 1.9.6   org.apache.felix org.osgi.compendium 1.4.0   org.osgi org.osgi.core 4.3.1 provided   ExampleFactoryService ExampleFactoryService 0.0.1-SNAPSHOT    

最初我尝试在karaf命令行中安装捆绑而不是名称,它显示捆绑的绝对路径。 一段时间后,我尝试将bundle放入deploy中,它显示了bundle的确切名称。 我不理解karaf为什么在进行正确的命令行安装时没有显示包名称的行为。 它根本没有显示任何错误[dependency / compilation / wiredexecption]。 任何人都可以告诉我包中有什么问题吗?

你可以去导入aQute.bnd.annotation,而不是为karaf导入org.apache.felix.scr.annotations。

参考下面的代码

 @Component(name="ExampleComponentFactoryManager",immediate=true,enabled=true) public class ExampleComponentFactoryManager { private static final Logger LOG = LoggerFactory.getLogger(ExampleComponentFactoryManager.class); private ComponentFactory factory; // @Reference(target = "(component.factory=example.factory.provider)",unbind = "unsetFactory") //private ComponentFactory factory; private ComponentInstance instance; private ExampleFactoryService service; @Activate public void activate(Map properties) { LOG.info("activation factorymanager"); instance=factory.newInstance(null); service =(ExampleFactoryService)instance.getInstance(); LOG.info("service instance from factory "+service.toString()); } @Reference(target = "(component.factory=example.factory.provider)",unbind = "unsetFactory") public void setFactory(final ComponentFactory factory) { LOG.info("set Factory"); this.factory = factory; System.out.println("setfactory called"); } public void unsetFactory(ComponentFactory factory) { this.factory = null; System.out.println("Unset factory called"); } }