博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
what is spring and what is spring for
阅读量:6834 次
发布时间:2019-06-26

本文共 2918 字,大约阅读时间需要 9 分钟。

1 what is spring

spring是一个轻量级的容器。

它使用依赖注入技术来构建耦合性很低的系统。

2 what is  spring for

用于系统的依赖解耦合。在一个系统中,A类依赖于B类,那么我在使用A类的时候,需要先用B来创建一个对象,然后再创建A类的对象。如果现在我换了一个C类,A类依赖于C类,那么我得修改这段使用代码,这是违背OCP的closed原则的。我扩展的时候,不要去修改已有的代码。这个问题该怎么解决呢?spring提供了一个很好的方法。这种依赖关系只需要在xml文件中体现就可以了,不需要显式的创建这个对象,这个对象的创建交给spring,所以称它为一个容器。

3 例子,引用于"http://stackoverflow.com/questions/1061717/what-exactly-is-spring-framework-for"

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

And maybe an implementation accessing a database to get all the users:

public class UserListerDB implements UserLister {    public List
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
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:

This way when the view is created it magically will have a UserLister ready to work.

List
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. states: "The application controls the framework, not the framework controls the application".

转载于:https://www.cnblogs.com/hustdc/p/6443872.html

你可能感兴趣的文章
前端面试题2
查看>>
linux 条件
查看>>
配置JAVA环境
查看>>
hdu5666 BestCoder Round #80
查看>>
linux下备份mysql命令
查看>>
mybatis由浅入深day02_4多对多查询_多对多查询总结
查看>>
关于Android连载(3)Android程序开发指南【转载】
查看>>
leetcode532
查看>>
架构设计(二)
查看>>
表单验证提交内容不能为空的几种方法
查看>>
QT项目性能调优小记
查看>>
通过button将form表单的数据提交到action层
查看>>
九度 1357 疯狂地Jobdu序列
查看>>
接口和抽象类对比
查看>>
memcached 常用命令及使用说明
查看>>
双链表
查看>>
.NET基础——数组
查看>>
解决 android 高低版本 webView 里内容 自适应屏幕的终极方法
查看>>
调用微信截图功能c# 截图带扩展名
查看>>
AC日记——830A - Office Keys
查看>>