转载请注明出处 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,将其中dist和lib中的jar包都copy到eclipse的plugins目录中。重启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是代码编译中需要用到的class的package,包括import的class、及其父类、成员变量的类等。在运行时,需要勾选相应package的Bundle。
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中的service,WEB-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 mapping,service注入。可特别注意下,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。
勾选timeservice和helloworldweb,引入相关的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/