Spring框架到底是什么? [英] What exactly is Spring Framework for?

查看:95
本文介绍了Spring框架到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说很多关于 Spring 的消息,人们都在网络上说Spring是一个很好的web开发框架。 Spring框架到底是什么?我如何将它用于我的Web-Java应用程序开发?任何示例?

解决方案

基本上Spring是一个依赖注入,这是一种允许构建非常分离的系统的模式。



问题



例如,假设您需要列出系统的用户并声明一个名为 UserLister的接口

  public interface UserLister {
List< User> getUsers();
}

也许一个实现访问数据库来获取所有用户:

  public class UserListerDB实现UserLister {
public List< User> getUsers(){
//数据库访问代码
}
}



  public class SomeView { 
private UserLister userLister;

public void render(){
List< User> users = userLister.getUsers();
view.render(users);






$ b

请注意,上面的代码没有初始化变量 userLister 。我们应该做什么?如果我像这样显式实例化对象:

  UserLister userLister = new UserListerDB(); 

...我将视图与我的实现访问数据库的类相耦合。如果我想从数据库实现切换到另一个从用逗号分隔的文件获取用户列表的东西(请记住,这是一个例子)?在这种情况下,我会再次访问我的代码并更改最后一行:

  UserLister userLister = new UserListerCommaSeparatedFile(); 

这对于像这样的小程序没有问题,但是......在一个程序中发生了什么数百个观点和类似数量的商业课程。维护变成了一场噩梦!

Spring(依赖注入)方法



Spring所做的是通过使用XML文件来连接类,这样所有的对象都被实例化,并且在正确的位置(Servlets,Web框架,业务类,DAO等等)被Spring和注入初始化。等等等)。



回到Spring的例子,我们只需要为 userLister 字段并且具有这样的XML:


$ b

 < bean id =userLister class =UserListerDB/> 

< bean class =SomeView>
< property name =userListerref =userLister/>
< / bean>

通过这种方式创建视图时,它会 c $ c> UserLister 准备工作。

 列表< User> users = userLister.getUsers(); //这将实际上工作
//不添加任何代码行

这很棒!是不是?




  • 如果您想要使用 UserLister interface?只需更改XML

  • 如果没有 UserLister 实现准备好吗编写 UserLister 的临时模拟实现并简化视图的开发

  • 如果我不想再使用Spring?只是不要使用它!你的应用程序没有耦合到它。 控制反转指出:应用程序控制框架,而不是框架控制应用程序。



依赖注入还有其他一些选择,除了它的简单性,优雅性和稳定性之外,我认为Spring让它如此着名。 SpringSource的编程人员已经编写了许多许多POJO,这些POJO有助于将Spring与许多其他常见框架集成,而不会在应用程序中造成干扰。另外,Spring还有几个很好的子项目,比如Spring MVC,Spring WebFlow,Spring Security,还有一些其他的等等。



希望这有帮助。无论如何,我鼓励你阅读关于依赖注入和控制反转的 Martin Fowler的文章,因为他这么做了比我好。 了解基础知识后,请查看 Spring Documentation a> ,在我看来 曾经是有史以来最好的Spring书。


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? How can I use it for my Web-Java application development? any examples?

解决方案

Basically Spring is a framework for which is a pattern that allows to build very decoupled systems.

The problem

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);
    }
}

Note that the code above doesn't have 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 last line by:

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 (Dependency Injection) approach

What Spring does is to wire the classes up by using a XML file, 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...).

Going back to the example in Spring we just need to have a setter for the userLister field and have an XML like this:

<bean id="userLister" class="UserListerDB" />

<bean class="SomeView">
    <property name="userLister" ref="userLister" />
</bean>

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

It is great! Isn't it?

  • 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 of UserLister 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".

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.

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 to Spring Documentation, in my opinion it is used to be the best Spring book ever.

这篇关于Spring框架到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