设计模式5——自定义Spring框架

1、Spring核心功能结构

Spring大约有20个模块,由1300多个不同的文件构成。这些模块可以分为:核心容器、AOP和设备支持、数据访问与集成、Web组件、通信报文和集成测试等。下面是Spring框架的整体架构图:

核心容器由beans、core、context 和 expression(Spring Expression Language, SpEL)4个模块组成。

spring-beans 和 spring-core 模块是Spring框架的核心模块,包含了控制反转(Inversion of Control, IoC)和依赖注入(Dependency Injection, DI)。BeanFactory使用控制反转对应用程序的配置和依赖性规范与实际的应用程序代码进行了分离。BeanFactory属于延时加载,也就是说在实例化容器对象后并不会自动实例化Bean,只有当Bean被使用时,BeanFactory才会对该Bean进行实例化与依赖关系的装配。

spring-context 模块构架于核心模块之上,扩展了BeanFactory,为它添加了Bean声明周期控制、框架时间体系及资源加载透明化等功能。此外,该模块还提供了许多企业级支持,如邮件访问、远程访问、任务调度等,ApplicationContext是该模块的核心接口,它的超类是BeanFactory。与BeanFactory不同,ApplicationContext实例化后会自动对所有的单实例Bean进行实例化与依赖关系的装配,使之处于待用状态。

spring-context-support 模块是对Spring IoC容器即IoC子容器的扩展支持。

spring-context-indexer 模块是Spring的类管理组件和Classpath扫描组件。

spring-expression 模块是统一表达式语言(EL)的扩展模块,可以查询、管理运行中的对象,同时也可以方便地调用对象方法,以及操作数组、集合等。它的语法类似于传统EL,但提供了额外的功能,最出色的要数函数调用和简单字符串的模板函数。EL的特性是基于Spring产品的需求而设计的,可以非常方便地同Spring IoC进行交互。

1.1、bean概述

Spring就是面向Bean的编程(BOP, Bean Oriented Programming),Bean在Spring中处于核心地位。Bean对于Spring的意义就向Object对于OOP的意义一样,Spring中没有Bean也就没有Spring存在的意义。Spring IoC 容器通过配置文件或者注解的方式来管理bean对象之间的依赖关系。

spring中bean用于对一个类进行封装,如下面的配置:

<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>

为什么Bean如此重要呢?

  • Spring将Bean对象交由一个叫IoC容器进行管理;
  • Bean对象之间的依赖关系在配置文件中体现,并由Spring完成。

2、Spring IoC 相关接口分析

2.1、BeanFactory解析

Spring中Bean的创建是典型的工厂模式,这一系列的Bean工厂,即IoC容器,为开发者管理对象之间的依赖关系提供了很多便利和基础服务,在Spring中有许多IoC容器的实现供用户选择,其相互关系如下图所示:

其中,BeanFactory作为最顶层的一个接口,定义了IoC容器的基本功能规范,BeanFactory有3个重要的子接口:ListableBeanFactory、HierarchicalBeanFactory 和 AutoWireCapableBeanFactory 。但是从类图中我们可以发现最终的默认实现类是 DefaultListableBeanFactory,它实现了所有的接口。

那么为什么要定义这么多层次的接口呢?

每个接口都有它的使用场合,主要是为了区分在Spring内部操作过程中对象的传递和转化,对对象的数据访问所做的限制。例如:

  • ListableBeanFactory接口表示这些Bean可列表化;
  • HierarchicalBeanFactory表示这些Bean是有继承关系的,也就是每个Bean可能有父Bean;
  • AutowireCapableBeanFactory接口定义Bean的自动装配规则。

这3个接口共同定义了Bean的集合、Bean之间的关系及Bean行为。最基本的IoC容器接口是BeanFactory,来看一下它的源码:

public interface BeanFactory {
    String FACTORY_BEAN_PREFIX = "&";

    Object getBean(String var1) throws BeansException;

    <T> T getBean(String var1, Class<T> var2) throws BeansException;

