1. 程式人生 > >白話Spring原始碼(三):spring框架的理解

白話Spring原始碼(三):spring框架的理解

一、為什麼需要Spring

我們想一下如果沒有spring框架我們是怎麼去開發web應用呢?

我估計大部分程式碼是跟業務無關而跟底層或者網路介面互動;物件,模組關係錯綜複雜;開發週期特別的長很容易流產;後期維護時程式碼會越來越爛,最後可能無法維護。。。

那spring框架給我們解決什麼問題了呢?

1.封裝非業務的程式碼,我們只需開發業務功能:比如獲取請求的資料,儲存資料庫,事務等等

2.統一建立物件,維護物件的關係和生命週期

3.方便的對方法加入通用的程式碼

4.簡化與其他框架的結合使用

總的來說就是為了簡化我們企業應用開發的難度,讓我們只關注業務功能的開發!

據說開發spring框架是大神也是一位音樂家,所以我們會發現他寫的程式碼很有藝術感,哈哈哈。。。

二、Spring框架的定義

Spring is a layered Java/J2EE application framework, based on code published in "Expert One-on-One J2EE Design
and Development" by Rod Johnson (Wrox, 2002). Spring includes:

* Powerful JavaBeans-based configuration management, applying Inversion-of-Control principles. This makes
wiring up applications quick and easy. No more singletons littered throughout your codebase, no more arbitrary
properties files: one consistent and elegant approach everywhere. This core bean factory can be used in any
environment, from applets to J2EE containers.

* Generic abstraction layer for transaction management, allowing for pluggable transaction managers, and making
it easy to demarcate transactions without dealing with low-level issues. Generic strategies for JTA and a
single JDBC DataSource are included. In contrast to plain JTA or EJB CMT, Spring's transaction support is not
tied to J2EE environments.

* JDBC abstraction layer that offers a meaningful exception hierarchy (no more pulling vendor codes out of
SQLException), simplifies error handling, and greatly reduces the amount of code you'll need to write.
You'll never need to write another finally block to use JDBC again. The JDBC-oriented exceptions comply to
Spring's generic DAO exception hierarchy.

* Integration with Hibernate, JDO, and iBATIS SQL Maps: in terms of resource holders, DAO implementation
support, and transaction strategies. First-class Hibernate support with lots of IoC convenience features,
addressing many typical Hibernate integration issues. All of these comply to Spring's generic transaction
and DAO exception hierarchies.

* AOP functionality, fully integrated into Spring configuration management. You can AOP-enable any object managed
by Spring, adding aspects such as declarative transaction management. With Spring, you can have declarative
transaction management without EJB... even without JTA, if you're using a single database in Tomcat or another
web container without JTA support.

* Flexible MVC web application framework, built on core Spring functionality. This framework is highly configurable
via strategy interfaces, and accommodates multiple view technologies like JSP, Tiles, Velocity, FreeMarker, iText,
and POI. Note that a Spring middle tier can easily be combined with a web tier based on any other web MVC framework,
like Struts, WebWork, or Tapestry.

You can use all of Spring's functionality in any J2EE server, and most of it also in non-managed environments.
A central focus of Spring is to allow for reusable business and data access objects that are not tied to specific
J2EE services. Such objects can be reused across J2EE environments (web or EJB), standalone applications, test
environments, etc without any hassle.

Spring has a layered architecture; all its functionality builds on lower levels. So you can e.g. use the
JavaBeans configuration management without using the MVC framework or AOP support. But if you use the web MVC
framework or AOP support, you'll find they build on the configuration framework, so you can apply your knowledge
about it immediately.

上面是spring的開發者Rod Johnson對spring的介紹,為什麼我貼出英文的呢?因為很多翻譯過來的就變味了,我們程式設計師還是要堅持看英文原版的一些概念的解釋,這樣會更好的去理解作者的本意。

三、spring架構

 

從這張官方的架構圖種我們可以看出,spring Core是其他模組的基礎,也是spring的核心模組,各模組的作用如下:

* "spring-core" (~185 KB)
- Contents: bean container, core utilities
- Dependencies: Commons Logging, (Log4J)

* "spring-aop" (~110 KB)
- Contents: AOP framework, source-level metadata support
- Dependencies: spring-core, AOP Alliance, (CGLIB, Commons Attributes)

* "spring-context" (~160 KB)
- Contents: application context, validation framework, UI support, JNDI, mail, EJB, remoting, scheduling
- Dependencies: spring-core, (Velocity, FreeMarker, JavaMail, EJB, JAX-RPC, Hessian, Burlap, Quartz)

* "spring-dao" (~175 KB)
- Contents: DAO support, transaction infrastructure, JDBC support
- Dependencies: spring-core, (spring-aop, JTA)

* "spring-orm" (~110 KB)
- Contents: Hibernate support, JDO support, iBATIS SQL Maps support
- Dependencies: spring-dao, (Hibernate, JDO, iBATIS SQL Maps)

* "spring-web" (~70 KB)
- Contents: web application context, multipart resolver, web utilities
- Dependencies: spring-context, Servlet, (Commons FileUpload, COS)

* "spring-webmvc" (~135 KB)
- Contents: web MVC framework, web controllers, web views
- Dependencies: spring-web, (JSP, JSTL, Tiles, iText, POI)

四、spring1.0原始碼地址

https://gitee.com/haoxin963/spring-framework-1.0

大家可以去下載,開始學習spring原始碼之旅吧!