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


当前回答

公认的答案不涉及注释的使用,因为Spring为配置引入了对各种注释的支持。

Spring注释方法(依赖注入)

还有另一种使用XML文件连接类的方法:注释。让我们使用接受的答案中的示例,并使用@Component、@Service、@Repository或@Configuration注释之一直接在类上注册bean:

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

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

上面的语句是有效的,它不需要使用任何XML文件,并使用另一个注释@Autowired来查找相关的实现并将其注入其中。

@Autowired
private UserLister userLister;

在用于获取要注入的bean实现的方法上使用@Bean注释。

其他回答

Spring框架绝对适合web开发,尤其适合restful api服务。

这是因为它的依赖注入和与其他模块的集成,如spring security、spring aop、mvc框架、微服务

在任何应用程序中,安全性都很可能是一个需求。 如果您的目标是构建一个需要长期维护的产品,那么您将需要利用Aop的概念。 如果你的应用程序有太多的流量,从而增加了负载,你需要使用微服务的概念。

Spring在一个平台中提供了所有这些特性。支持多个模块。 最重要的是,spring是开源的和可扩展的框架,在生命周期中到处都有钩子来集成自定义代码。

Spring Data是一个提供与您的项目集成的项目。

所以弹簧几乎可以满足所有的需求。

我认为这有两部分:

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

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

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.

基本上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书籍。