@Component在OSGI工厂实现中的目的是什么?

时间:2015-12-09 05:39:22

标签: java maven osgi apache-karaf service-factory

我正在尝试使用apache felix scr annotations实现servicefactory。

@Component
@Service(serviceFactory = true)
@Properties(value = { @Property(name = "className", value = "interface1") })
public class Tinterfaceimpl1 implements Tinterface {

    @Override
    public void consumeService() {
        System.out.println("tinterfaceimpl1");
    }
}

以上代码工作正常。但@Component的目的是什么?因为我试图将其作为服务而不是组件和&服务。如果我删除@Component状态不满意。工厂是否真的必须同时使用组件和服务? 的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>TinterfaceImpl</groupId>
    <artifactId>TinterfaceImpl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>2.3.5</version>
                <!-- <configuration>
                    <instructions>
                        <Import-Package>com.java.serviceeg.tinterface.Tinterface</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>
            <!-- Felix SCR annotations -->
            <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.apache.felix.scr</artifactId>
                <version>1.6.0</version>
            </dependency>
            <dependency>
                <groupId>Tinterface</groupId>
                <artifactId>Tinterface</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <type>bundle</type>
            </dependency>
        </dependencies>
        <packaging>bundle</packaging>

</project>

1 个答案:

答案 0 :(得分:1)

是的,这是强制性的。它根据Declarative Services规范将您的类声明为Component。如果没有@Component注释,它只是在你的包中闲置的一些类。

组件也可以作为服务发布,如本例所示。但是组件 不是服务 - 而是它们可能会暴露某种外部接口,如服务器套接字或GUI。