spring3.0 配置

來源:互聯網
上載者:User

原帖地址:http://blog.csdn.net/wzl002/article/details/5969469
這次從零開始建一個web項目, 發現網上針對spring 3.0的講解不多, 所以藉此機會記錄一下配置一個基本的web項目, 主要是配置spring3的過程, 其實大部分和2是類似的.

第一步: 建立並部署一個web項目, (可跳過)
1. 這裡想關於web項目簡單說一句, 其實一個wen工程最基本的,只看3個地方:
在根目錄下(這個目錄一般習慣叫WebContext或WebRoot)有
 1. WebRoot/WEB-INF/web.xml  啟動引導檔案
 2.WebRoot/WEB-INF/classes/  編譯的class檔案,會根據package建立子路徑
 3.WebRoot/WEB-INF/lib/      jar包 (注意,lib下不可以再建子目錄)
這就是一般比較常用的web工程的結構. 有了這三個結構, 然後把WebRoot的絕對路徑告訴tomcat這樣的web容器,就可以啟動了(當然裡面要有相應的東西才行).
因此雖然我們一般項目的結構是: projectname/src, projectname/WebRoot 但實際上src裡的.java原始碼是tomcat完全不關心的,它的解析是從WebRoot開始的, .class才是它認識的.

下面是一個初始的web.xml. 這個檔案就相當於項目的啟動引導檔案, web容器最先找到這個檔案,然後解讀並一一啟動這些類.

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.  version="2.5">  
  6.    
  7.     
  8.  <servlet>  
  9.   <servlet-name>TestServlet</servlet-name>  
  10.   <display-name>測試用Servlet</display-name>  
  11.   <servlet-class>test.TestServlet</servlet-class>  
  12.  </servlet>  
  13.  <servlet-mapping>  
  14.   <servlet-name>TestServlet</servlet-name>  
  15.   <url-pattern>/test/*</url-pattern>  
  16.  </servlet-mapping>  
  17.     
  18.    
  19.  <welcome-file-list>  
  20.   <welcome-file>index.jsp</welcome-file>  
  21.  </welcome-file-list>  
  22. </web-app>  

 

 2.部署至tomcat:
tomcat的部署方式有很多, 我比較喜歡的部署方式是apache-tomcat/conf/Catalina/localhost/ 下放入projectname.xml設定檔,

內容為

view plain
  1. <Context path="projectname" reloadable="true" docBase="D:/workspace/projectname/WebContent" ></Context>  

工程就部署完了. 簡單吧.
然後在eclipse裡也不用去配什麼server,運行環境. 裝一個tomcat外掛程式,指定tomcat路徑就行了. 上面的xml檔案也可以通過配置eclipse-tomcat外掛程式自動產生.

 

第二步: 把spring的jar包引進來, 這可不是廢話哦. 見過spring3.0的都知道, spring3把之前的一個萬能spring.jar分拆成了20個小jar. 當然你也可以省事的直接把20個都引進來,沒問題.
不過如果你不想這麼幹, 這裡是一個簡單能在web中實現最基本的依賴注入所需的jar包:
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
commons-logging-1.1.1.jar
其中commons-logging-1.1.1.jar 是spring的依賴包.

 

以上是一個不含spring-jdbc資料庫管理的合集, 如果要用spring的持久層,還需要:
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
commons-dbcp-1.3.jar  
commons.pool-1.5.3.jar
同樣,後面2個也是spring的依賴包.
關於spring持久層的配置在下一節介紹

 

第三步: 配置spring, 基本和spring2還是一樣的.
1 在web.xml中加入
 

view plain
  1. <context-param>    
  2.   <param-name>contextConfigLocation</param-name>    
  3.   <param-value>/WEB-INF/spring*.xml</param-value>    
  4.  </context-param>    
  5.  <listener>    
  6.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  7.  </listener>   


以前見通過配置spring的Servlet方式, 那種是為了對應在一些低版本的web容器. 這裡就不講了.

2. 在WEB-INF/ 下建一個 spring-conf.xml  (名字和路徑都隨便了, 跟第一步的配置對應就行了.不過出於安全考慮還是不要放到WEB-INF外面.)

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  6.     
  7.  <bean id="TestBo"  
  8.   class="test.TestBo">  
  9.  </bean>  
  10.   
  11.  <bean id="TestAction" class="test.TestAction">  
  12.   <property name="TestBo" ref="TestBo" />  
  13.  </bean>  
  14. </beans>  

這裡spring的配置就算完成了, 然後把測試類別實現一下, 就可以測測效果啦. 基本除了jar包以外,和spring2都是一致的.

下面是TestServlet , 其他的TestAction和TestBo就隨便寫了. 啟動後訪問

http://127.0.0.1:8080/projectname/test/abc 就可以看到執行輸出了

view plain
  1. package test;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletConfig;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.springframework.context.ApplicationContext;  
  12. import org.springframework.web.context.support.WebApplicationContextUtils;  
  13.   
  14. public class TestServlet extends HttpServlet {  
  15.   
  16.         TestAction ta ;  
  17.     public TestServlet()  
  18.     {  
  19.     }  
  20.   
  21.     public void init(ServletConfig servletConfig)  
  22.       throws ServletException{  
  23.      servletConfig.getServletContext();  
  24.      ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());  
  25.      this.ta = ctx.getBean(TestAction.class);  
  26.      this.ta.sayHello();  
  27.    System.out.println("TestServlet init");  
  28. //     TestAction ta = new TestAction();  
  29. //     ta.sayHello();  
  30.     }  
  31.   
  32.   
  33.     public void doGet(HttpServletRequest req, HttpServletResponse resp)  
  34.       throws IOException, ServletException  
  35.     {  
  36.       doPost(req, resp);  
  37.     }  
  38.   
  39.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  40.       throws IOException, ServletException  
  41.     {  
  42.      this.ta.sayHello();  
  43.     }  
  44.   
  45.     public void destroy()  
  46.     {  
  47.     }  
  48. }  

.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.