Spring回顧學習

用慣了servlet,大概也設想了一下web框架的實現,加上之前用了Jersey這個超輕量級的框架,導致感覺spring有些臃腫,特別是在網上查spring配置,就會出現很多不同的配置方法,單獨的可能可行,混在一起就各種報錯,這是因爲spring在很多地方提供了多種實現方式,比如bean的管理(beanfactory,beanwapper,applicationcontext),映射方式(配置文件,註解@RequestMapping)等。今天算是重新學習一下spring framework,同時也是自己第一次從頭到尾配置整個框架。

先零散的記錄看到的知識點。重要概念,從書上扒下來的
這裏寫圖片描述
說起spring,第一個想到的就是IOC/DI,先解釋一下,IOC就是由對象獲取資源轉變爲資源獲取對象,意思就是對象之間的依賴關係解除了,避免了高耦合的情況,DI就是實現方式了。spring有三種注入方式,接口,setter和構造,這麼唬人的說法,就好好的舉下面的例子就行:
public class Controller implements Person{…}

Person person = new Person().set(..);

Person person = new Person(name,age…);

不評價。還有個名詞叫依賴查找,好像跟JNDI有關,暫不討論。
還有AOP,相對於OOP,是一種編程思想,擯棄了繼承關係而使用接口實現方式,可以通過抽象類與接口的差別來稍微理解一下,目的也是爲了解耦,而且接口真的簡介,比抽象類易用的多。spring的aop實現水很深,來一篇blog:http://blog.csdn.net/udbnny/article/details/5870076
Spring提供了4種實現AOP的方式:
1.經典的基於代理的AOP
2.@AspectJ註解驅動的切面
3.純POJO切面
4.注入式AspectJ切面

到這裏就大概看到,spring最重要的原理就是利用反射機制實現ioc,然後還用了ThreadLocal實現了線程安全,因爲bean是用單例模式實現的,所以用ThreadLocal方式管理本地資源是最可取的方式。其他很多重要的方面其實都是功能性的東西,比如bean的管理,攔截器的配置,國際化之類的。

然後記錄一下配置。我最怕網上那種一步步的配的,先配一下環境用junit輸出一條語句顯示成功,或者在spring.xml裏面配置一堆bean然後也不在前端顯示的,太耽誤學習過程了。
1.隨便建個web項目,最好是maven的,然後build path,jdk1.8+tomcat1.8,必備
這裏寫圖片描述
2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

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

注意是不是3.1?DispatcherServlet是總的servlet分發器,攔截所有請求。url-pattern是/或/*,先這樣配。

3.spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans       
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <context:component-scan base-package="com.package" />

</beans>

注意是不是4.0?context:component-scan這玩意就一次掃描全部bean了。

4.然後就是這個阻礙我配置道路的東西,project facets,坑死我了,也不知道版本怎麼對應的,反正我以後就用這些版本了
這裏寫圖片描述

代碼部分就很簡單了,就不貼了,我感覺我配置失敗的幾次都是第4個問題,不管報了什麼稀奇古怪的錯,先把4調好了再看。
這裏寫圖片描述
controller裏面RequestMapping配置一下路徑然後return “welcome”應該就可以看到welcome.jsp內容了。配置就這麼多,有問題可留言。

還有兩個小問題,一個是配置了視圖解析器就是org.springframework.web.servlet.view.InternalResourceViewResolver後無法返回數據,這個可以用原生servlet的HttpServletResponse response,但是感覺好不正規,應該還有辦法。另一個是配置了總的攔截器之後js,png等資源文件路徑找不到了,這個也是用servlet原生方法,

  <servlet-mapping>      
    <servlet-name>default</servlet-name>   
    <url-pattern>*.js</url-pattern>   
</servlet-mapping> 

加在DispatcherServlet前面。

順便記錄一個ajax 的error函數,statusText是問題描述,簡直不能再有用了。
error:function(xhr,status,statusText){
alert(xhr.status);
},

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