我听说了很多关于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.

其他回答

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

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 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.

与J2EE相比,Spring是一个轻量级且灵活的框架。 弹簧容器充当了控制的反转。 Spring使用AOP,即代理和单例、工厂和模板方法设计模式。 分层架构:分离关注点和可重用层,易于维护。

基本上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一开始是依赖注入,然后为几乎所有的东西添加了包装之王(JPA实现的包装等)。

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

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

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