我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。


当前回答

我认为这有两部分:

"What exactly is Spring for" -> see the accepted answer by victor hugo. "[...] Spring is [a] good framework for web development" -> people saying this are talking about Spring MVC. Spring MVC is one of the many parts of Spring, and is a web framework making use of the general features of Spring, like dependency injection. It's a pretty generic framework in that it is very configurable: you can use different db layers (Hibernate, iBatis, plain JDBC), different view layers (JSP, Velocity, Freemarker...)

注意,不使用Spring MVC,你也可以在web应用程序中很好地使用Spring。我想说,大多数Java web应用程序都这样做,同时使用其他web框架,如Wicket, Struts, Seam,…

其他回答

我认为这有两部分:

"What exactly is Spring for" -> see the accepted answer by victor hugo. "[...] Spring is [a] good framework for web development" -> people saying this are talking about Spring MVC. Spring MVC is one of the many parts of Spring, and is a web framework making use of the general features of Spring, like dependency injection. It's a pretty generic framework in that it is very configurable: you can use different db layers (Hibernate, iBatis, plain JDBC), different view layers (JSP, Velocity, Freemarker...)

注意,不使用Spring MVC,你也可以在web应用程序中很好地使用Spring。我想说,大多数Java web应用程序都这样做,同时使用其他web框架,如Wicket, Struts, Seam,…

简而言之,我认为Spring是应用程序中的“粘合剂”。它用于集成不同的框架和您自己的代码。

过去,Spring只是一个依赖注入框架(Guice, PicoContainer,…),但现在它是构建企业应用程序的完整解决方案。

spring依赖注入,当然,spring的核心仍然在那里(你可以在这里查看其他好的答案),但是还有更多来自spring的答案……

Spring现在有很多项目,每个项目都有一些子项目(http://spring.io/projects)。当有人谈到spring时,你一定要弄清楚他说的是什么spring项目,是仅仅是spring核心,也就是spring框架,还是另一个spring项目。

值得一提的春季项目有:

Spring Security - http://projects.spring.io/spring-security/ Spring Webservices - http://projects.spring.io/spring-ws/ Spring集成- http://projects.spring.io/spring-integration/

如果你的应用程序需要一些更指定的功能,你也可以在那里找到:

Spring批处理框架设计,使批处理能够开发 批处理应用程序 Spring HATEOAS基于HATEOAS原理轻松创建REST API Spring Mobile和Spring Andriod用于移动应用程序开发 Spring Shell构建一个功能齐全的Shell(又名命令行)应用程序 Spring Cloud和Spring Cloud Data Flow用于云应用程序

还有一些小项目,比如spring-social-facebook (http://projects.spring.io/spring-social-facebook/)

你可以使用spring进行web开发,因为它有spring MVC模块,这是spring框架项目的一部分。或者你可以将spring与另一个web框架一起使用,比如struts2。

Spring一开始是依赖注入,然后为几乎所有的东西添加了包装之王(JPA实现的包装等)。

说来话长……Spring的大多数部分更倾向于XML解决方案(XML脚本引擎…brrrr),所以对于DI,我使用Guice

很好的库,但是随着依赖性的增长,例如Spring JDBC(可能是一个具有实名参数的Java JDBC解决方案)将从maven 4-5继承。

使用Spring MVC(“big Spring”的一部分)进行web开发…它是“基于请求的”框架,有“请求vs组件”的圣战…由你决定

春天有三样东西。

Spring handles Dependency Injection and I recommend you read Martin Fowler's excellent introduction on dependency injection. The second thing Spring does is wrap excellent Java libraries in a very elegant way to use in your applications. For a good example see how Spring wraps Task Executors and Quartz Scheduler. Thirdly Spring provides a bunch of implementations of web stuff like REST, an MVC web framework and more. They figure since you are using Spring for the first two, maybe you can just use it for everything your web app needs.

The problem is that Spring DI is really well thought out, the wrappers around other things are really well thought out in that the other things thought everything out and Spring just nicely wraps it. The Spring implementations of MVC and REST and all the other stuff is not as well done (YMMV, IMHO) but there are exceptions (Spring Security is da bomb). So I tend to use Spring for DI, and its cool wrappers but prefer other stuff for Web (I like Tapestry a lot), REST (Jersey is really robust), etc.