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


当前回答

春天是用来干什么的?我会简短地回答这个问题,但首先,让我们再看看维克多·雨果的例子。这不是一个很好的例子,因为它没有证明需要一个新的框架。

public class BaseView {
  protected UserLister userLister;

  public BaseView() {
    userLister = new UserListerDB(); // only line of code that needs changing
  }
}

public class SomeView extends BaseView {
  public SomeView() {
    super();
  }

  public void render() {
    List<User> users = userLister.getUsers();
    view.render(users);
  }
}

完成了!因此,现在即使您有数百或数千个视图,您仍然只需要更改一行代码,就像Spring XML方法中那样。 但是更改一行代码仍然需要重新编译,而不是编辑XML。好吧,我的挑剔的朋友,使用Ant和脚本!

那么春天是用来干什么的呢?这是为:

盲目的开发者随大流 雇主不想雇佣毕业的程序员,因为他们在大学里没有教过这样的框架 从一个糟糕的设计开始,需要修补的项目(如维克多·雨果的例子所示)

进一步阅读:http://discuss.joelonsoftware.com/?joel.3.219431.12

其他回答

基本上Spring是一个依赖注入框架,这种模式允许构建非常解耦的系统。

这个问题

例如,假设你需要列出系统的用户,并声明一个名为UserLister的接口:

public interface UserLister {
    List<User> getUsers();
}

也许一个实现访问一个数据库来获取所有的用户:

public class UserListerDB implements UserLister {
    public List<User> getUsers() {
        // DB access code here
    }
}

在你的视图中,你需要访问一个实例(只是一个例子,记住):

public class SomeView {
    private UserLister userLister;

    public void render() {
        List<User> users = userLister.getUsers();
        view.render(users);
    }
}

注意,上面的代码没有初始化变量userLister。我们该怎么办?如果我像这样显式地实例化对象:

UserLister userLister = new UserListerDB();

...我将视图与访问DB的类的实现结合在一起。如果我想从DB实现切换到另一个从逗号分隔的文件(记住,这是一个例子)获取用户列表的实现呢?在这种情况下,我将再次转到我的代码,并将上面的行更改为:

UserLister userLister = new UserListerCommaSeparatedFile();

这样的小程序没有问题,但是……在具有数百个视图和类似数量的业务类的程序中会发生什么?维护变成了噩梦!

Spring(依赖注入)方法

Spring所做的是使用XML文件或注释将类连接起来,这样所有的对象都由Spring实例化和初始化,并注入到正确的位置(servlet、Web框架、业务类、dao,等等,等等……)

回到Spring中的例子,我们只需要为userLister字段设置一个setter,并有一个像这样的XML文件:

<bean id="userLister" class="UserListerDB" />

<bean class="SomeView">
    <property name="userLister" ref="userLister" />
</bean>

或者更简单地用@Inject注释视图类中的字段:

@Inject
private UserLister userLister;

这样,当视图创建时,它将神奇地有一个UserLister准备工作。

List<User> users = userLister.getUsers();  // This will actually work
                                           // without adding any line of code

太棒了!不是吗?

如果您想使用UserLister接口的另一种实现呢?只需更改XML。 如果没有UserLister实现呢?编写UserLister的临时模拟实现,简化视图的开发。 如果我不想再使用Spring了怎么办?千万别用!您的应用程序没有与它耦合。控制反转:“应用程序控制框架,而不是框架控制应用程序”。

还有一些其他的依赖注入选项,在我看来,除了简单、优雅和稳定之外,Spring之所以如此有名,是因为SpringSource的人编写了许多pojo,这些pojo有助于将Spring与许多其他通用框架集成在一起,而不会对应用程序造成干扰。此外,Spring还有几个不错的子项目,比如Spring MVC、Spring WebFlow、Spring Security,还有一长串等等。

无论如何,我鼓励你阅读Martin Fowler关于依赖注入和控制反转的文章,因为他在这方面做得比我好。在了解了基本知识之后,看看Spring Documentation,在我看来,它是有史以来最好的Spring书籍。

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

春天有三样东西。

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包含(正如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