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