我听说了很多关于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 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框架绝对适合web开发,尤其适合restful api服务。

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

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

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

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

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

过去,我从纯技术的角度考虑Spring框架。

根据一些团队工作和开发企业web应用程序的经验,我会说Spring通过解耦各个元素(bean)来实现更快的应用程序(web应用程序)开发。更快的发展使它如此受欢迎。Spring允许将构建(连接)应用程序的责任转移到Spring框架上。Spring框架的依赖注入负责将单个bean连接/连接到工作应用程序中。

这样,一旦定义了bean之间的接口,开发人员就可以更专注于单个组件(bean)的开发。

测试这样的应用程序很容易——主要的关注点是单个bean。它们可以很容易地解耦和模拟,因此单元测试是快速和有效的。

Spring框架定义了多个专门的bean,如@Controller (@Restcontroller)、@Repository、@Component来服务于web目的。Spring和Maven一起提供了一个对开发人员来说很直观的结构。 团队工作简单快捷,因为单独的元素是分开的,可以重复使用。

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

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

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

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

公认的答案不涉及注释的使用,因为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注释。