Eclipse發布了最新的Virgo Tomccat Server。VTS是一個應用服務器與OSGi緊密結合並且可以開發bundles形式的Spring web apps應用,他們同樣擁有OSGi和Spring的特性。真是太好了,所以我想嘗試下。但是很多人和我一樣沒有接觸過VTS的開發工作。VTS的入門有些困難, 網上有些新手文檔但是大部分需要有Spring DM Server的相關開發經驗,所以我決定記錄我的入門經驗幫助從未用過Spring和VTS的新手。如果你了解OSGi和知道怎么開發OSGI的Bundles並且了解相關的開發知識,那么你可以直接看后面的部分使用Spring和OSGi的特性開發第一個應用。
這個教程將告訴大家環境的配置並且在最后開發一個HelloWorld的應用。我將在沒有安裝任何工具的情況下從頭包括環境的配置一步一步的講解, 這部分涵蓋安裝Eclipse,Virgo Tomcat Server和安裝Eclipse的開發插件. 你需要對Eclipse有些簡單的認識,這樣能幫助你了解這部分的內容,但對於開發app不是特別重要, 如果你了解它,在之前的工作中用過,那么你幾分鍾就能將它安裝,所以在這里我就不花篇幅進行詳細介紹了。.
安裝完后我會開發一個Hello World的應用跑在VTS上,這將作為一個引導告訴大家怎么開發這樣的應用,這個例子是Spring開發中非常基礎的入門例子,因為我們不需要任何之前的相關經驗。另外你會首先知道怎么安裝開發工具和使用它。
讓我們開始安裝工具, 下面會列出你需要的:
• VirgoTomcat Server
• Eclipse
• TheSpringSource Tool Suite Eclipse plugin
首先需要獲得Virgo Tomcat Server. http://www.eclipse.org/virgo/download/。解壓縮到任意目錄, 記住路徑稍后會用到,我解壓到 /Users/xiaxiayoyo/Personal/Development/virgo-tomcat-server-3.0.2.RELEASE
下面安裝開發工具,如果你還沒有安裝之前提到的Eclipse,到http://www.eclipse.org下載最新版的Eclipse IDE for Java EE,應為這個版本包含所有Web開發中需要的插件。 Eclipse不需要安裝,你只需要解壓到任意目錄就可以了, 當你第一次運行Eclipse,它將要求你選擇一個workspace location。 后面你所有的工程文件將保存在這個目錄下,你可以使用任意目錄,建議使用空目錄這樣會使你后面不會出什么問題。
接下來安裝SpringSource Tool Suite(STS)插件。
SpringSourceTool Suite組件和依賴將從更新站點安裝到基於3.6和3.7版本的Eclipse。
1. 安裝只有在下面必要條件都滿足才能成功: Eclipse版本要求:Eclipse 3.6 或者 Eclipse 3.7 WTP版本要求:Eclipse WTP 3.2 或者更高版本
2. 在Eclipse Update Manager中取消所有更新站點:
2.1 打開Preferences -> Install/Update -> AvailableUpdate Sites in Eclipse
2.2 全選所有站點先點擊Enable這是Enable按鈕會變成Disable再次點擊
2.3 點擊“OK”關閉preferences窗口並且保存設置
3. 配置STS更新站點:
3.1 下載下面對應版本的更新站點書簽文件
• Eclipse3.6: http://dist.springsource.com/release/TOOLS/composite/e3.6/bookmarks.xml
• Eclipse3.7: http://dist.springsource.com/release/TOOLS/composite/e3.7/bookmarks.xml
3.2 打開Preferences -> Install/Update -> AvailableUpdate Sites in Eclipse
3.3 點擊“Import...” 按鈕選擇剛才下載的bookmarks.xml. 點擊“Open” 完成導入.
3.4 驗證列表中是否有加入的新站點,如果成功關閉 preference窗口
4. 安裝 4.1 通過Help -> Install New Software ...打開update manager
4.2 在“Workwith” 下拉菜單中選擇名稱為“SpringSourceUpdate Site for Eclipse 3.6” 或 “SpringSource Update Site for Eclipse 3.7”的更新站點
確定選擇上圖勾選的組件,並且按照上面的安裝現象進行安裝。
點擊Next根據提示一步一步到Finish安裝結束。
接下來安裝SpringSource DM Server Tools插件,經過我的安裝官方給出1.1.1和2.0.0都無法正常安裝,只能安裝snapshots版,通過官方給出的在線更新地址,安裝步驟和上面的一樣,按照下圖的選項安裝。
接下來在Eclipse中設置VTS,打開Eclipse的preferences 找到Server -> Runtime Environments。 點擊右邊的Add…-按鈕,選擇SpringSource ->SpringSource dm Server (Runtime) v2.1. 確定勾選上下面的“Create a new local server” . 點擊Next, 你被要求輸入服務器名稱(可以隨意) 然后需要選擇上面你解壓VTS的目錄。所以在我這里我輸入/Users/xiaxiayoyo/Personal/Development/virgo-tomcat-server-3.0.2.RELEASE。
最后一步我們會嘗試將它啟動起來。打開“Servers”視圖如果不在你的工作區,可通過菜單打開Window -> Show View-> Other… -> Server -> Servers。選擇剛才配置的服務點擊“Start” (白色三角綠色背景) 或者右鍵菜單選擇"Start". 當狀態變為Started, 使用瀏覽器訪問http://localhost:8080. 你將看到服務器的初始頁. 你可以通過連接打開控制台並且輸入默認密碼(用戶名:admin,密碼:springsource)。
環境配置就結束了. 在下面我們將開發一個hello world應用
應用將由2個bundles組成:The app will consist of two bundles:一個bundle將對外公開一個服務,第二個bundle將包含一個web應用使用第一個bundle的服務來產生一個歡迎頁面。
在大多數情況下更好的辦法是把服務的接口單獨放在一個bundle里面,這樣可以避免依賴更新的問題。本教程我們不那么做。為了創建一個bundle,我們需要在Eclipse中先創建一個工程,選擇 File -> New -> Project…-> EclipseRT 並且創建一個BundleProject。 取名為HelloProvider.在BundleContent頁, 選擇Module Type is “None”. 工程創建完后,右鍵選擇工程在菜單中選擇Properties->TargetedRuntimes確定選擇上面在Eclipse中創建的VTS, 創一個生命服務接口的接口文件,在這里我創建的是com.xiaxiayoyo.helloprovider.HelloProvider這個接口只有一個方法:
- publicString getGreeting()
接下來創建一個實現類;com.xiaxiayoyo.helloprovider.impl.HelloProviderImpl. 實現定義的接口如下:
- public String getGreeting() {
- return "Hello World!";
- }
在標准的OSGi系統里, 我們應該創建一個類BundleActivator,它會實例化服務的實現類和注冊它。但在這里我們使用VirgoTomcat Server,我們不必在這么干了,Spring會幫我們干這件事,不過你仍然可以自己來處理,而且一樣正常工作,這樣是為了將其他項目輕松的遷移到VirgoTomcat server中。
接下來是使用Spring的注冊配置來注冊OSGi服務, 在你bundle工程的META-INF目錄里創建一個新目錄"spring",創建一個文件"osgi-context.xml"。在文件里我們定義一個bean將它注冊為一個OSGi的服務,如下面文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beansxmlnsbeansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd">
- <osgi:serviceinterfaceosgi:serviceinterface="com.xiaxiayoyo.helloprovider.HelloProvider">
- <beanclassbeanclass="com.xiaxiayoyo.helloprovider.impl.HelloProviderImpl" />
- </osgi:service>
- </beans>
你可以看到我們將之前接口的實現類注冊為服務。啟動這個bundle,VTS將實例化服務實現類HelloProviderImpl並且把它注冊為接口HelloProvider的服務,所以其他bundle可以使用這個服務。
下面,要讓它們能這么做我們還需要導出這個接口。 打開MANIFEST.MF文件,到runtime頁,在“exported packages”中添加com.xiaxiayoyo.helloprovider包, 不要導出實現類的包。
現在我們需要開發一個web工程通過服務來展示歡迎頁面。接下來創建一個Dynamic Web Project這里我叫做GreeterUI,Target runtime選擇VTS,Configuration選擇VTS,如下圖:
點擊下一步 ,在Default output folder里面將輸出路徑改為WebContent/WEB-INF/classes,這里的WebContent為在下個頁面設置的Content Directory,兩個參數一定要確保一直,如下圖:
點擊下一步,這里的Context Root無關緊要因為在Bundle里面上下文路徑在MANIFEST.MF里面配置但是還是建議兩個地方一致。如下圖:
點擊Finish完成。
接下來還需要幾步來配置下工程右鍵工程選擇Properties,確定Targeted Runtimes為之前創建的VTS,另外由於這個Web Project的開發需要HelloProvider工程提供的服務,所以在這里需要在Project References里勾選HelloProvider工程。當前這個工程是標准的Web工程,我們需要將它加入Spring Project的特性,我們需要再次右鍵工程->Spring Tools->Add OSGiBundle Project Nature,並且選擇EnableBundle Classpath Container。
工程創建完成后,導入必要的依賴包。使用manifest編輯器打開manifest文件(META-INF->MANIFEST.MF),選擇Dependencies頁添加依賴項。配置完后如下:
- Manifest-Version:1.0
- Module-Type:Web
- Bundle-SymbolicName:GreetUI
- Bundle-Version:1.0.0
- Bundle-Name:GreetUI
- Bundle-ManifestVersion:2
- Web-ContextPath:greeter
- Import-Package:com.xiaxiayoyo.helloprovider,
- javax.servlet;version="[3.0.0,3.0.0]",
- javax.servlet.http;version="[3.0.0,3.0.0]",
- javax.servlet.jsp.el;version="[2.2.0,2.2.0]",
- javax.servlet.jsp.jstl.core;version="[1.2.0.v20110728,1.2.0.v20110728]",
- org.eclipse.virgo.web.dm;version="[3.0.2.RELEASE,3.0.2.RELEASE]"
- Import-Bundle:com.springsource.org.apache.taglibs.standard;version="[1.1.2.v20110517,1.1.2.v20110517]"
- Import-Library:org.springframework.spring;version="[3.0.5.RELEASE,3.0.5.RELEASE]"
- Bundle-ClassPath:/WEB-INF/classes/
注意一定要設置Classpath,不然會響應404錯誤。
接下來創建Web應用的控制器,創建一個 class叫 com.xiaxiayoyo.greeterui.GreetingsController ,代碼如下:
- @Controller
- public class GreetingsController {
- @RequestMapping(value= "/greeting", method = { RequestMethod.GET,RequestMethod.POST})
- public ModelAndView greeting(HttpServletRequest request) {
- ModelAndViewmv = new ModelAndView();
- mv.addObject("greeting",provider.getGreeting());
- mv.setViewName("greeting");
- returnmv;
- }
- @Autowired
- private HelloProvider provider;
provider會被Virgo Tomcat Server注入之前注冊的OSGi服務HelloProvider,The setter will be used by the DM server to set thereference to the HelloProvider service which we have registerd in the OSGiservice registry before. 方法greeting在用戶發起請求的時候會被spring調用。這個例子的邏輯比較簡單,只是從HelloProvider中拿到歡迎信息然后把信息放入ModelAndView內,ViewName是用來渲染頁面的JSP文件名。
接下來需要配置來告訴Spring來為控制器裝配OSGi服務,Spring會先查找HelloProvider然后將引用注入到GreetingsController,你需要告訴Spring那些服務是必須的,那么需要,創建一個context-osgi.xml,內容如下:
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:osgi="http://www.springframework.org/schema/osgi"
- xmlns:context="http://www.springframework.org/schema/context"
- 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
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <osgi:referenceidosgi:referenceid="helloProvider"
- interface="com.xiaxiayoyo.helloprovider.HelloProvider"/>
- </beans>
接下來需要在Web.xml里加入Spring MVC相關的配置,如下:
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <web-appxmlns:xsiweb-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID"version="2.5">
- <display-name>GreetUI</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/osgi-context.xml
- </param-value>
- </context-param>
- <context-param>
- <param-name>contextClass</param-name>
- <param-value>
- org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext
- </param-value>
- </context-param>
- <servlet>
- <servlet-name>springDispather</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springDispather</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <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>
DispatcherServlet是Spring MVC中負責轉發的,所以我們需要告訴它怎么處理請求,我們在相同的目錄下面再創建一個配置文件,配置文件名稱的前綴和ServletName一樣,springDispather-servlet.xml,內容如下:
- <?xmlversionxmlversion="1.0" encoding="UTF-8"?>
- <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:osgi="http://www.springframework.org/schema/osgi"
- xmlns:context="http://www.springframework.org/schema/context"
- 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
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scanbase-packagecontext:component-scanbase-package="com.xiaxiayoyo.greeterui"/>
- <beanidbeanid="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <propertynamepropertyname="prefix"value="/WEB-INF/jsp/" />
- <propertynamepropertyname="suffix" value=".jsp" />
- </bean>
- </beans>
最后一步, 我們需要一個JSP來渲染我們的歡迎頁面. 創建目錄/WEB-INF/jsp創建greeting.jsp.
內容如下:
- <%@ page contentType="text/html;charset=UTF-8"language="java" %>
- <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
- <html>
- <head>
- <title>Hello!</title>
- </head>
- <body>
- <c:out value="${greeting}"/>
- </body>
- </html>
開發就結束了,剩下的是部署。打開Server視圖,右鍵VTS選擇 “Add and Remove Projects…” 添加GreeterUI和HelloProvider兩個工程. 當你啟動服務器你會看到下面類似的日志輸出:
- [2012-04-2215:13:31.078] start-signalling-5 <WE0000I> Starting web bundle 'GreetUI' version '1.0.0' with context path'/greeter'.
- [2012-04-2215:13:31.505] start-signalling-5 <WE0001I> Started web bundle 'GreetUI' version '1.0.0' with context path'/greeter'.
- [2012-04-2215:13:31.507] start-signalling-5 <DE0005I> Started bundle 'GreetUI' version '1.0.0
這個時候打開游覽器訪問http://localhost:8080/greeter/greeting.do,並且你會看到HelloProvder接口的返回內容。
參考文檔: