Spring框架用到了很多设计模式,比如:工厂模式、单例模式、代理模式、模板模式、装饰器模式、责任链模式、观察者模式、策略模式、构建模式、原型模式、适配器模式等。
工厂模式BeanFactory、ApplicationContext
单例模式AbstractBeanFactory、DefaultSingletonBeanRegistry
Spring依赖注入Bean实例默认是单例的,Spring的依赖注入(包括lazy-init方式)都是发生在 AbstractBeanFactory 的 getBean 里,getBean 的 doGetBean 方法调用 getSingleton 进行bean的创建。
代理模式JdkDynamicAopProxy、Cglib2AopProxy
AOP 的原理就是代理模式。
模板模式JdbcTemplate、RestTemplate
装饰器模式ServletRequestWrapper、SessionFactory
责任链模式DispatcherServlet、HandlerExecutionChain
观察者模式ApplicationEvent、ApplicationListener
策略模式SimpleInstantiationStrategy
构建模式BeanDefinitionBuilder
原型模式AbstractBeanFactory
适配器模式AdvisorAdapter、MethodBeforeAdviceAdapter、AfterReturningAdviceAdapter、ThrowsAdviceAdapter