我正在尝试一个使用组件工厂的示例,一切工作正常。我的项目结构是
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语句
<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的方法,该方法将打印一些字符串!在karaf中部署时,在提供程序中激活也会被调用两次。任何关于这个的猜测。我已经在更高版本中验证了同样的情况(调用了两次激活),Apache karaf-2.3.11运行良好,并且只调用了一次。这是Apache Karaf 2.3.10中的一个问题吗?有人能证实这一点吗?
发布于 2015-10-21 18:30:48
很可能是您的代码或您使用的某个库需要servlet包。没有你的代码就很难说。maven bundle插件扫描类并只导入所需的内容。
https://stackoverflow.com/questions/33256008
复制相似问题