简体   繁体   English

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

[英]use of javax.servlet package in maven-bundle-plugin

I am trying an example which uses component factory and everything works fine. 我正在尝试一个使用组件工厂的示例,并且一切正常。 My project structure is 我的项目结构是

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

below is my pom.xml 下面是我的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>

If i remove the import statement 如果我删除导入语句

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

classcastexception occurs classcastexception发生

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

I am writing a simple application and not using servlet class. 我正在编写一个简单的应用程序,而不使用servlet类。 Just a class with println statements. 只是一个带有println语句的类。 Can any one please tell me why we are importing servlet package here.? 谁能告诉我为什么我们要在这里导入servlet包? I know there is a link related to this but there is no clear explanation. 我知道有与此相关的链接 ,但没有明确的解释。

factory provider 工厂供应商

@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");
    }

bundle 3 套装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;
    }

Apart from the above two classes remaining things are interface and implementation. 除了以上两个类,其余的都是接口和实现。 Just contains method with names start and stop , which inturn will print some strings!. 仅包含名称为start和stop的方法,该方法将打印一些字符串! Also activate in provider is called two times while deploying in karaf. 在karaf中部署时,还两次在提供者中激活。 Any guess on this. 任何对此的猜测。 I have verified the same(activate called two times) in greater versions Apache karaf-2.3.11 is working fine and called only once. 我已经在更高版本中验证了相同的(激活两次),Apache karaf-2.3.11可以正常工作并且仅被调用一次。 Is this an issue in Apache Karaf 2.3.10. 这是Apache Karaf 2.3.10中的问题吗? Could anybody please confirm the same 任何人都可以确认相同吗

Most probably either your code or some library you use needs the servlet package. 您的代码或使用的某些库很可能都需要servlet包。 Difficult to say without your code. 没有您的代码很难说。 The maven bundle plugin scans the classes and only imports what is needed. Maven软件包插件扫描类,仅导入所需的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM