基于Maven创建:
1. mvn archetype:create -DgroupId=com.manning.sdmia -DartifactId=SpringDM-Sample
mvn eclipse:eclipse
2. Package:Felix Bundle Plugin
pom.xml:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.4</version>
<extensions>true</extensions>
</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>
</plugins>
</build>
执行命令:mvn package
3. 部署安装:
install file:///D:/sts_workspace/SpringDM-Sample/target/SpringDM-Sample-1.0.0.jar
4. 测试代码开发
The Spring DM integration test framework provides a handy abstract
class(AbstractConfigurableBundleCreatorTests) that provides hooks for the OSGi container
configuration, and this test uses it as its parent class。
例如:
public class SpringDmSampleTest extends AbstractConfigurableBundleCreatorTests {
public void testIntergration() {
boolean bundleIsHereAndStarted = false;
for (Bundle bundle : bundleContext.getBundles()) {
if ("com.manning.sdmia.SpringDM-Sample".equals(bundle.getSymbolicName()) &&
bundle.getState() == Bundle.ACTIVE) {
bundleIsHereAndStarted = true;
break;
}
}
assertTrue("SpringDM-Sample is not installed or activated!",bundleIsHereAndStarted);
}
/*
* SpringDM会为测试提供OSGi容器及依赖的JAR,若要添加扩展的JAR则需要复写该方法;
* 同时若希望被扩展的Bundle能被安装并启动,还需要提供Maven标示符:grouoId,
* artifactId,version。
*/
@Override
protected String[] getTestBundlesNames() {
return new String[]{"com.manning.sdmia,SpringDM-Sample,1.0.0"};
}
/*
* 若希望在测试周期中SpringDM测试框架能自动安装客制化的Bundle,需要复写该方法;
* 需指定一个地址路径指定所有希望的测试的周期中能被安装在OSGi容器中的
*/
@Override
protected Resource getTestingFrameworkBundlesConfiguration() {
return new InputStreamResource(SpringDmSampleTest.class.getResourceAsStream("boot-
bundles.properties"));
}
}
其中文件boot-bundles.properties:
# versioning
ignore.junit.version=4.8.1
ignore.log4j.version=1.2.15
ignore.spring.version=3.0.5.RELEASE
ignore.spring.osgi.version=2.0.0.M1
ignore.slf4j.version=1.6.1
ignore.asm.version=2.2.3
ignore.aopalliance.version=1.0.0
# groupIds
ignore.spring.groupId=org.springframework
ignore.spring.osgi.groupId=org.springframework.osgi
ignore.slf4j.groupId=org.slf4j
# junit
org.junit,com.springsource.org.junit,${ignore.junit.version}=
# log4j
org.apache.log4j,com.springsource.org.apache.log4j,${ignore.log4j.version}=
# slf4j (BRITS)
${ignore.slf4j.groupId},com.springsource.slf4j.api,${ignore.slf4j.version}=
${ignore.slf4j.groupId},com.springsource.slf4j.log4j,${ignore.slf4j.version}=
${ignore.slf4j.groupId},com.springsource.slf4j.org.apache.commons.logging,${ignore.slf4j.version}=
# aop alliance
org.aopalliance,com.springsource.org.aopalliance,${ignore.aopalliance.version}=
# asm
org.objectweb.asm,com.springsource.org.objectweb.asm,${ignore.asm.version}=
# spring libs
${ignore.spring.groupId},org.springframework.asm,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.beans,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.core,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.context,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.expression,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.aop,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.test,${ignore.spring.version}=
# spring osgi libs
${ignore.spring.osgi.groupId},spring-osgi-io,${ignore.spring.osgi.version}=
${ignore.spring.osgi.groupId},spring-osgi-core,${ignore.spring.osgi.version}=
${ignore.spring.osgi.groupId},spring-osgi-annotation,${ignore.spring.osgi.version}=+15
${ignore.spring.osgi.groupId},spring-osgi-extender,${ignore.spring.osgi.version}=
${ignore.spring.osgi.groupId},spring-osgi-test,${ignore.spring.osgi.version}=
5. 启动容器
java -jar org.eclipse.osgi-3.5.1.R35x_v20090827.jar -console
6. 案例代码
见附件:SpringDM-Sample.rar