原文:http://bill-xing.iteye.com/blog/793517
springdm是spring和osgi之间的桥梁,利用这个桥梁在osgi的bundle中,可以方便的利用spring的IOC、AOP、配置等特征来管理bundle中的对象,在Struts2的发布包中,自带了一个struts2-osgi-demo-plugin的例子工程,其中就是一个利用springdm管理osgi struts2 bundle的实现。
这个例子配置运行的步骤如下:
1、建立一个普通的web工程,在web的配置文件web.xml中加入对于struts2、springdm、osgi的支持,通过context-param的配置,制定在工程中,spring的加载类是OSGi的实现:org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext,也指定了bundle中的spring配置文件所在的位置/META-INF/spring目录。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>webapp</display-name>
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <filter>
- <filter-name>struts2-prepare</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
- </filter>
- <filter>
- <filter-name>struts2-execute</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2-prepare</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2-execute</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <listener>
- <listener-class>org.apache.struts2.osgi.StrutsOsgiListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextClass</param-name>
- <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
- </context-param>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>osgibundle:/META-INF/spring/*.xml</param-value>
- </context-param>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
2、把Struts的core、osgi、felix实现jar,放到web的lib目录下,实现web工程自动加载felix引擎到系统的运行环境中,并在struts.xml配置文件中设置两个常量:
- <constant name="struts.objectFactory" value="osgi" />
- <constant name="struts.objectFactory.delegate" value="springOsgi" />
3、在src目录下建立bundles目录,并建立以数字命名的子目录,子目录的名字代表在其中的bundles启动的基本,在这个例子中,需要放入有关springdm(2.0)的bundles、spring(3.0.0)和log实现及其他一些必须的bundles,如下图所示:
然后再建立一个3的子目录,把struts2-osgi-demo-bundle-2.1.8.1.jar放入其中,在application Server上启动web工程,进行访问,可以获得这样的界面:
就表示这个demo运行成功了,在demo中有spring和struts2的配置,其中体现了springdm的作用。