SpringMVC和Spring是什麼關係?


Spring可以說是一個管理bean的容器,也可以說是包括很多開源項目的總稱,spring mvc是其中一個開源項目,所以簡單走個流程的話,http請求一到,由容器(如:tomact)解析http搞成一個request,通過映射關係(路徑,方法,參數啊)被spring mvc一個分發器去找到可以處理這個請求的bean,那tomcat裡面就由spring管理bean的一個池子(bean容器)裡面找到,處理完了就把響應返回回去。

經常看到寫個springmvc的controller裡面有個註解service吧,看起來不像類屬性吧?spring的ioc功能就可以把這個sercice插進去(反射)~

當然具體過程好複雜的,這只是個大概的大概~


印象里spring早期就是一個IoC容器實現,後來,隨著IoC思想的發揚光大,逐步將各種功能模塊集成到IoC這個框架下,包括事物、日誌、緩存等。那時候SSH就是以Spring的IoC容器為核心,銜接Struts和Hibernate來搭建應用。

後來SpringMVC出來了,作為Spring的一個子項目,與Spring的IoC容器配合起來更簡便,逐步淘汰Struts。


基於Spring實現的MVC框架。不能不使用Spring,單獨使用SpringMVC,因為其需要依賴Ioc容器等。但是如果單獨為了更好的理解SpringMVC這種MVC框架,就把它和Struts/Struts2。。。一系列的MVC框架,只是基於DispatcherServlet或者Filter做一個前端分發器,最終把這個框架引導起來,進行其自己的邏輯處理。沒有什麼特殊的地方。每個MVC框架都有自己的特點,互相比較一下,就理解原理了。


最開始只有spring,後來出了亂七八糟的比如mvc,security,boot等。原來的spring就變成了現在的spring core。所以spring,也既spring core指ioc,aop那塊。mvc指web的mvc框架


springMVC.startWith(spring) 結果為true


可以把spring看成是一個工具箱,裡面有各種工具,springmvc只是其中的一把扳手。


spring boot extends spring mvc extends spring


SpringMVC是一個MVC模式的WEB開發框架;

Spring是一個通用解決方案, 最大的用處就是通過Ioc/AOP解耦, 降低軟體複雜性, 所以Spring可以結合SpringMVC等很多其他解決方案一起使用, 不僅僅只適用於WEB開發

ps: 一個Spring + Spring MVC開源項目供參考: youzan/bugCatcher


springmvc就是spring在mvc框架中的實踐,與springboot啥的沒有區別,面相的方向不同而已


spring主要包括IOC AOP這兩大塊,spring mvc是web層的框架,主要用來接受HTTP請求並轉發給controller,主要就是幾個常用的註解@Controller @service

如果你英文夠好的話,我建議你讀一讀下面的解釋

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files. The default handler is based on the @Controller and @RequestMapping annotations, offering a wide range of flexible handling methods. With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications, through the @PathVariable annotation and other features.

"Open for extension…" A key design principle in Spring Web MVC and in Spring in general is the "Open for extension, closed for modification" principle.

Some methods in the core classes of Spring Web MVC are marked final. As a developer you cannot override these methods to supply your own behavior. This has not been done arbitrarily, but specifically with this principle in mind.

For an explanation of this principle, refer to Expert Spring Web MVC and Web Flow by Seth Ladd and others; specifically see the section "A Look At Design," on page 117 of the first edition. Alternatively, see

  • Bob Martin, The Open-Closed Principle (PDF)

You cannot add advice to final methods when you use Spring MVC. For example, you cannot add advice to the AbstractController.setSynchronizeOnSession() method. Refer to Section 10.6.1, 「Understanding AOP proxies」 for more information on AOP proxies and why you cannot add advice to final methods.

In Spring Web MVC you can use any object as a command or form-backing object; you do not need to implement a framework-specific interface or base class. Spring』s data binding is highly flexible: for example, it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. Thus you need not duplicate your business objects" properties as simple, untyped strings in your form objects simply to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects.

Spring』s view resolution is extremely flexible. A Controller is typically responsible for preparing a model Map with data and selecting a view name but it can also write directly to the response stream and complete the request. View name resolution is highly configurable through file extension or Accept header content type negotiation, through bean names, a properties file, or even a custom ViewResolver implementation. The model (the M in MVC) is a Map interface, which allows for the complete abstraction of the view technology. You can integrate directly with template based rendering technologies such as JSP, Velocity and Freemarker, or directly generate XML, JSON, Atom, and many other types of content. The model Map is simply transformed into an appropriate format, such as JSP request attributes, a Velocity template model.