    Object getBean(String var1, Object... var2) throws BeansException;

    <T> T getBean(Class<T> var1) throws BeansException;

    <T> T getBean(Class<T> var1, Object... var2) throws BeansException;

    <T> ObjectProvider<T> getBeanProvider(Class<T> var1);

    <T> ObjectProvider<T> getBeanProvider(ResolvableType var1);

    boolean containsBean(String var1);

    boolean isSingleton(String var1) throws NoSuchBeanDefinitionException;

    boolean isPrototype(String var1) throws NoSuchBeanDefinitionException;

    boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException;

    boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException;

    @Nullable
    Class<?> getType(String var1) throws NoSuchBeanDefinitionException;

    @Nullable
    Class<?> getType(String var1, boolean var2) throws NoSuchBeanDefinitionException;

    String[] getAliases(String var1);
}

在BeanFactory里只对IoC容器的基本行为做了定义,根本不关心你的Bean是如何定义及怎样加载的。正如我们只关心能从工厂里得到什么产品,不关心工厂是怎么生产这些产品的。

BeanFactory有一个很重要的子接口,就是ApplicationContext接口,该接口主要来规范容器中的Bean对象是非延时加载,即在创建容器对象的时候就对Bean进行初始化,并存储到一个容器中。

要知道工厂是如何产生对象的,我们需要看具体的IoC容器实现,Spring提供了许多IoC容器实现,比如:

  • ClasspathXmlApplicationContext:根据类路径加载xml配置文件,并创建IoC容器对象;
  • FileSystemXmlApplicationContext:根据系统路径加载xml配置文件,并创建IoC容器对象;
  • AnnotationConfigApplicationContext:加载注解类配置,并创建IoC容器。

2.2、BeanDefinition解析

Spring IoC容器管理我们定义的各种Bean对象及其相互关系,Bean对象在Spring实现中是以BeanDefinition来描述的,如下面配置文件:

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>

Bean标签还有很多属性:scope、init-method、destroy-method等。

其继承体系如下图所示:

2.3、BeanDefinitionReader解析

Bean的解析过程非常复杂,功能被分得很细,因为这里需要被扩展的地方很多,必须保证足够的灵活性,以应对可能的变化。Bean的解析主要就是对Spring配置文件的解析。这个解析过程主要通过BeanDefinitionReader来完成,看看Spring中BeanDefinitionReader的类结构图,如下图所示:

看看BeanDefinitionReader接口定义的功能来理解它具体的作用:

public interface BeanDefinitionReader {
    // 获取BeanDefinitionRegistry注册表对象
    BeanDefinitionRegistry getRegistry();

    @Nullable
    ResourceLoader getResourceLoader();

    @Nullable
    ClassLoader getBeanClassLoader();

    BeanNameGenerator getBeanNameGenerator();

    // 下面的loadBeanDefinitions都是加载Bean定义,从指定的资源中
    int loadBeanDefinitions(Resource var1) throws BeanDefinitionStoreException;
    int loadBeanDefinitions(Resource... var1) throws BeanDefinitionStoreException;
    int loadBeanDefinitions(String var1) throws BeanDefinitionStoreException;
    int loadBeanDefinitions(String... var1) throws BeanDefinitionStoreException;
}

2.4、BeanDefinitionRegistry解析

BeanDefinitionReader用来解析Bean定义,并封装BeanDefinition对象,而我们定义的配置文件中定义了很多的Bean标签,所以就有一个问题,解析的BeanDefinition对象存储到哪儿?答案就是BeanDefinition的注册中心,而该注册中心顶层接口就是BeanDefinitionRegistry。

public interface BeanDefinitionRegistry extends AliasRegistry {
    // 往注册表中注册Bean
    void registerBeanDefinition(String var1, BeanDefinition var2) throws BeanDefinitionStoreException;
    // 从注册表中删除指定名称的Bean
    void removeBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
    // 获取注册表中指定名称的Bean
    BeanDefinition getBeanDefinition(String var1) throws NoSuchBeanDefinitionException;
    // 判断注册表中是否已经注册了指定名称的Bean
    boolean containsBeanDefinition(String var1);
    // 获取注册表中所有的Bean的名称
    String[] getBeanDefinitionNames();

