spring中的设计模式(四)

简介: 5.单例模式 首先单例模式中的懒汉和饿汉模式, 懒汉模式: //懒汉式单例类.在第一次调用的时候实例化自己 public class Singleton {    private Singleton() {}    private static Singleton single...

5.单例模式

首先单例模式中的懒汉和饿汉模式,

懒汉模式:

//懒汉式单例类.在第一次调用的时候实例化自己

public class Singleton {

   private Singleton() {}

   private static Singleton single=null;

   //静态工厂方法

   public static Singleton getInstance() {

        if (single == null) { 

            single = new Singleton();

        } 

       return single;

    }

}

但是这个懒汉模式并没有考虑到线程安全,所以可以有三种方法对这个懒汉模式进行线程安全化

1.给getInstance方法加synchronized 关键字

2.双重检查锁定

public static Singleton getInstance() {

       if (singleton == null) { 

           synchronized (Singleton.class) { 

               if (singleton == null) { 

                  singleton = new Singleton();

               } 

           } 

       } 

       return singleton;

    }

3.静态内部类的方式

public class Singleton { 

   private static class LazyHolder { 

      private static final Singleton INSTANCE = new Singleton(); 

   } 

   private Singleton (){} 

   public static final Singleton getInstance() { 

      return LazyHolder.INSTANCE; 

   } 

饿汉模式:

//饿汉式单例类.在类初始化时,已经自行实例化

public class Singleton1 {

   private Singleton1() {}

   private static final Singleton1 single = new Singleton1();

   //静态工厂方法

   public static Singleton1 getInstance() {

       return single;

    }

}

 

区别:

饿汉模式天生就是线程安全的,而懒汉模式则需要自行实现线程安全

饿汉式模式在类创建的时候就会实例化一个静态的对象出来,会占用一定内存,但是相应的第一次调用会快一点

懒汉模式的延迟加载,需要调用的时候才会实例化对象

第一种方法实现同步的话,每次调用都会同步,非常耗性能,因为大部分情况是不需要同步的

第二种方法双重判断则避免了每次都需要同步的情况

第三种方法,利用了ClassLoader的机制确保初始化Intance的时候都只有一个线程

 

spring中的单例模式:

spring中的依赖注入都发生在AbstractBeanFactory的getBean方法里面,而getBean方法里面doGetBean方法中的getSingleton方法

 

protected Object getSingleton(String beanName, boolean allowEarlyReference)

    {

        Object singletonObject = singletonObjects.get(beanName);

        if(singletonObject == null &&isSingletonCurrentlyInCreation(beanName))

            synchronized(singletonObjects)

            {

                singletonObject = earlySingletonObjects.get(beanName);

                if(singletonObject == null && allowEarlyReference)

                {

                    ObjectFactory singletonFactory =(ObjectFactory)singletonFactories.get(beanName);

                    if(singletonFactory != null)

                    {

                        singletonObject = singletonFactory.getObject();

                        earlySingletonObjects.put(beanName, singletonObject);

                        singletonFactories.remove(beanName);

                    }

                }

            }

        return singletonObject == NULL_OBJECT ? null : singletonObject;

   }

 

可以看到spring中用的是懒汉模式的双重判断来实现线程安全的,避免在加锁的瞬间有其他注入的时候创建实例

相关文章
|
25天前
|
设计模式 SQL Java
Spring中的设计模式
Spring中的设计模式
|
5天前
|
设计模式 安全 Java
【初学者慎入】Spring源码中的16种设计模式实现
以上是威哥给大家整理了16种常见的设计模式在 Spring 源码中的运用,学习 Spring 源码成为了 Java 程序员的标配,你还知道Spring 中哪些源码中运用了设计模式,欢迎留言与威哥交流。
|
8天前
|
设计模式 存储 Java
手写spring第二章-运用设计模式编写可扩展的容器
手写spring第二章-运用设计模式编写可扩展的容器
8 0
|
8天前
|
设计模式 缓存 Java
Spring中用到的设计模式小结
Spring中用到的设计模式小结
14 0
|
16天前
|
设计模式 Java 数据库连接
Spring Framework 6 中的设计模式
Spring Framework 6 中的设计模式
21 1
|
2月前
|
设计模式 Java 数据库连接
Spring 中经典的 9 种设计模式
Spring 中经典的 9 种设计模式
54 2
|
2月前
|
设计模式 Java 数据库连接
9种设计模式在Spring中的运用
9种设计模式在Spring中的运用
78 0
|
设计模式 Java 数据库连接
Spring设计模式(一)
Spring设计模式(一)
|
4月前
|
设计模式 Java Spring
Spring5深入浅出篇:Spring工厂设计模式拓展应用
Spring5深入浅出篇:Spring工厂设计模式拓展应用
|
4月前
|
设计模式 Java Spring
Spring5深入浅出篇:Spring与工厂设计模式简介
Spring5深入浅出篇:Spring与工厂设计模式简介