關於Spring-mvc的機制以及Ioc、DI的理解

Spring-mvc算得上是ssh框架中的管理者,是用來配置bean,並維護bean之間的關係的容器框架。beans是spring核心配置文件:applicationContext.xml的主角,程序中所有的對象都可以配置爲bean併爲其注入屬性值,是實現IoC和DI的關鍵,下面先介紹一下Spring的工作機制:
這裏寫圖片描述
一般來說,我們把一個項目分爲Web層、業務層、Dao層和持久層,Web層主要是頁面的展示以及動作的處理,可以認爲涵蓋了View層與Control層,業務層包含的主要是對象實體類,也就是常說的bean或者pojo,也有叫domain的,以及Service包;Dao層是定義數據庫訪問接口層,如果用到了hibernate框架,這裏就是hibernateUtil發揮功能的地方。持久層的功能是將數據庫中的數據固化在存儲設備中,也就是hibernate工作的層次,主要是解決關係模型和對象模型之間的阻抗。業務層+Dao層+持久層就是MVC中常說的Model層,但是並非Model層一定會同時包含這三種層次,可根據自己的需要進行添加。MVC三層架構的理解並不是一概而論,各層的稱呼也各有千秋,主要是方便自己理解就好。
Spring的作用主要是用來管理各個層次的組件,因爲各層級之間交流依賴的對象,都被以bean的形式配置在spring的核心文件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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  http://www.springframework.org/schema/mvc  
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

  <bean id="test" class="com.Spring.Student.stu">
  <property name="name" value="David"></property>
  </bean>

  </beans>


<beans></beans>
標籤是定義bean的起止標籤,只有在這裏定義的bean纔有效。

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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"

這段定義也是必須的,相當於beans的聲明。

<bean id="test" class="com.Spring.Student.Test">
  <property name="name" value="David"></property>
  </bean>

稱爲一個bean,bean的id必須唯一,這個id名就是在程序中實例化的時候的對象名,class是這個bean的來源,必須帶包名否則找不到,
id=”test” class=”com.Spring.Student.Test”這句話的意思就是在com.Spring.Student包下有一個類叫做Test,它被實例化了的時候,它的對象就叫test,也就是說,當程序實例化這個bean時,會產生類似如下效果:Test test=new Test();但程序中不需要這麼寫,所以這也是控制反轉的原理。

 <property name="name" value="David"></property>

這句話的意思是說,這個Test類中有一個name屬性,將這個name屬性的值設置爲Davide,這裏需要注意的是,這個Test類中必須要存在name屬性,並有這個屬性的set和get方法,缺一不可,同理我們還可以注入其他屬性,只要該類中存在這個屬性及其get、set方法。如:

<property name="age" value="18"></property>
<property name="gender" value="male"></property>

從中我們就能窺到一些控制反轉的定義所在了:
Ioc控制反轉,即將創建及維護對象(bean)的權利交付給Spring框架而不是像傳統的new方式在程序中創建,同時程序也不再負責維護對象,這種控制權的轉移就是Spring框架的控制反轉機制。
DI依賴注入,依賴注入實質上是和控制反轉同樣的道理,只是爲了更加體現Spring的核心概念。

<property name="propertyname" value="propertyvalue"></property>

這種定義屬性的方式就叫做注入,而bean和bean之間的關係不是獨立的,可以通過引用而相互依賴,如在上面的bean中若有如下的一條屬性注入:

<property name="Davide" ref="course"</property>

同時,配置文件中也要有course的定義:

<bean id="course" class="com.Spring.Student.Course">
  <property name="name" value="C++"></property>
  </bean>

相當於在Student類中引用了Course類的對象,那麼test就依賴於course,因此注入依賴和控制反轉表達的其實是一個意思只是理解的角度有所不同。
以上就是我在學習過程中對Spring-mvc和控制反轉和依賴注入的理解,如果有更好的理解或者不同意見,歡迎交流。

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