    int getBeanDefinitionCount();
    boolean isBeanNameInUse(String var1);
}

继承结构图如下:

从上面类图可以看到BeanDefinitionRegistry接口的子实现类主要有以下几个:

1、DefaultListableBeanFactory

在该类中定义了如下代码,就是用来注册Bean:

private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

2、SimpleBeanDefinitionRegistry

在该类中定义了如下代码,就是用来注册Bean:

private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(64);

2.5、创建容器

ClassPathXmlApplicationContext对Bean配置资源的载入是从refresh()方法开始的。refresh()方法是一个模板方法,规定了IoC容器的启动流程,有些逻辑要交给其子类实现。它对Bean配置资源进行载入,ClassPathXmlApplicationContext通过调用其父类AbstractApplicationContext的refresh()方法启动整个Ioc容器对Bean定义的载入过程。

3、自定义SpringIoC

现要对下面的配置文件进行解析,并自定义Spring框架的IoC对涉及到的对象进行管理。

<?xml version="1.0" encoding="UTF-8"?>
<beans>
     <bean id="userService" class="com.itheima.service.impl.userServiceImpl">
         <property name="userDao" ref="userDao"></property>
     </bean>
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
</beans>

3.1、定义Bean相关的POJO类

3.1.1、PropertyValue类

用于封装Bean的属性,体现到上面的配置文件就是封装Bean标签的子标签property标签数据。

// 给基本数据类型及String类型数据赋的值
public class PropertyValue {
    private String name;
    private String ref;
    private String value;

    public PropertyValue() {
    }

