what is spring and what is spring for
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<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 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".
what is spring and what is spring for的更多相关文章
- spring boot(五):spring data jpa的使用
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
- [Spring]支持注解的Spring调度器
概述 如果想在Spring中使用任务调度功能,除了集成调度框架Quartz这种方式,也可以使用Spring自己的调度任务框架. 使用Spring的调度框架,优点是:支持注解(@Scheduler),可 ...
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块
spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...
- Spring学习1-初识Spring
一.简介 1.Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发. 2.为何要使用Spring? ...
- Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...
- Spring 读书笔记-----使用Spring容器(一)
pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...
- Spring实战1:Spring初探
主要内容 Spring的使命--简化Java开发 Spring容器 Spring的整体架构 Spring的新发展 现在的Java程序员赶上了好时候.在将近20年的历史中,Java的发展历经沉浮.尽管有 ...
- 通过spring.net中的spring.caching CacheResult实现memcached缓存
通过spring.net中的spring.caching CacheResult实现memcached缓存1.SpringMemcachedCache.cs2.APP.config3.Program. ...
随机推荐
- workflow engine Ruote初体验之二(通用属性)
罗列一下表达式所支持的属性: :timeout :if/ unless :forget :lose :flank :on_error :on_cancel :on_timeout :tag :filt ...
- JavaScript 深克隆
深克隆 function judgeType(arg){//判断js数据类型 return Object.prototype.toString.call(arg).slice(8,-1); } fun ...
- CSS3的box-shadow
基础说明: 外阴影:box-shadow: X轴 Y轴 Rpx color; 属性说明(顺序依次对应): 阴影的X轴(可以使用负值) 阴影的Y轴(可以使用负值) 阴影 ...
- eclipse中mybatis generator插件的安装与使用,实现自动生成代码
git地址:https://github.com/mybatis/generator 下载后解压: 选择任意一个版本的jar放到eclipse的features目录下即可 选择任意一个版本的jar放到 ...
- go--互斥锁
解读: main函数里调用了两次lockPrint方法,这个方法中的println(i, "in lock")这句话,由于是在Mutex的Lock和Unlock之间,所以在第一次调 ...
- 模拟登陆web微信的流程和参数细节
这几天在用python写了一个模拟登陆web微信,发送和接受信息的程序.发现步骤不多,但需要的参数太多了 整个过程中,务必保证session.headers.cookie一致,不然的话,中间会出现登陆 ...
- CSS 属性选择器的深入挖掘
CSS 属性选择器,可以通过已经存在的属性名或属性值匹配元素. 属性选择器是在 CSS2 中引入的并且在 CSS3 中得到了很好拓展.本文将会比较全面的介绍属性选择器,尽可能的去挖掘这个选择器在不同场 ...
- 自学MVC看这里——全网最全ASP.NET MVC 教程汇总【转】
自学MVC看这里——全网最全ASP.NET MVC 教程汇总 http://www.cnblogs.com/powertoolsteam/archive/2015/08/13/4667892.html ...
- yii2操作数据库 mysql 读写分离 主从复制
转载地址:http://www.kuitao8.com/20150115/3471.shtml 开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的&quo ...
- 习科小D整理找网站后台办法[科普]
习科科普贴,如何找到网站的后台 作者:小Dの马甲来自:习科论坛 - Silic.Org - BlackBap.Org 1, 穷举猜解 现如今可以暴力猜解网站后台登陆地址的软件有很多,从最早的啊D ...