從零開始學--Java搭建 Spring MVC 框架

沒有什麼比一個時代的沒落更令人傷感的了

整個社會和人都在追求創新、進步、成長,沒有人願意停步不前,一個個老事物慢慢從我們生活中消失掉真的令人那麼傷感麼?或者說被取代?我想有些是的,但有些東西其實並不是這樣。

遊天鳴和藍玉不正說明了兩種截然不同的人生麼。隨着社會的進步,那些消失的藝術或者說民間藝術可能現實中我們再也見不到了,這個社會永遠都是優勝劣汰,消失以爲着不再需要,只不過這些老物件有更多的技藝和精神層面的東西罷了。

《百鳥朝鳳》這部電影我最近纔在網絡上看,看完後我久久不能動彈,它並沒有強烈的戲劇衝突,畫面也不夠細膩,人物刻畫並不突出,但它講明瞭嗩吶這個事兒,不腦殘,令人動容,這纔是有思考的電影。

如果創建一個 Spring 項目

Spring MVC 框架在 Java 的 Web 項目中應該是無人不知的吧,你不會搭建一個 Spring 框架?作爲身爲一個剛剛學習Java的我都會,如果你不會的話,那可真令人憂傷。

1.在 MyEclipse 創建項目後,可以以選擇的方式去配置一個 Spring 項目,這裏不在討論。因爲我只用 Eclipse。

2.手動搭建。就是動手。

新建一個 Java Web 項目

1.打開 Eclipse ,在Project Explorer選項卡下面點擊右鍵,選擇Web - Dynamic Web Prodect(這一步應該都知道阿!!!)。

newProject.png

2.點擊Next。起一個你認爲還不錯的項目名,注意:命名很重要,把每一次命名都當做給自己孩子起名字一樣莊嚴神聖。

SpringDemo.png

3.沒有了,完成。

demoMenu.png

搞到 Spring 框架的 jar 包

無論你用坑蒙拐騙,還是死皮賴臉,只要你搞到 Spring 框架的 jar 包就行。我這裏給你個地址,你可以體面的去下載就行了。地址:http://projects.spring.io/spring-framework/
找到適合自己的版本,下載下來保存到合適的位置就可以了,就這麼簡單。解壓後,應該是這樣的:

spring4.2.6.png

你看包的命名,你可能就大致明白了這個 jar 包是幹嘛的了,接下來就是引入你需要的了。
然後,你要你需要的 jar 包,複製到項目的/WebContent/WEB-INF/lib下,爲什麼要這麼做,下面會說的。

導入 jar 包

記得當年一個學 Java 的朋友抱怨說: Java 每天都在導包,不如 .Net 爽。我現在並不這麼認爲。
在項目名上,點擊右鍵,Build Path - Configure Bulid Path... - Libraries - Add JARs...,在彈出的框裏邊找到項目的/WebContent/WEB-INF/lib,這樣就看到剛剛你複製過來的 jar 包了。

add-jars.png

配置配置配置

搭建 Spring 框架最重要的步驟應該就是配置了。官網對框架的解釋說明如下:

Spring MVC 框架是圍繞一個 DispatcherServlet 來設計的,這個 Servlet 會把請求分發給各個處理器,並支持可配置的處理器映射、視圖渲染、本地化、時區與主題渲染等,甚至還能支持文件上傳。處理器是你的應用中註解了 @Controller 和 @RequestMapping 的類和方法,Spring 爲處理器方法提供了極其多樣靈活的配置。

所以,首先我們應該在/WebContent/WEB-INF/下新建web.xml文件,接下來在這個文件中配置 DispatcherServlet。

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

還可以配置字符編碼,默認啓動頁面什麼的,這裏不在配置,具體見示例項目:https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/web.xml,因爲這裏是把 DispatcherServlet 命名爲springMVC,並且讓它在 Web 項目一啓動就加載。接下來我們需要在/WebContent/WEB-INF/目錄下創建一個springMVC-servlet.xml的Spring配置文件。Spring官方文檔上推薦的默認的文件名是[servlet-name]-servlet.xml文件,這裏 servlet-name 叫 springMVC ,因此,我新建了一個springMVC-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

<!-- 使用默認的註解映射 -->
<mvc:annotation-driven />
<mvc:resources location="/" mapping="/index.html" />


<!-- 自動掃描controller包中的控制器 -->
<context:component-scan base-package="cn.mayongfa.api.controller" />
<context:component-scan base-package="cn.mayongfa.controller" />

<!-- 上傳文件攔截,設置最大上傳文件大小 30M=30*1024*1024(B)=31457280 bytes -->
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="31457280" />
</bean>

具體詳見:https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/springMVC-servlet.xml
我們在web.xml文件中定義的contextConfigLocation,指定要裝入的 Spring 配置文件,一般文件都命名爲applicationContext.xml,這個文件中我們可以進行掃描類包、讀取配置文件、數據源管理、AOP配置、緩存以及消息隊列等配置,所以,接下來就新建applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 將多個配置文件讀取到容器中,交給Spring管理 -->
<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:global.properties</value>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

<!-- 掃描類包,將標註Spring註解的類自動轉化Bean,同時完成Bean的注入 -->
<context:component-scan base-package="cn.mayongfa.common" />
<context:component-scan base-package="cn.mayongfa.service" />
<context:component-scan base-package="cn.mayongfa.dao" />

<!--master 配置數據源 -->
<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    <property name="driverClassName">
        <value>${master.jdbc.driverClassName}</value>
    </property>
    <property name="url">
        <value>${master.jdbc.url}</value>
    </property>
    <property name="username">
        <value>${master.jdbc.username}</value>
    </property>
    <property name="password">
        <value>${master.jdbc.password}</value>
    </property>

    ...
</bean>

<!--slave 配置數據源 -->
<bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    
            ...
</bean>

<bean id="dataSource" class="cn.mayongfa.service.imp.DynamicDataSource">
    <property name="targetDataSources">
        <map>
            <entry key="slave" value-ref="slaveDataSource" />
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="masterDataSource" />
</bean>

<!-- 配置Jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 配置事務管理器 -->
...

<!-- 通過AOP配置提供事務增強,讓service包下所有Bean的所有方法擁有事務 -->
...

上面只是簡單的配置,文件並不完整,具體完整項目示例見GitHub:https://github.com/mafly/SpringDemo

到這裏,其實我們已經配置完成了,接下來就是新建我們需要的Package包,來實現不同包來完成不同的事兒的。

新增 Package 包

分層的意義及優缺點我這裏不在嘮叨,按照正常的分層架構一般都會分爲 View 層、Action 層、Service 層、Dao 層,這裏我們也是這樣做的,下面就開始新建包,.Net 下面是叫類庫。

package.png

按照這樣的方式新建就可以了,具體的架構如下圖:
demoLastMenu.png

到這裏,搭建 Spring MVC 框架的工作算是完成了。接下來就是配置具體的數據源、緩存、AOP、JMS 這些東西了。祝你好運!

歡迎大家一起學習研究相關技術

願意瞭解框架技術或者源碼的朋友直接求求交流分享技術:2042849237

更多詳細源碼參考來源:http://minglisoft.cn/technology

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章