关于基于SpringDM的WEB应用开发
来源:优易学  2011-12-26 13:19:52   【优易学:中国教育考试门户网】   资料下载   IT书店

  大致结构如下:
  META-INF
  MANIFEST.MF
  WEB-INF
  Classes
  Lib
  Web.xml
  applicationContext.xml
  1 MANIFEST.MF配置参考:
  Manifest-Version: 1.0
  Bundle-ManifestVersion: 2
  Bundle-Name: Osgi_web_app Plug-in
  Bundle-SymbolicName: osgi_web_app
  Bundle-Version: 1.0.0
  Bundle-Vendor: ccsoft
  Import-Package: javax.servlet,
  javax.servlet.http,
  javax.servlet.resources;version="2.4.0",
  org.ccsoft.service,
  org.springframework.osgi.web.context.support;version="1.1.2",
  org.springframework.web.context,
  org.springframework.web.context.support
  Bundle-ClassPath: WEB-INF/classes/,
  .
  Require-Bundle: org.springframework.bundle.osgi.core,
  org.springframework.bundle.osgi.io,
  org.springframework.bundle.spring.beans,
  org.springframework.bundle.spring.context,
  org.springframework.bundle.spring.core
  2 为了在web应用中使用spring dm的IoC功能,web.xml中需要加入一些特定配置,类似于使用Spring时的配置,web.xml配置参考如下:
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <web-app 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"
  version="2.4">
  <display-name>Simple Osgi WebApp Bundle</display-name>
  <description>Simple OSGi War</description>
  <context-param>
  <param-name>contextClass</param-name>
  <param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>org.ccsoft.web.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/servlet</url-pattern>
  </servlet-mapping>
  </web-app>
  至于applicationContext.xml则是标准的spring dm配置文件形式,只是没有放在我们所熟悉的位置(META-INF/spring)
  配置示例:
  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:osgi="http://www.springframework.org/schema/osgi"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  <osgi:reference id="HelloServiceOsgi" interface="org.ccsoft.service.SpeakService"/>
  </beans>
  在你的WEB应用中可以使用如下代码来访问别的Bundle提供的服务:
  WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
  SpeakService ss=(SpeakService)ctx.getBean("HelloServiceOsgi");
  与你使用Spring开发WEB应用的写法是完全一致的。
  好了现在你可以利用spring dm开发你的web应用了。

上一页  [1] [2] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训