1

I am trying to implement servicefactory using apache felix scr annotations.

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

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

Above code is working fine. But what is the purpose of @Component? Because i am trying to expose it as a service instead of both Component & Service. If i remove the @Component state is unsatisfied. IS it really mandatory for a factory to use both component and service? 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>
Shriram
  • 4,343
  • 8
  • 37
  • 64

1 Answers1

2

Yes this is mandatory. It declares your class as a Component according to the Declarative Services specifcation. Without the @Component annotation, it is merely some class hanging around in your bundle.

Components may also be published as services, as in this example. But components do not have to be services -- instead they might expose some kind of external interface like a server socket or a GUI.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • I am trying to expose a service,using ds, which requires Component annotation, otherwise it fails and service is unsatisfied state. Could you please why component annotation is required when i am going with simply a service?. Also could you please provide me the pointer for the above explanation . – Shriram Dec 31 '15 at 06:46
  • My answer did explain why @Component is required. What you are writing is both a service *and* a component. – Neil Bartlett Dec 31 '15 at 09:46
  • My question was not related to the above one. It's just service I am implementing(with ds). There it expects component annotation. Without the annotation the the service becomes unsatisfied. – Shriram Jan 01 '16 at 08:28
  • That's because the @Component annotation is required. I don't know any more ways to state the same thing. – Neil Bartlett Jan 02 '16 at 13:49
  • Hi, may i know what is the use of factory attribute in @component annotation. – vijaya kumar Oct 21 '16 at 06:59