spring系列——简介

1、spring 架构图

spring系列——简介_第1张图片

spring IoC:IoC是控制反转,以前java应用中,对象之间的关系是由代码直接定义的(应用来管理),现在交给IoC容器来管理,因此叫控制反转,我们只需要将类定义好,至于类的实例化,以及对象的管理,都交给IoC去管理。举个例子:对象a 要调用对象b,原本是对象a代码里直接写代码去调用,但是IoC不是,对象a和对象b都被IoC容器管理着,容器会将b对象的引用传递给对象a。所以控制反转是一个对象如何获取它所依赖的对象的引用。

spring AOP:面向切面编程。通过AOP声明的方式,声明一个代码段插入另一个代码段中的哪个位置(只是声明就能起作用,而不是将代码段写进另一个代码段),然后执行的时候,就能按插入的顺序来执行代码了。直接举个例子:如果我们代码都写完了,这时却需要添加一段代码 a 在 已经写好的代码 b 之前去执行,要么我们修改已经写好的代码,将a 加到 b 之前,如果我们不修改代码,那么此时AOP就起作用了,可以直接将代码a作一个AOP声明,声明它在代码b之前执行,那么应用程序在运行时,代码a就会在代码b之前运行了。

spring MVC:model 、view 和 controller 三部分。controller:控制层,web请求到后台时,controller将这个请求下放到具体的处理这个请求的方法; model:模型,处理请求的方法中的逻辑代码就是模型,负责处理请求; view:视图,model处理完请求后,可能要返回一些数据给web前端,将要把数据给view,view才能将这些数据处理成展示给用户看的数据。

spring ORM:spring提供的持久化层,提供接口对数据库进行操作。

spring 事务处理:事务这个概念就不作介绍了,提供了一系列的事务功能。

spring 远端调用:spring可以将应用解耦,模块化。这些模块可以分布式地部署,那么模块之间需要通信的,spring恰恰对通信和远端调用作了封装,因此开发者可以不用去深究通信的细节,也可以灵活地使用不同的通信协议等等。

spring 应用:基于spring开发的各种应用,可以形成一个良好的生态圈,相互之间兼容和协作是比较方便的。

2、IoC容器的设计与实现

在spring IoC容器设计中,主要是两类容器:实现BeanFactory接口的基础容器 和  ApplicationContext。

2.1 BeanDefinition 接口

开发者只是定义了开发者角度的bean,但是BeanDefinition 接口还会对bean进行一次抽象封装,封装成IoC角度的bean。

BeanDefinition 接口继承了两个接口 AttributeAccessor 和 BeanMetadataElement

2.1.1 AttributeAccessor 接口——属性访问层

AttributeAccessor接口是用于操作一个属性队列的,整个属性队列是LinkedHashMap类型,属性名-属性值。提供操作整个队列的一些方法:添加、删除、获取、修改、获取所有属性名、一个队列复制到另一个队列、两个属性队列判等、将整个队列哈希成一个哈希值。

spring系列——简介_第2张图片

public interface AttributeAccessor {
    void setAttribute(String var1, @Nullable Object var2);//设置属性,键值对的方式
    @Nullable
    Object getAttribute(String var1);//根据键,获取属性
    @Nullable
    Object removeAttribute(String var1); //根据键,删除属性
    boolean hasAttribute(String var1); //根据键,判断属性是否存在
    String[] attributeNames(); //返回所有键值
}

//看看这个基本的实现类,就知道属性访问机制是怎么回事了
public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable {
  //这是用于存储属性的LinkedHashMap,存储形式是<属性名,属性值>,属性名不能为空,属性值可以为空
    private final Map attributes = new LinkedHashMap();
    public AttributeAccessorSupport() {}
    //实现setAttribute方法,设置属性
    public void setAttribute(String name, @Nullable Object value) {
        Assert.notNull(name, "Name must not be null");//属性名不能为空
        if (value != null) { //存储属性
            this.attributes.put(name, value);
        } else {
            this.removeAttribute(name);
        }
    }
    //根据属性名获取属性值
    @Nullable
    public Object getAttribute(String name) {
        Assert.notNull(name, "Name must not be null");
        return this.attributes.get(name);
    }
    //删除<属性名,属性值>
    @Nullable
    public Object removeAttribute(String name) {
        Assert.notNull(name, "Name must not be null");
        return this.attributes.remove(name);
    }
    //判断属性存储队列中是否存在此属性
    public boolean hasAttribute(String name) {
        Assert.notNull(name, "Name must not be null");
        return this.attributes.containsKey(name);
    }
    //获取所有属性名
    public String[] attributeNames() {
        return StringUtils.toStringArray(this.attributes.keySet());
    }
    //将另外一个属性存储队列source的内容全部复制到此队列中
    protected void copyAttributesFrom(AttributeAccessor source) {
        Assert.notNull(source, "Source must not be null");
        String[] attributeNames = source.attributeNames();
        String[] var3 = attributeNames;
        int var4 = attributeNames.length;
        for(int var5 = 0; var5 < var4; ++var5) {
            String attributeName = var3[var5];
            this.setAttribute(attributeName, source.getAttribute(attributeName));
        }
    }
    //判断两个队列是否相等
    public boolean equals(Object other) {
        return this == other || other instanceof AttributeAccessorSupport && this.attributes.equals(((AttributeAccessorSupport)other).attributes);
    }
    //将整个属性队列哈希成一个哈希值,并返回哈希值
    public int hashCode() {
        return this.attributes.hashCode();
    }
}

 

你可能感兴趣的:(spring)