Hello Spring MVC and OSGi
8
Nov
Nov
0
源起
之前已經可以將物件利用 spring+hibernet 存放道資料庫,現要整合到一個網頁服務上, 所以需要練習一下 Spring MVC 的架構,並把 MVC 納入 haha.remix.spring 中。
預計將之前的 jpa 練習的 userdao 拿來注到 controller 之中,然後在網頁中簡單列出來。
Spring OSGi Specification (v0.7) 2.5 Web application support
結果
瀏覽器開到 http://127.0.0.1/foo/index.html 可以看到資料庫輸出。
osgi> ss Framework is launched. id State Bundle 0 ACTIVE system.bundle_3.3.0.v20061101 5 ACTIVE javax.servlet_2.4.0.200609281713 6 ACTIVE javax.servlet.jsp_2.0.0.200610251427 7 ACTIVE log4j_1.2.13 9 ACTIVE org.apache.commons.logging_1.0.4 10 ACTIVE org.eclipse.equinox.ds_1.0.0.v20060828 12 ACTIVE org.eclipse.equinox.log_1.0.100.v20060717 13 ACTIVE org.eclipse.osgi.services_3.1.100.v20060918 75 ACTIVE org.apache.commons.el_1.0.0 76 ACTIVE org.apache.jasper_5.5.17.200610251427 77 ACTIVE org.eclipse.equinox.http.jetty_1.0.0.v20061012 78 ACTIVE org.eclipse.equinox.http.servlet_1.0.0.v20061023 79 ACTIVE org.eclipse.equinox.jsp.jasper_1.0.0.200610251427 80 ACTIVE org.eclipse.equinox.jsp.jstl_1.0.0 81 ACTIVE org.mortbay.jetty_5.1.11.200609281713 85 ACTIVE haha.hello.jpa.userdao_1.0.0 87 ACTIVE haha.remix.derby_1.0.0 88 ACTIVE haha.remix.hibernate_1.0.0 94 ACTIVE haha.hello.mvc.userdao_1.0.0 95 ACTIVE haha.remix.spring_1.0.0
haha.hello.mvc.userdao
首先要先找到 HttpService 來用。
META-INF/spring/bean.xml
<osgi:reference id="httpService" interface="org.osgi.service.http.HttpService"/> <bean name="servletRegister" class="haha.hello.mvc.userdao.ServletRegister" init-method="init"> <property name="httpService" ref="httpService"/> </bean>
haha.hello.mvc.userdao.ServletRegister.java
DispatcherServlet servlet = new DispatcherServlet();
servlet.setNamespace("foo-servlet");
OsgiWebApplicationContext owac = new OsgiWebApplicationContext();
servlet.setContextClass(owac.getClass());
httpService.registerServlet("/foo", servlet, null ,null);
WEB-INF/foo-servlet.xml
<osgi:reference id="userDao" interface="haha.hello.jpa.userdao.UserDao"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*.html=fooController
</value>
</property>
</bean>
<bean id="fooController"
class="haha.hello.mvc.userdao.FooController">
<property name="userDao" ref="userDao"/>
</bean>
haha.hello.mvc.userdao.FooController.java
PrintWriter out = res.getWriter();
Collection users = userDao.loadUsers();
int count = 0;
for (Iterator iter = users.iterator(); iter.hasNext();) {
User user = (User) iter.next();
out.println("Hello, " + user.getName() + "<BR/>");
}
META-INF/MANIFEST.MF
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: haha.hello.mvc.userdao Bundle-SymbolicName: haha.hello.mvc.userdao Bundle-Version: 1.0.0 Bundle-Activator: haha.hello.mvc.userdao.Activator Bundle-Localization: plugin Import-Package: haha.hello.jpa.userdao, javax.servlet;version="2.4.0", javax.servlet.http;version="2.4.0", org.osgi.framework;version="1.3.0", org.osgi.service.http;version="1.2.0", org.springframework.osgi.context.support, org.springframework.web.servlet, org.springframework.web.servlet.mvc
觀察
- 註冊的動作之前都是用 OSGi DS 做法注入,這次用 Spring 的 osgi:reference 注入,同時也將 userdao 服務注入 controller 之中練習。
- OsgiWebApplicationContext 提供支援將 context 注入,不然 osgi:reference 無法使用。