Preparation

  1. Download and install maven2.
  2. Download spring DM 1.1.2.
  3. Install spring DM archetype for maven.
    • Open a cmd window in spring-osgi-1.1.2
    • Run command: mvn -P equinox clean install. This step is used to install spring OSGI archetype.
  4. Create spring DM project using maven: 
    mvn archetype:create -DarchetypeGroupId=org.springframework.osgi -DarchetypeArtifactId=spring-osgi-bundle-archetype -DarchetypeVersion=1.1.2 -DgroupId=org.foo -DartifactId=org.foo.my-springdm-bundle -Dversion=0.1
  5. Add a class HelloService as following:
    package org.foo;
    
    publicclass HelloService {
        publicvoid init() {
            System.out.println("start");
        }
    }
  6. Add bean declaration for HelloService to bundle-context.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
      <!-- regular spring configuration file defining the beans for this
           bundle. The configuration of OSGi definitions is kept in a separate 
           configuration file so that this file can easily be used
           for integration testing outside of an OSGi environment -->
        <bean id="helloService" class="org.foo.HelloService" init-method="init"/>
    </beans>
  7. Edit pom.xml and modify imported package for the bundle:
    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <version>1.4.0</version>
        <configuration>
            <manifestLocation>META-INF</manifestLocation>
            <instructions>
            <Export-Package>
                !org.foo.internal,org.foo*
            </Export-Package>
            <Import-Package>org.osgi.framework</Import-Package>
            <Include-Resource>src/main/resources</Include-Resource>
            </instructions>
        </configuration>
    </plugin
  8. Create MANIFEST.MF and bundle using command: mvn package
  9. If you want to run it in eclipse, please run mvn eclipse:eclipse to generate a eclipse plugin project and import it to your workspace, then run it as OSGI framework. Please refer to this article:http://www.javaworld.com/javaworld/jw-04-2008/jw-04-osgi2.html?page=1
  10. Start a OSGI container: java -jar org.osgi.framework.jar -console
  11. Install the necessary bundle to OSGI container using the following commands in osgi console:
    install file:/path/to/the/bundle
    start file:/path/to/the/bundle
  12. You can find the bundles under spring-osgi-1.1.2/dist or libs. You need to install and active the following bundles:
    • org.springframework.bundle.osgi.core_1.1.2.jar
    • org.springframework.bundle.osgi.extender_1.1.2.jar
    • org.springframework.bundle.osgi.io_1.1.2.jar
    • org.springframework.bundle.spring.core_2.5.5.jar
    • org.springframework.bundle.spring.context_2.5.5.jar
    • org.springframework.bundle.spring.beans_2.5.5.jar
    • org.springframework.bundle.spring.aop_2.5.5.jar
    • org.springframework.osgi.log4j.osgi_1.2.15.SNAPSHOT.jar
    • om.springsource.org.aopalliance_1.0.0.jar
    • com.springsource.slf4j.org.apache.commons.logging_1.5.0.jar
    • com.springsource.slf4j.api_1.5.0.jar
    • com.springsource.slf4j.log4j_1.5.0.jar
  13. Install and active your bundle under target folder using the same commands and you will see the output.
  14. Use "ss" in OSGI console to list the installed bundles.