Java karaf在组件工厂示例中未显示任何错误

Java karaf在组件工厂示例中未显示任何错误,java,osgi,bundle,osgi-bundle,karaf,Java,Osgi,Bundle,Osgi Bundle,Karaf,我已经尝试了这个组件工厂的例子。 接口: package com.java.examplefactoryservice; public interface ExampleFactoryService { public void start(); public void stop(); } 工厂供应商: package com.java.examplecomponentfactoryserviceprovider; import java.util.Map; import

我已经尝试了这个组件工厂的例子。 接口:

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<String, Object> properties) {
        System.out.println("Actiavted!!!");
    }

    @Override
    public void start() {
        System.out.println("Started !!!!");
    }

    @Override
    public void stop() {
        System.out.println("Stopped!!!");
    }

}
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

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ExampleComponentFatoryManager</groupId>
    <artifactId>ExampleComponentFatoryManager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>2.3.5</version>
                <configuration>
                    <instructions>
                        <Import-Package>
                            *,
                            javax.servlet*;version="[2.5,4)"
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <version>1.14.0</version>
                <executions>
                    <execution>
                        <id>generate-scr-scrdescriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.compendium</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>ExampleFactoryService</groupId>
            <artifactId>ExampleFactoryService</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

4.0.0
示例ComponentFatoryManager
示例ComponentFatoryManager
0.0.1-快照
src
org.apache.maven.plugins
maven编译器插件
2.3.2
1.6
1.6
org.apache.felix
maven捆绑插件
真的
2.3.5
*,
servlet*;version=“[2.5,4)”
org.apache.felix
maven scr插件
1.14.0
生成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
假如
示例工厂服务
示例工厂服务
0.0.1-快照
起初,我尝试在karaf命令行中安装一个包而不是名称,它显示包的绝对路径。过了一段时间,我尝试将包放入deploy,它显示包的确切名称。
我不理解karaf的行为,为什么它在执行正确的命令行安装时没有显示捆绑包名称。而且它根本没有显示任何错误[dependency/compilation/wiredexecption]。有人能告诉我捆绑包中有什么问题吗?

您可以改为导入aqte.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<String, Object> 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");
    }
}
@Component(name=“ExampleComponentFactoryManager”,立即=真,启用=真)
公共类示例ComponentFactoryManager
{
私有静态最终记录器LOG=LoggerFactory.getLogger(ExampleComponentFactoryManager.class);
私营零部件厂;
//@Reference(target=“(component.factory=example.factory.provider)”,unbind=“unsetFactory”)
//私营零部件厂;
私有组件实例;
私人工厂服务;
@激活
公共无效激活(地图属性){
日志信息(“激活工厂经理”);
instance=factory.newInstance(null);
服务=(ExampleFactoryService)实例。getInstance();
LOG.info(“来自工厂的服务实例”+service.toString());
}
@引用(target=“(component.factory=example.factory.provider)”,unbind=“unsetFactory”)
公共无效设置工厂(最终部件工厂){
日志信息(“设置工厂”);
这个工厂=工厂;
System.out.println(“调用设置工厂”);
}
公共工厂(零部件工厂){
this.factory=null;
System.out.println(“未设置工厂名”);
}
}

您可以改为导入aqte.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<String, Object> 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");
    }
}
@Component(name=“ExampleComponentFactoryManager”,立即=真,启用=真)
公共类示例ComponentFactoryManager
{
私有静态最终记录器LOG=LoggerFactory.getLogger(ExampleComponentFactoryManager.class);
私营零部件厂;
//@Reference(target=“(component.factory=example.factory.provider)”,unbind=“unsetFactory”)
//私营零部件厂;
私有组件实例;
私人工厂服务;
@激活
公共无效激活(地图属性){
日志信息(“激活工厂经理”);
instance=factory.newInstance(null);
服务=(ExampleFactoryService)实例。getInstance();
LOG.info(“来自工厂的服务实例”+service.toString());
}
@引用(target=“(component.factory=example.factory.provider)”,unbind=“unsetFactory”)
公共无效设置工厂(最终部件工厂){
日志信息(“设置工厂”);
这个工厂=工厂;
System.out.println(“调用设置工厂”);
}
公共工厂(零部件工厂){
this.factory=null;
System.out.println(“未设置工厂名”);
}
}

是否是
scr:list
的全部输出?
ExampleComponentFactoryManager
丢失是。我只安装了一个捆绑包。ExampleComponentFactoryManager未显示在scr:list中。@Neil,至少我希望SOP会打印出来。但是没有打印任何内容。另外,如果我通过cmd行捆绑包名称安装它,则没有显示的是绝对路径。我不了解Karaf的行为。这是Karaf中的性能问题还是我做错了什么?我认为捆绑包的构造中存在错误,因为
ExampleComponentFactoryManager
根本没有出现。但是我只能猜测,因为只有Java源代码已发布。
ExampleComponentFactoryManager
丢失是的。我只安装了一个包。ExampleComponentFactoryManager未显示放在scr:list中。@Neil,至少我希望SOP会被打印出来。但是没有什么是优先的
@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<String, Object> 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");
    }
}