【问题标题】:use of javax.servlet package in maven-bundle-plugin在 maven-bundle-plugin 中使用 javax.servlet 包
【发布时间】:2015-10-21 09:48:17
【问题描述】:

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

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>

发生类转换异常

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 的方法,这又会打印一些字符串!。 在 karaf 中部署时也会调用两次在提供程序中激活。对此有任何猜测。 我已经在更大的版本中验证了相同的(激活调用了两次)Apache karaf-2.3.11 工作正常并且只调用了一次。这是 Apache Karaf 2.3.10 中的问题吗?有人可以确认一下吗

【问题讨论】:

  • 只有 * 有效吗?
  • @BJ 我的问题是如果我不使用它,为什么我需要导入 javax.servlet 包?
  • 我的问题是您删除了整个 Import-Package 元素,因为它有 2 个部分:* 和 servlet。如果您只是删除了 servlet 部分怎么办?
  • 如果删除了整个导入包,我会收到 classcastexception。我没有尝试只删除 servlet 部分。
  • 也许您应该尝试只删除 servlet 部分。

标签: java maven servlets osgi apache-karaf


【解决方案1】:

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

【讨论】:

  • 我确信我没有使用除 osgi.core、scr 注释之外的任何库。还粘贴了代码。
  • @Chirstian Classcastexception 由于与 org.apache.felix.scr (1.8.2) & org.apache.felix.scr (1.6.0) 的版本冲突而发生。
  • 你应该只安装一个版本的 scr。
  • 对我发布的与组件工厂相关的问题有任何想法stackoverflow.com/questions/33603037/…
猜你喜欢
  • 2013-08-19
  • 2013-11-15
  • 2010-11-25
  • 2011-06-18
  • 1970-01-01
  • 2016-07-20
  • 2012-09-08
  • 2019-08-11
  • 2015-02-20
相关资源
最近更新 更多