我想使用其中一个示例to launch a FrameworkFactory。我的编译器都拒绝编译......
Map<String,String> props=new HashMap<String,String>();
props.put("org.osgi.framework.storage","target/osgi-store");
props.put("org.osgi.framework.storage.clean","onFirstInit");
FrameworkFactory factory=ServiceLoader.load(FrameworkFactory.class);
// Type mismatch: cannot convert from ServiceLoader<FrameworkFactory> to FrameworkFactory
或者vm无法加载工厂......
ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class);
Iterator<FrameworkFactory> iter = loader.iterator();
FrameworkFactory ff = iter.next(); // there is no "next"
这是我的pom
<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>prices.otemba.org</groupId>
<artifactId>PricesFromOtemba</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe</executable>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.runtime</artifactId>
<version>0.16.2</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
如果您对我的pom有一个建议,请随时提供建议。
答案 0 :(得分:1)
以下条目:
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
将OSGi 4.3 API添加到您的项目中(请注意,泛型工作需要4.3.1或更高版本)。这为您提供了FrameworkFactory
接口,但没有要查找的服务加载程序的实现。要获得实现,您需要在运行时在类路径上拥有OSGi R4.3(或更高版本)框架。
例如,要添加Apache Felix框架R6实现(与R4.3 API向后兼容),您需要添加:
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.0.1</version>
<!-- You aren't compiling against Felix, just using it at runtime -->
<scope>runtime</scope>
</dependency>
我还建议删除以下内容:
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.4.0</version>
</dependency>
你应该删除它的原因是它是OSGi API jar的旧版本。它不包含FrameworkFactory接口(它早于它),它将与编译路径上的OSGi 4.3 API冲突。
答案 1 :(得分:0)
所以我的问题是关于java.util.ServiceLoader
documentation州:&#34;它会在您的应用程序的类路径&#34;中搜索服务提供商。我构建了这些例子并对该陈述产生了强烈怀疑。这些示例需要在.jar中与META-INF中的子目录进行汇编,并具有服务的完全限定名称。
在这种情况下,它将是实现org.osgi.framework.launch.FrameworkFactory的类的完全限定名称。
所以我检查了我的M2存储库,如果包org.osgi.core有这样的条目。答案是否定的。
结论:我使用的示例不适用于没有为java.util.ServiceLoader准备的包