我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。
当前回答
过去,我从纯技术的角度考虑Spring框架。
根据一些团队工作和开发企业web应用程序的经验,我会说Spring通过解耦各个元素(bean)来实现更快的应用程序(web应用程序)开发。更快的发展使它如此受欢迎。Spring允许将构建(连接)应用程序的责任转移到Spring框架上。Spring框架的依赖注入负责将单个bean连接/连接到工作应用程序中。
这样,一旦定义了bean之间的接口,开发人员就可以更专注于单个组件(bean)的开发。
测试这样的应用程序很容易——主要的关注点是单个bean。它们可以很容易地解耦和模拟,因此单元测试是快速和有效的。
Spring框架定义了多个专门的bean,如@Controller (@Restcontroller)、@Repository、@Component来服务于web目的。Spring和Maven一起提供了一个对开发人员来说很直观的结构。 团队工作简单快捷,因为单独的元素是分开的,可以重复使用。
其他回答
春天有三样东西。
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是应用程序中的“粘合剂”。它用于集成不同的框架和您自己的代码。
Spring非常适合将类实例粘合在一起。你知道你的Hibernate类总是需要一个数据源,Spring将它们连接在一起(也有一个数据源的实现)。
数据访问对象总是需要Hibernate访问,Spring将Hibernate类连接到dao中。
此外,Spring基本上为您提供了一系列库的可靠配置,并指导您应该使用哪些库。
Spring真的是一个很棒的工具。(我说的不是Spring MVC,只是基本框架)。
你可能想在一个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应用程序中使用的常见组件。
春天是用来干什么的?我会简短地回答这个问题,但首先,让我们再看看维克多·雨果的例子。这不是一个很好的例子,因为它没有证明需要一个新的框架。
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
推荐文章
- 如何使一个Java通用方法静态?
- for-each循环和迭代器,哪个更有效?
- 泛型类中的静态方法?
- 如何在JPA中持久化类型列表<字符串>的属性?
- 考虑在配置中定义一个'package'类型的bean [Spring-Boot]
- Java注释中的/**和/*
- java8 LocalDate Jackson格式
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 如何在Intellij生成串行版本UID
- “比较法违反其总合同!”
- 从Java项目生成UML类图
- 正确地从一个<Integer>的列表中移除一个整数
- Java开关语句:需要常量表达式,但它是常量
- Java的assertEquals方法可靠吗?
- 如何在Java中获得系统变量值?