随笔-7  评论-3  文章-0  trackbacks-0

转载请注明出处 http://www.blogjava.net/fireflyk/

 

接上文,[OSGi] OSGi + Spring + Web Demo [1]

1.       同样方法创建helloworldweb Bundle,用Maven方式创建并转为PDE Tools

2.       引入Spring OSGi 1.2.1

下载Spring OSGi 1.2.1,将其中distlib中的jar包都copyeclipseplugins目录中。重启eclipse

3.       HelloWorldController.java

public class HelloWorldController extends MultiActionController {

   

    private TimeService timeService;

 

    public ModelAndView echoTime(HttpServletRequest request,

           HttpServletResponse response) throws Exception {

       System.out.println("echoTime");

       ModelAndView mv = new ModelAndView("result");

       mv.addObject("result", timeService.getCurrentTime());

       return mv;

    }

 

    public TimeService getTimeService() {

       System.out.println("getTimeService");

       return timeService;

    }

 

    public void setTimeService(TimeService timeService) {

       System.out.println("setTimeService");

       this.timeService = timeService;

    }

}

4.       MANIFEST.MF。个人理解,import-package是代码编译中需要用到的classpackage,包括importclass、及其父类、成员变量的类等。在运行时,需要勾选相应packageBundle

Import-Package: javax.servlet.http;version="2.5.0",

 javax.servlet.jsp.jstl.core;version="1.1.2",

 org.apache.jasper.servlet;version="5.5.17",

 org.apache.taglibs.standard.tag.common.fmt;version="1.1.2",

 org.osgichina.demo.timeservice,

 org.springframework.beans;version="2.5.6.SEC01",

 org.springframework.context.support;version="2.5.6.SEC01",

 org.springframework.core;version="2.5.6.SEC01",

 org.springframework.osgi.web.context.support;version="1.2.1",

 org.springframework.web.context;version="2.5.6.SEC01",

 org.springframework.web.context.support;version="2.5.6.SEC01",

 org.springframework.web.servlet;version="2.5.6.SEC01",

 org.springframework.web.servlet.handler;version="2.5.6.SEC01",

 org.springframework.web.servlet.mvc;version="2.5.6.SEC01",

 org.springframework.web.servlet.mvc.multiaction;version="2.5.6.SEC01",

 org.springframework.web.servlet.mvc.support;version="2.5.6.SEC01",

 org.springframework.web.servlet.view;version="2.5.6.SEC01"

 

 

5.       引入timeservice Bundle中的serviceWEB-INF/applicationContext.xml。引入的OSGi service,注入方式和普通service是一样的,见WEB-INF/spring-servlet.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" 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

                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

   

    <!-- 引入OSGi Service -->

    <osgi:reference id="osgiTimeService" interface="org.osgichina.demo.timeservice.TimeService" />

   

</beans>

 

6.       定义web.xml,配置OSGi Web,配置Spring Framework

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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">

   

    <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>spring</servlet-name>

       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

       <init-param>

           <param-name>contextClass</param-name>

           <param-value>

              org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

           </param-value>

       </init-param>

       <load-on-startup>2</load-on-startup>

    </servlet>

    <servlet-mapping>

       <servlet-name>spring</servlet-name>

       <url-pattern>/spring/*</url-pattern>

    </servlet-mapping>

</web-app>

 

7.       WEB-INF/spring-servlet.xml。配置url mappingservice注入。可特别注意下,timeService的注入和普通service的注入是一样的。

<?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">

    <!-- page config -->

    <bean id="viewResolver"

       class="org.springframework.web.servlet.view.UrlBasedViewResolver">

       <property name="viewClass"

           value="org.springframework.web.servlet.view.JstlView" />

       <property name="prefix" value="/" />

       <property name="suffix" value=".jsp" />

    </bean>

   

    <!-- controller general config -->

    <bean

       class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">

       <property name="caseSensitive" value="true" />

       <property name="order" value="0" />

       <property name="pathPrefix" value="/" />

    </bean>

   

    <!-- controller general config -->

    <bean id="internalPathMethodNameResolver"

        class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />

 

    <!-- action to url mapping -->

    <bean id="urlMapping"

       class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

       <property name="mappings">

           <props>

              <prop key="/helloWorld/*">helloWorldController</prop>

           </props>

       </property>

    </bean>

   

    <!-- Bean Definition -->

    <bean id="helloWorldController" class="org.osgichina.demo.web.HelloWorldController">

       <property name="methodNameResolver">

            <ref bean="internalPathMethodNameResolver" />

        </property>

       <property name="timeService" ref="osgiTimeService" />

    </bean>

   

</beans>

 

8.       启动Demo

Run Configurations -> OSGi Framework

勾选timeservicehelloworldweb,引入相关的Bundle,示意图如下,


Validate Bundle,可以看到还缺少哪些包。个人理解,这里应该包含import package中的Bundle和运行时调用的Bundle

启动后,可以看到Console中的输出日志。访问以下Url查看效果,

http://127.0.0.1:8080/helloworldweb/spring/helloWorld/echoTime

http://127.0.0.1:8080/helloworldweb/

 



专注于Java,数据库性能,Web Server负载,数据挖掘,机器学习等方向
posted on 2011-10-09 13:12 柳桐 阅读(3377) 评论(2)  编辑  收藏 所属分类: Java

评论:
# re: [OSGi] OSGi + Spring + Web Demo [2][未登录] 2012-09-04 14:50 | 啊啊
spring osgi的包里没有org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext这个类啊  回复  更多评论
  
# re: [OSGi] OSGi + Spring + Web Demo [2] 2012-09-04 23:20 | 柳桐
@啊啊
版本对吗?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: