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


当前回答

其优点是依赖注入(DI)。这意味着将对象创建任务外包出去。让我用一个例子来解释。

public interface Lunch
{
   public void eat();
}

public class Buffet implements Lunch
{
   public void eat()
   {
      // Eat as much as you can 
   }
}

public class Plated implements Lunch
{
   public void eat()
   {
      // Eat a limited portion
   }
}

现在在我的代码中,我有一个类LunchDecide,如下所示:

public class LunchDecide {
    private Lunch todaysLunch;
    public LunchDecide(){
        this.todaysLunch = new Buffet(); // choose Buffet -> eat as much as you want
        //this.todaysLunch = new Plated(); // choose Plated -> eat a limited portion 
    }
}

在上面的类中,根据心情选择Buffet()或Plated()。但是这个系统是紧密耦合的。每次需要不同类型的对象时,我们都需要更改代码。在本例中,注释掉一行!假设有50个不同的人使用50个不同的类。那会是一团乱麻。在这种情况下,我们需要解耦系统。让我们重写LunchDecide类。

public class LunchDecide {
    private Lunch todaysLunch;
    public LunchDecide(Lunch todaysLunch){
        this.todaysLunch = todaysLunch
        }
    }

Notice that instead of creating an object using new keyword we passed the reference to an object of Lunch Type as a parameter to our constructor. Here, object creation is outsourced. This code can be wired either using Xml config file (legacy) or Java Annotations (modern). Either way, the decision on which Type of object would be created would be done there during runtime. An object would be injected by Xml into our code - Our Code is dependent on Xml for that job. Hence, Dependency Injection (DI). DI not only helps in making our system loosely coupled, it simplifies writing of Unit tests since it allows dependencies to be mocked. Last but not the least, DI streamlines Aspect Oriented Programming (AOP) which leads to further decoupling and increase of modularity. Also note that above DI is Constructor Injection. DI can be done by Setter Injection as well - same plain old setter method from encapsulation.

其他回答

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

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

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

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

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

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,但绝对值得一看。

我认为这有两部分:

"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,…

你可能想在一个web应用程序中使用Spring -

Spring MVC, which with 2.5+ allows you to use POJOs as Controller classes, meaning you don't have to extend from any particular framework (as in Struts or Spring pre-2.5). Controller classes are also dead simple to test thanks in part to dependency injection Spring integration with Hibernate, which does a good job of simplifying work with that ORM solution (for most cases) Using Spring for a web app enables you to use your Domain Objects at all levels of the application - the same classes that are mapped using Hibernate are the classes you use as "form beans." By nature, this will lead to a more robust domain model, in part because it's going to cut down on the number of classes. Spring form tags make it easier to create forms without much hassle.

此外,Spring是巨大的——所以在web应用程序中使用很多其他的东西你可能会感兴趣,比如Spring AOP或Spring Security。但上面列出的四件事描述了Spring在web应用程序中使用的常见组件。