全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

Spring中都应用了哪些设计模式

发布时间:2022-09-21 17:22:33
发布人:wjy

  1. 简单工厂模式:简单工厂模式的本质就是一个工厂类根据传入的参数,动态的决定实例化哪个类。Spring中的BeanFactory就是简单工厂模式的体现,根据传入一个唯一的标识来获得bean对象。

  2. 工厂方法模式:应用程序将对象的创建及初始化职责交给工厂对象,工厂Bean。 定义工厂方法,然后通过config.xml配置文件,将其纳入Spring容器来管理,需要通过factory-method指定静态方法名称。

  3. 单例模式Spring用的是双重判断加锁的单例模式,通过getSingleton方法从singletonObjects中获取bean。 /** * Return the (raw) singleton object registered under the given name. *

  Checks already instantiated singletons and also allows for an early * reference to a currently created singleton (resolving a circular reference). * @param beanName the name of the bean to look for * @param allowEarlyReference whether early references should be created or not * @return the registered singleton object, or {@code null} if none found */ protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized (this.singletonObjects) { singletonObject = this.earlySingletonObjects.get(beanName); if (singletonObject == null && allowEarlyReference) { ObjectFactory singletonFactory = this.singletonFactories.get(beanName); if (singletonFactory != null) { singletonObject = singletonFactory.getObject(); this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return (singletonObject != NULL_OBJECT ? singletonObject : null); }

Spring中都应用了哪些设计模式

  4. 代理模式Spring的AOP中,使用的Advice(通知)来增强被代理类的功能。Spring实现AOP功能的原理就是代理模式(① JDK动态代理,② CGLIB字节码生成技术代理。)对类进行方法级别的切面增强。

  5. 装饰器模式 装饰器模式:动态的给一个对象添加一些额外的功能。 Spring的ApplicationContext中配置所有的DataSource。这些DataSource可能是不同的数据库,然后SessionFactory根据用户的每次请求,将DataSource设置成不同的数据源,以达到切换数据源的目的。 在Spring中有两种表现:

  - 一种是类名中含有Wrapper;

  - 另一种是类名中含有Decorator。

  6. 观察者模式 定义对象间的一对多的关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动更新。Spring中观察者模式一般用在listener的实现。

  7. 策略模式 策略模式是行为性模式,调用不同的方法,适应行为的变化 ,强调父类的调用子类的特性 。getHandler是HandlerMapping接口中的唯一方法,用于根据请求找到匹配的处理器。

  8. 模板方法模式Spring JdbcTemplate的query方法总体结构是一个模板方法+回调函数,query方法中调用的execute()是一个模板方法,而预期的回调doInStatement(Statement state)方法也是一个模板方法。

相关文章

什么是域控制器?

什么是域控制器?

2023-10-15
图卷积网络和self-attention有什么区别?

图卷积网络和self-attention有什么区别?

2023-10-15
深度学习模型权重h5、weights、ckpt、pth有什么区别?

深度学习模型权重h5、weights、ckpt、pth有什么区别?

2023-10-15
机器学习中Inference和predict的区别是什么?

机器学习中Inference和predict的区别是什么?

2023-10-15

最新文章

常见网络安全面试题:Windows常用的命令有哪些?

常见网络安全面试题:Windows常用的命令有哪些?

2023-10-09
常见网络安全面试题:根据设备告警如何展开排查?

常见网络安全面试题:根据设备告警如何展开排查?

2023-10-09
常见网络安全面试题:mysql加固呢?(数据库加固)

常见网络安全面试题:mysql加固呢?(数据库加固)

2023-10-09
常见网络安全面试题:windows和linux加固?(操作系统加固)

常见网络安全面试题:windows和linux加固?(操作系统加固)

2023-10-09
在线咨询 免费试学 教程领取