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


当前回答

春天有三样东西。

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.

其他回答

Spring非常适合将类实例粘合在一起。你知道你的Hibernate类总是需要一个数据源,Spring将它们连接在一起(也有一个数据源的实现)。

数据访问对象总是需要Hibernate访问,Spring将Hibernate类连接到dao中。

此外,Spring基本上为您提供了一系列库的可靠配置,并指导您应该使用哪些库。

Spring真的是一个很棒的工具。(我说的不是Spring MVC,只是基本框架)。

春天有三样东西。

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.

Spring是Enterprise JavaBeans (EJB)技术的一个很好的替代方案。它还具有web框架和web服务框架组件。

Spring最初是一个相当简单的依赖注入系统。现在它很大,里面什么都有(除了众所周知的厨房水槽)。

但不用担心,它是模块化的,所以你可以只使用你想要的部件。

去看看一切从哪里开始尝试:

http://www.amazon.com/Expert-One-Design-Development-Programmer/dp/0764543857/ref=sr_1_1?ie=UTF8&s=books&qid=1246374863&sr=1-1

它可能很旧,但却是一本好书。

关于另一本关于春天的好书,请看:

http://www.amazon.com/Professional-Java-Development-Spring-Framework/dp/0764574833/ref=sr_1_2?ie=UTF8&s=books&qid=1246374863&sr=1-2

它还引用了较老版本的Spring,但绝对值得一看。

Spring包含(正如Skaffman正确指出的那样)MVC框架。简而言之,这里是我的输入。 Spring支持服务层、web层和业务层的分离,但它最擅长的是对象的“注入”。用一个例子解释一下,请看下面的例子:

public interface FourWheel
{
   public void drive();
}

public class Sedan implements FourWheel
{
   public void drive()
   {
      //drive gracefully
   }
}

public class SUV implements FourWheel
{
   public void drive()
   {
      //Rule the rough terrain
   }
}

现在,在代码中有一个名为RoadTrip的类,如下所示

public class RoadTrip
{
    private FourWheel myCarForTrip;
}

现在,无论何时你想要一个Trip实例;有时你可能需要一个SUV来初始化FourWheel,有时你可能需要一个Sedan。这真的取决于你想要什么基于具体的情况。

为了解决这个问题,你需要一个工厂模式作为创建模式。其中工厂返回正确的实例。所以最终你会用大量的粘合代码来正确地实例化对象。在没有粘合代码的情况下,Spring可以最好地完成粘合代码的工作。您用XML声明映射,它会自动初始化对象。它还为实例使用了大量的单例架构,这有助于优化内存使用。

这也被称为控制反转。其他可以做到这一点的框架有谷歌guice, Pico container等。

除此之外,Spring还有验证框架,广泛支持与JDBC、iBatis和Hibernate(以及更多)合作的DAO层。对数据库事务提供出色的事务控制。

还有很多关于Spring的东西可以在像“Pro Spring”这样的好书中读到。

跟踪url可能也有帮助。 http://static.springframework.org/docs/Spring-MVC-step-by-step/ http://en.wikipedia.org/wiki/Spring_Framework http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework