【问题标题】:what is the purpose of @Component in OSGI factory implementations?OSGI 工厂实现中@Component 的目的是什么?
【发布时间】:2015-12-09 05:39:22
【问题描述】:

我正在尝试使用 apache felix scr 注释来实现 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>

【问题讨论】:

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


    【解决方案1】:

    是的,这是强制性的。它根据声明式服务规范将您的类声明为组件。如果没有 @Component 注释,它只是你的包中的一些类。

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

    【讨论】:

    • 我正在尝试使用 ds 公开一个服务,这需要组件注释,否则它会失败并且服务处于不满意状态。您能否请教为什么当我只使用一项服务时需要组件注释?另外,能否请您提供上述解释的指针。
    • 我的回答确实解释了为什么需要@Component。你所写的既是一个服务一个组件。
    • 我的问题与上述问题无关。这只是我正在实施的服务(使用 ds)。它需要组件注释。没有注释,服务就会变得不满意。
    • 那是因为@Component注解是必需的。我不知道有更多的方式来表达同样的事情。
    • 您好,请问@component 注解中工厂属性的用途是什么。
    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2016-06-16
    • 2018-08-13
    • 2021-09-09
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多