[英]use of javax.servlet package in maven-bundle-plugin
我正在嘗試一個使用組件工廠的示例,並且一切正常。 我的項目結構是
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包? 我知道有與此相關的鏈接 ,但沒有明確的解釋。
工廠供應商
@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中的問題嗎? 任何人都可以確認相同嗎
您的代碼或使用的某些庫很可能都需要servlet包。 沒有您的代碼很難說。 Maven軟件包插件掃描類,僅導入所需的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.