    public PropertyValue(String name, String ref, String value) {
        this.name = name;
        this.ref = ref;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

3.1.2、MutablePropertyValues类

一个Bean标签可以有多个property子标签,所以再定义一个MutablePropertyValues类,用来存储并管理多个PropertyValue对象。

// 用来存储和管理多个 PropertyValue对象
public class MutablePropertyValues implements Iterable<PropertyValue> {
    // 定义List集合对象,用来存储PropertyValue对象
    private final List<PropertyValue> propertyValues;

    public MutablePropertyValues() {
        this.propertyValues = new ArrayList<>();
    }

    public MutablePropertyValues(List<PropertyValue> propertyValues) {
        if (propertyValues == null) {
            this.propertyValues = new ArrayList<>();
        } else {
            this.propertyValues = propertyValues;
        }
    }

    // 获取所有的PropertyValue对象,返回以数组的形式
    public PropertyValue[] getPropertyValues() {
        // 将集合转换为数组并返回
        return propertyValues.toArray(new PropertyValue[0]);
    }

    // 根据name属性值获取PropertyValue对象
    public PropertyValue getPropertyValue(String propertyName) {
        // 遍历集合对象
        for (PropertyValue propertyValue : propertyValues) {
            if (propertyValue.getName().equals(propertyName)) {
                return propertyValue;
            }
        }
        return null;
    }

    // 判断集合是否为空
    public boolean isEmpty() {
        return propertyValues.isEmpty();
    }

    // 添加PropertyValue对象
    public MutablePropertyValues addPropertyValue(PropertyValue propertyValue) {
        // 判断集合中存储的PropertyValue对象是否和传递进来的重复了,如果重复了,进行覆盖
        for (int i = 0; i < propertyValues.size(); i++) {
            // 获取集合中每一个PropertyValue对象
            PropertyValue currPv = propertyValues.get(i);
            if (currPv.getName().equals(propertyValue.getName())) {
                propertyValues.set(i, propertyValue);
                return this; // 实现链式编程
            }
        }
        this.propertyValues.add(propertyValue);
        return this;
    }

    // 判断是否有指定name属性值的对象
    public boolean contains(String propertyName) {
        return getPropertyValue(propertyName) != null;
    }

    // 获取迭代器对象
    @Override
    public Iterator<PropertyValue> iterator() {
        return propertyValues.iterator();
    }
}

3.1.3、BeanDefinition类

BeanDefinition类用来封装Bean信息的,主要包含id(即Bean对象的名称)、class(需要交由Spring管理的类的全类名)及子标签property数据。

// 用来封装Bean标签数据
public class BeanDefinition {
    private String id;
    private String className;
    
    private MutablePropertyValues propertyValues;

    public BeanDefinition() {
        propertyValues = new MutablePropertyValues();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public MutablePropertyValues getPropertyValues() {
        return propertyValues;
    }

    public void setPropertyValues(MutablePropertyValues propertyValues) {
        this.propertyValues = propertyValues;
    }
}

3.2、定义注册表相关类

3.2.1、BeanDefinitionRegistry接口

BeanDefinitionRegistry接口定义了注册表的相关操作,定义如下功能:

  • 注册BeanDefinition对象到注册表中;
  • 从注册表中删除指定名称的BeanDefinition对象;
  • 根据名称从注册表中获取BeanDefinition对象;
  • 判断注册表中是否包含指定名称的BeanDefinition对象;
  • 获取注册表中BeanDefinition对象的个数;
  • 获取注册表中所有的BeanDefinition的名称。
public interface BeanDefinitionRegistry {
    // 注册BeanDefinition对象到注册表中
    void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
    
    // 从注册表中删除指定名称的BeanDefinition对象
    void removeBeanDefinition(String beanName) throws Exception;
    
    // 根据名称从注册表中获取BeanDefinition对象
    BeanDefinition getBeanDefinition(String beanName) throws Exception;
    
    boolean containsBeanDefinition(String beanName);
    
    int getBeanDefinitionCount();
    
    String[] getBeanDefinitionNames();
}

3.2.2、SimpleBeanDefinitionRegistry类

该类实现了BeanDefinitionRegistry接口,定义了Map集合作为注册表容器。

// 注册表接口的子实现类
public class SimpleBeanDefinitionRegistry implements BeanDefinitionRegistry {
    // 定义一个容器,用来存储BeanDefinition对象
    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();
    
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    @Override
    public void removeBeanDefinition(String beanName) throws Exception {
        beanDefinitionMap.remove(beanName);
    }

    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws Exception {
        return beanDefinitionMap.get(beanName);
    }

    @Override
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }

    @Override
    public int getBeanDefinitionCount() {
        return beanDefinitionMap.size();
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return beanDefinitionMap.keySet().toArray(new String[0]);
    }
}

3.3、定义解析器相关类

3.3.1、BeanDefinitionReader接口

BeanDefinitionReader是用来解析配置文件并在注册表中注册Bean的信息。定义了2个规范:

  • 获取注册表的功能,让外界可以通过该对象获取注册表对象;
  • 加载配置文件,并注册Bean数据。
public interface BeanDefinitionReader {
    // 获取注册表对象
    BeanDefinitionRegistry getRegistry();
    
    // 加载配置文件并在注册表中进行注册
    void loadBeanDefinitions(String configLocation) throws Exception;
}

3.3.2、XmlBeanDefinitionReader类

解析Xml文件我们引入如下坐标:

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

定义该类:

// 针对 XML 配置文件进行解析的类
public class XmlBeanDefinitionReader implements BeanDefinitionReader {
    // 声明注册表对象
    private BeanDefinitionRegistry registry;

    public XmlBeanDefinitionReader() {
        registry = new SimpleBeanDefinitionRegistry();
    }

    @Override
    public BeanDefinitionRegistry getRegistry() {
        return registry;
    }

    @Override
    public void loadBeanDefinitions(String configLocation) throws Exception {
        // 使用dom4j进行xml配置文件的解析
        SAXReader reader = new SAXReader();
        // 获取类路径下的配置文件
        InputStream iStream = XmlBeanDefinitionReader.class.getClassLoader().getResourceAsStream(configLocation);
        Document document = reader.read(iStream);
        // 根据 Document 对象获取根标签对象
        Element rootElement = document.getRootElement();
        // 获取根标签下所有的bean标签对象
        List<Element> beanElements = rootElement.elements("bean");
        // 遍历集合
        for (Element beanElement : beanElements) {
            // 获取id属性
            String id = beanElement.attributeValue("id");
            // 获取class属性
            String className = beanElement.attributeValue("class");
            // 将id属性和class封装套BeanDefinition对象
            // 创建BeanDefinition
            BeanDefinition beanDefinition = new BeanDefinition();
            beanDefinition.setId(id);
            beanDefinition.setClassName(className);

            // 创建MutablePropertyValues对象
            MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();

            // 获取bean标签下所有的property标签对象
            List<Element> propertyElements = beanElement.elements("property");
            for (Element propertyElement : propertyElements) {
                String name = propertyElement.attributeValue("name");
                String ref = propertyElement.attributeValue("ref");
                String value = propertyElement.attributeValue("value");
                PropertyValue propertyValue = new PropertyValue(name, ref, value);
                mutablePropertyValues.addPropertyValue(propertyValue);
            }

            // 将MutablePropertyValues对象封装到BeanDefinition对象中
            beanDefinition.setPropertyValues(mutablePropertyValues);

            // 将BeanDefinition对象注册到注册表中
            registry.registerBeanDefinition(id, beanDefinition);
        }
    }
}

3.4、IoC容器相关类

3.4.1、BeanFactory接口

在该接口中定义IoC容器的统一规范即获取Bean对象。

// IoC容器父接口
public interface BeanFactory {
    // 根据Bean对象的名称获取Bean对象
    Object getBean(String name) throws Exception;
    
    // 根据Bean对象的名称获取Bean对象,进行类型转换
    <T> T getBean(String name, Class<? extends T> clazz) throws Exception;
}

3.4.2、ApplicationContext接口

该接口的所有的子实现类对Bean对象的创建都是非延时的,所以在该接口中定义refresh()方法,该方法主要完成以下两个功能:

  • 加载配置文件;
  • 根据注册表中的BeanDefinition对象封装的数据进行Bean对象的创建。
// 定义非延时功能
public interface ApplicationContext extends BeanFactory {
    // 进行配置文件加载并进行对象创建
    void refresh() throws Exception;
}

3.4.3、AbstractApplicationContext类

作为ApplicationContext接口的子类,所以该类也是非延时加载,所以需要在该类中定义一个Map集合,作为Bean对象存储的容器。

声明BeanDefinitionReader类型的变量,用来进行xml配置文件的解析,符合单一职责原则。BeanDefinitionReader类型的对象创建交由子类实现,因为只有子类明确到底创建BeanDefinitionReader哪个子实现类对象。

// ApplicationContext接口的子实现类,用于立即加载
public abstract class AbstractApplicationContext implements ApplicationContext {
    // 声明解析器变量(交由子类赋值)
    protected BeanDefinitionReader beanDefinitionReader;
    // 用来存储Bean对象的Map容器,key存储的是bean的id值,value存储的是bean对象
    protected Map<String, Object> singletonObjects = new HashMap<>();
    // 声明配置文件路径的变量
    protected String configLocation;

    @Override
    public void refresh() throws Exception {
        // 加载BeanDefinition对象
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        // 初始化Bean
        finishBeanInitialization();
    }
    
    // Bean的初始化
    private void finishBeanInitialization() throws Exception {
        // 获取注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        // 获取BeanDefinition对象
        String[] beanNames = registry.getBeanDefinitionNames();
        // 进行Bean的初始化
        for (String beanName : beanNames) {
            // 进行Bean的初始化
            getBean(beanName);
        }
    }
}

3.4.4、ClassPathXmlApplicationContext类

该类主要是加载类路径下的配置文件,并进行Bean对象的创建,主要完成以下功能:

  • 在构造方法中,创建BeanDefinitionReader对象;
  • 在构造方法中,调用refresh()方法,用于进行配置文件加载、创建Bean对象并存储到容器中;
  • 重写父接口中的getBean()方法,并实现依赖注入操作。
// IoC容器具体的子实现类,用于加载类路径下的xml格式的配置文件
public class ClassPathXmlApplicationContext extends AbstractApplicationContext {
    public ClassPathXmlApplicationContext(String configLocation) {
        this.configLocation = configLocation;
        // 构建解析器对象
        beanDefinitionReader = new XmlBeanDefinitionReader();
        try {
            super.refresh();
        } catch (Exception e) {
            
        }
    }
    
    // 根据Bean对象的名称获取Bean对象
    @Override
    public Object getBean(String name) throws Exception {
        // 判断对象容器中是否包含指定名称的Bean对象,如果包含,直接返回即可,如果不包含,需要自行创建
        Object obj = singletonObjects.get(name);
        if (obj != null) {
            return obj;
        }
        // 获取BeanDefinition对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        BeanDefinition beanDefinition = registry.getBeanDefinition(name);
        // 获取Bean信息中的ClassName全类名
        String className = beanDefinition.getClassName();
        // 通过反射创建对象
        Class<?> clazz = Class.forName(name);
        Object beanObj = clazz.newInstance();
        
        // 进行依赖注入操作
        MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
        for (PropertyValue propertyValue : propertyValues) {
            // 获取name属性值
            String propertyName = propertyValue.getName();
            // 获取value属性
            String value = propertyValue.getValue();
            // 获取ref属性
            String ref = propertyValue.getRef();
            if (ref != null && !"".equals(ref)) {
                // 获取依赖的Bean对象
                Object bean = getBean(ref);
                // 拼接方法名
                String methodName = StringUtils.getSetterMethodByFieldName(propertyName);
                // 反射获取所有方法对象
                for (Method method : clazz.getMethods()) {
                    if (methodName.equals(method.getName())) {
                        // 执行该set()方法
                        method.invoke(beanObj, bean);
                    }
                }
            }
            if (value != null && !"".equals(value)) {
                // 拼接方法名
                String methodName = StringUtils.getSetterMethodByFieldName(propertyName);
                // 获取method对象
                Method method = clazz.getMethod(methodName, String.class);
                method.invoke(beanObj, value);
            }
        }

        // 在返回beanObj对象之前,将该对象存储到Map容器中
        singletonObjects.put(name, beanObj);
        return beanObj;
    }

    @Override
    public <T> T getBean(String name, Class<? extends T> clazz) throws Exception {
        Object bean = getBean(name);
        if (bean == null) {
            return null;
        }
        return clazz.cast(bean);
    }
}

3.5、测试

public class AppTest {
    @Test
    public void test1() throws Exception {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
        userService.add();
    }
}

3.6、总结

3.6.1、使用到的设计模式

  • 工厂模式。这个使用工厂模式+配置文件的方式。
  • 单例模式。Spring IoC管理的对象都是单例的,此处的单例不是通过构造器进行单例的控制的,而是Spring框架对每一个Bean只创建了一个对象。
  • 模板方法模式。AbstractApplicationContext类中的finishBeanInitialization()方法调用了子类的getBean()方法,因为getBean()的实现和环境息息相关。
  • 迭代器模式。对于MutablePropertyValues类定义使用到了迭代器模式,因为此类存储并管理PropertyValue对象,也属于一个容器,所以给该容器提供一个遍历方式。

Spring框架其实使用到了很多设计模式,如AOP使用到了代理模式,选择JDK代理或者CGLIB代理使用到了策略模式,还有适配器模式,装饰者模式,观察者模式等。

3.6.2、符合大部分设计原则

3.6.3、整个设计和Spring的设计还有一定的出入

Spring框架底层是很复杂的,进行了很深入的封装,并对外提供了很好的扩展性。而我们定义的Spring IoC有以下几个目的:

  • 了解Spring底层对对象的大体管理机制;
  • 了解设计模式在具体的开发中的使用。
  • 以后学习Spring源码,通过该案例的实现,可以降低Spring学习的入门成本。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值