问题描述
我听到很多关于 Spring 的消息,人们都在说 Spring 是一个很好的 Web 开发框架.简而言之,Spring Framework 到底是什么?我为什么要在纯 Java 上使用它.
I hear a lot about Spring, people are saying all over the web that Spring is a good framework for web development. What exactly is Spring Framework for in a nutshell? Why should I used it over just plain Java.
推荐答案
基本上 Spring 是一个框架,用于 dependency-injection 这是一种允许构建非常解耦的系统的模式.
Basically Spring is a framework for dependency-injection which is a pattern that allows building very decoupled systems.
例如,假设您需要列出系统的用户并因此声明一个名为UserLister
的接口:
For example, suppose you need to list the users of the system and thus declare an interface called UserLister
:
public interface UserLister {
List<User> getUsers();
}
也许还有一个访问数据库以获取所有用户的实现:
And maybe an implementation accessing a database to get all the users:
public class UserListerDB implements UserLister {
public List<User> getUsers() {
// DB access code here
}
}
在您看来,您需要访问一个实例(只是一个示例,请记住):
In your view you'll need to access an instance (just an example, remember):
public class SomeView {
private UserLister userLister;
public void render() {
List<User> users = userLister.getUsers();
view.render(users);
}
}
请注意,上面的代码没有初始化变量userLister
.我们应该做什么?如果我像这样显式实例化对象:
Note that the code above hasn't initialized the variable userLister
. What should we do? If I explicitly instantiate the object like this:
UserLister userLister = new UserListerDB();
...我将视图与访问数据库的类的实现结合起来.如果我想从数据库实现切换到另一个从逗号分隔文件中获取用户列表的实现(记住,这是一个示例),该怎么办?在这种情况下,我会再次转到我的代码并将上面的行更改为:
...I'd couple the view with my implementation of the class that access the DB. What if I want to switch from the DB implementation to another that gets the user list from a comma-separated file (remember, it's an example)? In that case, I would go to my code again and change the above line to:
UserLister userLister = new UserListerCommaSeparatedFile();
这样的小程序没有问题,但是......在一个有数百个视图和类似数量的业务类的程序中会发生什么?维护成为一场噩梦!
This has no problem with a small program like this but... What happens in a program that has hundreds of views and a similar number of business classes? The maintenance becomes a nightmare!
Spring 所做的是通过使用 XML 文件或注释将类连接,这样所有对象都由 Spring 实例化和初始化,并在右侧 注入位置(Servlet、Web 框架、业务类、DAO 等,等等……).
What Spring does is to wire the classes up by using an XML file or annotations, this way all the objects are instantiated and initialized by Spring and injected in the right places (Servlets, Web Frameworks, Business classes, DAOs, etc, etc, etc...).
回到 Spring 中的示例,我们只需要为 userLister
字段设置一个设置器,并拥有一个像这样的 XML 文件:
Going back to the example in Spring we just need to have a setter for the userLister
field and have either an XML file like this:
<bean id="userLister" class="UserListerDB" />
<bean class="SomeView">
<property name="userLister" ref="userLister" />
</bean>
或者更简单地用 @Inject
注释我们视图类中的文件:
or more simply annotate the filed in our view class with @Inject
:
@Inject
private UserLister userLister;
这样,当视图被创建时神奇地就会有一个UserLister
准备好工作.
This way when the view is created it magically will have a UserLister
ready to work.
List<User> users = userLister.getUsers(); // This will actually work
// without adding any line of code
太棒了!不是吗?
- 如果您想使用
UserLister
接口的其他实现怎么办?只需更改 XML. - 如果没有准备好
UserLister
实现怎么办? 编写UserLister
的临时模拟实现并简化视图的开发. - 如果我不想再使用 Spring 怎么办? 不要使用它!您的应用程序没有耦合到它.控制反转 声明:应用程序控制框架,而不是框架控制应用程序".
- What if you want to use another implementation of your
UserLister
interface? Just change the XML. - What if don't have a
UserLister
implementation ready? Program a temporal mock implementation ofUserLister
and ease the development of the view. - What if I don't want to use Spring anymore? Just don't use it! Your application isn't coupled to it. Inversion of Control states: "The application controls the framework, not the framework controls the application".
还有一些其他的依赖注入选项,在我看来,除了简单、优雅和稳定之外,Spring 如此出名的原因是 SpringSource 的人编写了许多 POJO,有助于将 Spring 与许多其他常见的集成框架,而不会侵入您的应用程序.此外,Spring 有几个不错的子项目,如 Spring MVC、Spring WebFlow、Spring Security 以及一大堆等等.
There are some other options for Dependency Injection around there, what in my opinion has made Spring so famous besides its simplicity, elegance and stability is that the guys of SpringSource have programmed many many POJOs that help to integrate Spring with many other common frameworks without being intrusive in your application. Also, Spring has several good subprojects like Spring MVC, Spring WebFlow, Spring Security and again a loooong list of etceteras.
希望这会有所帮助.无论如何,我鼓励你阅读 Martin Fowler 的关于依赖注入和控制反转的文章,因为他做到了比我好.了解基础知识后看看 Spring 文档,在我看来,它是 曾经是有史以来最好的Spring书籍.
Hope this helps. Anyway, I encourage you to read Martin Fowler's article about Dependency Injection and Inversion of Control because he does it better than me. After understanding the basics take a look at Spring Documentation, in my opinion, it is used to be the best Spring book ever.
这篇关于Spring 框架到底是干什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!