https://blog.csdn.net/zmh458/article/details/99685031

https://open.163.com/newview/movie/free?pid=XFVPTUCRI&mid=NFVPUVALS

IOC用到了哪些设计模式 - 图1

前言

Spring作为一款及其优秀的框架,其代码的编写非常优秀,里面采用了大量的设计模式。

叙述

Spring IOC 容器就像是一个工厂一样,当我们需要创建一个对象的时候,只需要配置好配置文件/注解即可,完全不用考虑对象是如何被创建出来的。IOC 容器负责创建对象,将对象连接在一起,配置这些对象,并从创建中处理这些对象的整个生命周期,直到它们被完全销毁。

工厂模式

Spring使用工厂模式可以通过 BeanFactory 或 ApplicationContext 创建 bean 对象。

两者对比:

  • BeanFactory :延迟注入(使用到某个 bean 的时候才会注入),相比于ApplicationContext 来说会占用更少的内存,程序启动速度更快。
  • ApplicationContext :容器启动的时候,不管你用没用到,一次性创建所有 bean 。BeanFactory 仅提供了最基本的依赖注入支持,ApplicationContext 扩展了 BeanFactory ,除了有BeanFactory的功能还有额外更多功能,所以一般开发人员使用ApplicationContext会更多。

Spring 实现单例的方式:

ApplicationContext的三个实现类:

ClassPathXmlApplication:把上下文文件当成类路径资源。 FileSystemXmlApplication:从文件系统中的 XML 文件载入上下文定义信息。 XmlWebApplicationContext:从Web系统中的XML文件载入上下文定义信息。

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.FileSystemXmlApplicationContext;
  3. public class App {
  4. public static void main(String[] args) {
  5. ApplicationContext context = new FileSystemXmlApplicationContext(
  6. "C:/work/IOC Containers/springframework.applicationcontext/src/main/resources/bean-factory-config.xml");
  7. HelloApplicationContext obj = (HelloApplicationContext) context.getBean("helloApplicationContext");
  8. obj.getMsg();
  9. }
  10. }

单例模式

Spring 中 bean 的默认作用域就是 singleton(单例)的。

Spring 实现单例的方式:

  1. xml : <bean id="userService" class="top.snailclimb.UserService" scope="singleton"/>
  2. 注解:@Scope(value = "singleton")

Spring 通过 ConcurrentHashMap 实现单例注册表的特殊方式实现单例模式。Spring 实现单例的核心代码如下

  1. // 通过 ConcurrentHashMap(线程安全) 实现单例注册表
  2. private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(64);
  3. public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
  4. Assert.notNull(beanName, "'beanName' must not be null");
  5. synchronized (this.singletonObjects) {
  6. // 检查缓存中是否存在实例
  7. Object singletonObject = this.singletonObjects.get(beanName);
  8. if (singletonObject == null) {
  9. //...省略了很多代码
  10. try {
  11. singletonObject = singletonFactory.getObject();
  12. }
  13. //...省略了很多代码
  14. // 如果实例对象在不存在,我们注册到单例注册表中。
  15. addSingleton(beanName, singletonObject);
  16. }
  17. return (singletonObject != NULL_OBJECT ? singletonObject : null);
  18. }
  19. }
  20. //将对象添加到单例注册表
  21. protected void addSingleton(String beanName, Object singletonObject) {
  22. synchronized (this.singletonObjects) {
  23. this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT));
  24. }
  25. }
  26. }

策略模式

在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。策略模式实际就是一堆算法族的封装。

Spring中策略模式的应用

当bean需要访问资源配置文件时,Spring有两种方式

代码中获取Rescource实例

依赖注入

第一种方式需要获取rescource资源的位置,代码中耦合性太高,而今我们一直使用注解,依赖注入的方式去获取。这样的话就无需修改程序,只改配置文件即可。

  1. <beans>
  2. <bean id="test" class="com.example.Test">
  3. <!-- 注入资源 -->
  4. <property name="tmp" value="classpath:book.xml"/>
  5. </bean>
  6. </beans>

在依赖注入的过程中,Spring会调用 ApplicationContext 来获取 Resource 的实例。然而,Resource 接口封装了各种可能的资源类型,包括了:UrlResource,ClassPathResource,FileSystemResource等,Spring需要针对不同的资源采取不同的访问策略。在这里,Spring 让 ApplicationContext 成为了资源访问策略的“决策者”。在资源访问策略的选择上,Spring采用了策略模式。当 Spring 应用需要进行资源访问时,它并不需要直接使用 Resource 实现类,而是调用 ApplicationContext 实例的 getResource() 方法来获得资源,ApplicationContext 将会负责选择 Resource 的实现类,也就是确定具体的资源访问策略,从而将应用程序和具体的资源访问策略分离开来。

  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
  2. Resource res = ctx.getResource("book.xml");

上面的代码中,Spring 将采用和 ApplicationContext 相同的策略来访问资源。即: ApplicationContext 是 ClassPathXmlApplicationContext,则 res 就是 ClassPathResource 实例。若将代码改为:

  1. ApplicationContext ctx = new Class FileSystemXmlApplicationContext("bean.xml");

则再次调用ctx.getResource时,res 就是 ClassPathResource 实例。

装饰者模式

通过使用修饰模式,可以在运行时扩充一个类的功能。原理是:增加一个修饰类包裹原来的类,包裹的方式一般是通过在将原来的对象作为修饰类的构造函数的参数。装饰类实现新的功能,但是,在不需要用到新功能的地方,它可以直接调用原来的类中的方法。修饰类必须和原来的类有相同的接口。

Spring 中装饰器模式的使用

Spring 中类中带有 Wrapper 的都是包装类,如下创建 bean 就是典型的装饰器模式

  1. BeanWrapper instanceWrapper = null;
  2. if (mbd.isSingleton()) {
  3. instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
  4. }
  5. if (instanceWrapper == null) {
  6. //根据指定的bean使用对应的侧脸创建新的实例,如工厂方法,构造函数自动注入,简单初始化
  7. instanceWrapper = createBeanInstance(beanName, mbd, args);
  8. }
  9. final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
  10. Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
  11. mbd.resolvedTargetType = beanType;
  12. if (beanType != null) {
  13. // Allow post-processors to modify the merged bean definition.
  14. synchronized (mbd.postProcessingLock) {
  15. if (!mbd.postProcessed) {
  16. try {
  17. applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
  18. }
  19. catch (Throwable ex) {
  20. throw new BeanCreationException(mbd.getResourceDescription(), beanName,
  21. "Post-processing of merged bean definition failed", ex);
  22. }
  23. mbd.postProcessed = true;
  24. }
  25. }
  26. }

模板模式

https://open.163.com/newview/movie/free?pid=XFVPTUCRI&mid=NFVPUVALS
image.png