在maven-bundle-plugin中使用javax.servlet包

时间:2015-10-21 09:48:17

标签: java maven servlets osgi apache-karaf

我正在尝试一个使用组件工厂的例子,一切正常。我的项目结构是

Bundle1 
   -- interface
Bundle2
   -- implemenation
Bundl3
   -- factory to produce objects 
Bundle4
   -- factoryprovider.

下面是我的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>com.test.java.cintconsumer.CIntConsumer</groupId>
    <artifactId>com.test.java.cintconsumer.CIntConsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <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>
            <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>
                        <Bundle-SymbolicName>com.test.java.cintconsumer.CIntConsumer</Bundle-SymbolicName>
                        <Import-Package>
                            *,
                            javax.servlet*;version="[2.5,4)"
                        </Import-Package>
                    </instructions>
                </configuration>
            </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.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.java.test.cinterface.CInterface</groupId>
            <artifactId>com.java.test.cinterface.CInterface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <packaging>bundle</packaging>
</project>

如果我删除了导入语句

 <Import-Package>*,javax.servlet*;version="[2.5,4)"
    </Import-Package>

发生classcastexception

java.lang.ClassCastException: org.apache.felix.scr.impl.manager.ComponentFactoryImpl cannot be cast to org.osgi.service.component.ComponentFactory

我正在编写一个简单的应用程序,而不是使用servlet类。只是一个带有println语句的类。任何人都可以告诉我为什么我们在这里导入servlet包。我知道有一个link与此相关,但没有明确的解释。

工厂提供商

@Activate
    public void activate(BundleContext context) throws InvalidSyntaxException {
        serRef = context.getAllServiceReferences(null, "(component.factory=com.test.java.cintconsumer.CIntClient)")[0];
        componentFactory = (ComponentFactory) context.getService(serRef);

        ComponentInstance instance = componentFactory.newInstance(null);
        CIntClient client = (CIntClient) instance.getInstance();
        System.out.println("client " + client);
        client.getCInterface().start("New Component started");
        client.getCInterface().stop("New Component stopped");
    }

捆绑3

@Component(factory = "com.test.java.cintconsumer.CIntClient")
public class CIntClient {

    @Reference(bind = "bind", unbind = "unbind")
    private CInterface cinter;


    @Activate
    public void activate() {
    }

    public void bind(CInterface cinter) {
        this.cinter = cinter;
    }

    public void unbind(CInterface cinter) {
        this.cinter = null;
    }

    public CInterface getCInterface() {
        return cinter;
    }

除了以上两个类之外,其余的东西都是接口和实现。只包含名称start和stop的方法,其中inturn将打印一些字符串! 在karaf中部署时,还会在提供程序中激活两次。对此有任何猜测。 我在更高版本中验证了相同的(激活调用两次)Apache karaf-2.3.11工作正常,只调用一次。这是Apache Karaf 2.3.10中的一个问题。任何人都可以确认相同的

1 个答案:

答案 0 :(得分:0)

您使用的代码或某些库很可能需要servlet包。没有你的代码很难说。 maven bundle插件扫描类并仅导入所需的内容。