Spring Framework 官方文檔學習(一)介紹


 
Spring框架提供了大概20個模塊,分類為: Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming)Instrumentation, Messaging, and Test
 
1、 Core Container包含:spring-core, spring-beans, spring-context, spring-context-support, and spring-expression 模塊。
①spring-core, spring-beans是Spring框架的基礎,提供了IoC和DI功能。spring-beans中的BeanFactory是工廠模式的實現。
 
②spring-context模塊基於spring-core、spring-beans,是一種框架式訪問對象的方法,類似JNDI注冊方式。
還添加了國際化支持、事件傳播支持、資源加載支持、上下文的創建支持等等。還有JavaEE的部分功能。
ApplicationContext接口是該模塊的核心。
 
③spring-context-support模塊則提供了集成通用第三方庫的支持,將其集成到Spring應用上下文中,如緩存、郵件、計划、模板引擎。
 
④spring-expression模塊提供了可在運行時查詢和操作對象圖的表達式語言,很強大。是對JSP中unified EL的擴展。
支持:設置/獲取屬性值,屬性分配,方法調用,訪問數組、集合及索引的內容,邏輯和數字運算符,命名變量,通過名字在Spring IoC容器中獲取對象。
 
2、 AOP and Instrumentation包含:
spring-aop模塊提供了AOP編程,允許定義方法攔截器和切入點,以與具體的功能代碼解耦合。
spring-aspects模塊集成了AspectJ。
spring-instrument模塊,提供了類工具支持和類加載器實現,用於特定的應用服務器。
spring-instrument-tomcat模塊,包含了對tomcat的代理。
 
3、 Messaging
Spring Framework 4 包含了spring-messaging模塊,帶有Spring Integration項目的關鍵抽象,如Message、MessageChannel、MessageHandler以及其他,以作為基於消息的應用的基礎。 
也提供了一些注解,可以將消息發送到方法中。
 
4、 Data Access/Integration,包含JDBC、ORM、OXM、JMS和Transaction模塊。
spring-jdbc模塊,提供了JDBC抽象層,免除了繁雜的JDBC編碼和不同數據庫開發商錯誤代碼的分析。
spring-tx模塊,允許代碼式和聲明書事務管理。(需要實現特定接口,支持所有POJO??)
spring-orm模塊,提供了對常見ORM的集成層,常見ORM包括JPA、JDO和Hibernate。通過使用該模塊,你既可以使用ORM的功能,也可以使用Spring提供的功能,例如簡單的聲明式事務管理。
spring-oxm模塊,提供了抽象層,支持Object/XML映射實現 如JAXB、Castor、XMLBeans、JiBX和XStream。
spring-jms模塊,包含了生產和消費消息功能,集成了spring-messaging模塊。
 
5、 Web,包含spring-web, spring-webmvc, spring-websocket, and springwebmvc-portlet模塊。
spring-web模塊,提供了基本的web功能,如文件上傳、使用Servlet Listeners初始化IoC容器、web容器。同樣包含了一個HTTP客戶端和Spring遠程支持的web部分。
spring-webmvc模塊,包含了MVC和REST Web Service實現。
spring-webmvc-portlet模塊,提供了用於Portlet環境的MVC實現,鏡像了spring-webmvc模塊的功能。
 
6、 Test
spring-test模塊,支持Spring組件的單元測試和集成測試,使用JUnit或者TestUG。提供了Spring ApplicationContext的加載及緩存。還提供了mock對象!
 
技巧
通過使用Spring BOM,在  dependencyManagement 導入spring-framework-bom ,然后就可以保證所有的Spring依賴都是同一個版本 -- 可以不用再聲明版本了。
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
<dependencies>

 

 
Logging,這是Spring唯一強轉要求的外部依賴,JCL。
關掉JCL:從spring-core模塊中排除依賴。或者,選一個依賴取代JCL。
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.3.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

至此,已經破壞了應用(不能運行了),所以還需要修復一下。使用JCL的其他實現例如SLF4J。

SLF4J,是編譯時綁定,而不是運行時判斷,所以更干凈。(應該是指在編譯時就判斷是否需要相應的代碼,不需要就去掉)
但是,有一些容器內置了JCL的實現,這時單純的排除依賴是不夠的。這時候最好的辦法是反轉類加載器層次,讓應用控制JCL依賴,而非容器控制。(如IBM的WebSphere Application Server。知道就行,略過。)
 
 
4.0 版本的改進(摘錄):
  1. 注入bean時,支持泛型限定。如 @Autowired Repository<Customer> customerRepository  。
  2. CGLIB-based proxy classes 不再需要一個默認構造器。
  3. 新的 @RestController 注解。
4.1 版本的改進(摘錄):
  1. 用Jackson支持JSONP
  2. 新的lifecycle,允許攔截 @ResponseBody@ResponseEntity 方法,在controller返回之后,但寫入響應之前。
  3. 新的HttpMessageConverter選項:通過 jackson-dataformat-xml擴展開啟基於Jackson的XML序列化支持。如果存在 jackson-dataformat-xml,將取代 JAXB2
4.2 版本的改進(摘錄):
  1. 可以使用Java 8的默認方法來編寫配置文件,@Bean 默認方法。
  2. @Import 支持導入常規類作為bean,而不僅僅是配置類了。
  3. 內建的CORs支持,包含全局的(MVC Java config和 XML namespace)和局部的(如@CrossOrigin)配置。
  4. 默認的JSON前綴,由 "{} && " 改成更安全的 ")]}', " 
4.3 版本的改進(摘錄):
  1. Java 8 的默認方法可以用作bean屬性的GETTER/SETTER。
  2. 如果目標bean只定義了一個構造方法,就不再需要@Autowired 注解了。
  3. 內建了對 HTTP HEAD and HTTP OPTIONS 的支持。
  4. @RequestMapping 新的組合注解:@GetMapping@PostMapping@PutMapping@DeleteMapping, and @PatchMapping

  5.  

    新的組合注解:@RequestScope, @SessionScope, and @ApplicationScope

  6.  

    新的 @RestControllerAdvice 注解。

  7.  

    新的 @SessionAttribute 和 @RequestAttribute 注解。

  8.  @

    ModelAttribute allows preventing data binding via binding=false attribute (see reference).

  9.  @

    PathVariable may be declared as optional (for use on @ModelAttribute methods).

  10.  

    HTTP消息轉換器中一致的charset處理,包括multipart文本內容默認的UTF-8編碼。

  11. 靜態資源處理使用配置好的ContentNegotiationManager ,來決定媒體類型。

 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM