【发布时间】: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