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 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".

what is spring and what is spring for的更多相关文章

  1. spring boot(五):spring data jpa的使用

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

  2. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  3. [Spring]支持注解的Spring调度器

    概述 如果想在Spring中使用任务调度功能,除了集成调度框架Quartz这种方式,也可以使用Spring自己的调度任务框架. 使用Spring的调度框架,优点是:支持注解(@Scheduler),可 ...

  4. 使用 Spring Boot 快速构建 Spring 框架应用--转

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...

  5. 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架构模式,可 ...

  6. Spring学习1-初识Spring

    一.简介   1.Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发.  2.为何要使用Spring?   ...

  7. Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能

    由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...

  8. Spring 读书笔记-----使用Spring容器(一)

    pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...

  9. Spring实战1:Spring初探

    主要内容 Spring的使命--简化Java开发 Spring容器 Spring的整体架构 Spring的新发展 现在的Java程序员赶上了好时候.在将近20年的历史中,Java的发展历经沉浮.尽管有 ...

  10. 通过spring.net中的spring.caching CacheResult实现memcached缓存

    通过spring.net中的spring.caching CacheResult实现memcached缓存1.SpringMemcachedCache.cs2.APP.config3.Program. ...

随机推荐

  1. Codeforces 746G(构造)

                                                                                                      G. ...

  2. 10.1综合强化刷题 Day5

    T1 拼不出的数 lost.in/.out/.cpp[问题描述]3 个元素的集合{5; 1; 2}的所有子集的和分别是0; 1; 2; 3; 5; 6; 7; 8.发现最小的不能由该集合子集拼出的数字 ...

  3. TOYS-POJ2318

    本题主要是确定给定的点在那块区域.原题给出n条直线,将长方形分为n+1快区域.我们可以对每个给定的点来判断它在那块区域,判段方法可以根据点与直线的位置关系,具体如下,对于点(x0,y0)和直线ax+b ...

  4. supervisor开机自启动方法

    配置service类型服务 #!/bin/bash # # supervisord This scripts turns supervisord on # # Author: Mike McGrath ...

  5. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之静态页面

    一.引言 接着上一篇,京东个人中心的所有功能数据分析完成之后,现在需要把静态页面完成,实现过程中要用到的技术有:Bootstrap.html5表单新特性等.除此之外,还要利用Node.js的Expre ...

  6. json lib 2.4及其依赖包下载

    下载文件地址:https://files.cnblogs.com/files/xiandedanteng/json-lib-2.4%26dependencies_jars.rar 它包括 common ...

  7. 微信小程序 - 答题进度条

    关于进度条的话,我是使用官方原生的progress的. 关于进度progress接受保留2位小数(从后端获取到平均值,再item循环出来) js wxml

  8. mysql莫名的主键重复

    REPAIR TABLE t_car_type; OPTIMIZE TABLE t_car_type; 可解决问题

  9. servlet实现文件下载所需步骤及说明

      servlet实现文件下载所需步骤及说明 CreateTime--2017年9月1日15:46:22 Author:Marydon 参考链接:http://blog.sina.com.cn/s/b ...

  10. No bean named &#39;cxf&#39; is defined

    1.错误描写叙述  严重:Exception starting filter CXFServlet        org.springframework.beans.factory.NoSuchBea ...