子容器和父容器


spring : ioc 容器 spring mvc : 就是servlet 控制器 同類的有struts2


Spring 框架是一個分層架構,由 7 個定義良好的模塊組成,而Spring MVC是其中的一個模塊。

MVC框架是一個全功能的構建 Web應用程序的 MVC 實現,通過策略介面,MVC框架變成為高度可配置的,MVC 容納了大量視圖技術,其中包括 JSP、Velocity、Tiles、iText 和 POI。


Spring 是是一個開源框架,是為了解決企業應用程序開發,功能如下:

目的:解決企業應用開發的複雜性;

功能:使用基本的JavaBean代替EJB,並提供了更多的企業應用功能;

範圍:任何Java應用;

簡單來說,Spring是一個輕量級的控制反轉(IoC)和面向切面(AOP)的容器框架。

輕量——從大小與開銷兩方面而言Spring都是輕量的。完整的Spring框架可以在一個大小只有1MB多的JAR文件里發布。並且Spring所需的處理開銷也是微不足道的。此外,Spring是非侵入式的:典型地,Spring應用中的對象不依賴於Spring的特定類。

控制反轉——Spring通過一種稱作控制反轉(IoC)的技術促進了松耦合。當應用了IoC,一個對象依賴的其它對象會通過被動的方式傳遞進來,而不是這個對象自己創建或者查找依賴對象。你可以認為IoC與JNDI相反——不是對象從容器中查找依賴,而是容器在對象初始化時不等對象請求就主動將依賴傳遞給它。

面向切面——Spring提供了面向切面編程的豐富支持,允許通過分離應用的業務邏輯與系統級服務(例如審計(auditing)和事務(transaction)管理)進行內聚性的開發。應用對象只實現它們應該做的——完成業務邏輯——僅此而已。它們並不負責(甚至是意識)其它的系統級關注點,例如日誌或事務支持。

容器——Spring包含並管理應用對象的配置和生命周期,在這個意義上它是一種容器,你可以配置你的每個bean如何被創建——基於一個可配置原型(prototype),你的bean可以創建一個單獨的實例或者每次需要時都生成一個新的實例——以及它們是如何相互關聯的。然而,Spring不應該被混同於傳統的重量級的EJB容器,它們經常是龐大與笨重的,難以使用。

框架——Spring可以將簡單的組件配置、組合成為複雜的應用。在Spring中,應用對象被聲明式地組合,典型地是在一個XML文件里。Spring也提供了很多基礎功能(事務管理、持久化框架集成等等),將應用邏輯的開發留給了你。

所有Spring的這些特徵使你能夠編寫更乾淨、更可管理、並且更易於測試的代碼。它們也為Spring中的各種模塊提供了基礎支持。

Spring的兩大核心AOP與IOC,可以單獨用於任何應用,包括與Struts等MVC框架與Hibernate等ORM框架的集成,目前很多公司所謂的輕量級開發就是用 Spring + Struts(2)+Hibernate。

Spring MVC就是一個MVC框架,個人覺得Spring MVC annotation式的開發比Struts2方便,可以直接代替上面的Struts(當然Struts的做為一個非常成熟的MVC,功能上感覺還是比Spring強一點,不過Spring MVC已經足夠用了)。當然spring mvc的執行效率比struts高,是因為struts的值棧影響效率。spring mvc類似於struts的一個MVC開框架,其實都是屬於spring,spring mvc需要有spring的架包作為支撐才能跑起來。


spring 框架有超多的延伸產品例如 boot security jpa etc... 但它的基礎就是 spring 的 ioc 和 aop ioc 提供了依賴注入的容器 aop 解決了面向橫切面的編程 然後在此兩者的基礎上實現了其他延伸產品的高級功能 Spring MVC 呢是基於 Servlet 的一個 MVC 框架 主要解決 WEB 開發的問題 因為 Spring 的配置太複雜了 各種 XML JavaConfig hin 麻煩 於是懶人改變世界推出了 Spring boot 約定優於配置 簡化了 spring 的配置流程。


Spring是一個輕量級的控制反轉(IoC)和面向切面(AOP)的容器框架。

Spring + Struts(2)+Hibernate。

Spring MVC類似於struts的一個MVC開框架,其實都是屬於Spring ,Spring MVC需要有Spring 的架包作為支撐才能跑起來。

學習學習。我重新打了一遍。還是有點亂,新手


spring容器,好比人的身體,mvc就是手了,至於其他組件自己yy


學習了


推薦閱讀:

@Autowired和@Resource的區別是什麼?

TAG:Java | Spring | SpringMVC框架 |