spring源码单例模式有哪些
-
Spring源码中使用了多种方式实现单例模式,下面列举几种常见的方式:
-
饿汉式单例:在类加载的时候就创建了唯一的实例,保证了线程安全,但可能导致资源浪费。
示例代码:public class Singleton { private static Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } } -
懒汉式单例:在需要使用实例时才进行实例化,避免了资源浪费,但需要考虑线程安全的问题。
示例代码:public class Singleton { private static Singleton instance; private Singleton() {} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } -
双重检测锁单例:通过双重判断实现延迟加载和线程安全,提高了性能。
示例代码:public class Singleton { private volatile static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } -
静态内部类单例:利用Java类加载机制,实现延迟加载和线程安全。
示例代码:public class Singleton { private Singleton() {} private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } }
以上是Spring中常用的几种单例模式实现方式,它们各有特点,在使用时可以根据具体情况选择适合的方式。
1年前 -
-
Spring 框架中实现单例模式的方式有以下几种:
-
饿汉式单例模式(Eager Singleton):在类加载时就创建实例,保证实例的静态唯一性。
public class EagerSingleton { private static final EagerSingleton instance = new EagerSingleton(); private EagerSingleton() {} public static EagerSingleton getInstance() { return instance; } } -
懒汉式单例模式(Lazy Singleton):在获取实例时才进行实例化,延迟加载。
public class LazySingleton { private static volatile LazySingleton instance; private LazySingleton() {} public static LazySingleton getInstance() { if (instance == null) { synchronized (LazySingleton.class) { if (instance == null) { instance = new LazySingleton(); } } } return instance; } } -
注册式单例模式(Registry Singleton):将多个单例对象存储在一个特定的注册表中,通过唯一的标识符获取实例。
public class RegistrySingleton { private static Map<String, Object> singletonRegistry = new ConcurrentHashMap<>(); private RegistrySingleton() {} public static void register(String key, Object instance) { if (!singletonRegistry.containsKey(key)) { singletonRegistry.put(key, instance); } } public static Object getSingleton(String key) { return singletonRegistry.get(key); } } -
容器式单例模式(Container Singleton):将单例对象存储在一个特定的容器中,通过容器获取实例。
public class ContainerSingleton { private static Map<String, Object> singletonContainer = new ConcurrentHashMap<>(); private ContainerSingleton() {} public static void addSingleton(String key, Object instance) { if (!singletonContainer.containsKey(key)) { singletonContainer.put(key, instance); } } public static Object getSingleton(String key) { return singletonContainer.get(key); } } -
枚举单例模式(Enum Singleton):利用枚举类型的特性,保证实例的唯一性和线程安全性。
public enum EnumSingleton { INSTANCE; public void doSomething() { // ... } }
总结:Spring 框架中利用了以上几种方式实现了单例模式,开发者可以根据具体的业务需求和实现要求选择适合的单例模式。
1年前 -
-
在Spring源码中,使用了多种方式来实现单例模式,包括以下几种:
-
饿汉式单例模式:
饿汉式单例模式是指在类加载时就创建实例,因此在获取实例时无需进行判空操作。Spring源码中通过静态变量的方式来实现饿汉式单例模式,例如BeanDefinitionMap这个类中:private static Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256); -
懒汉式单例模式:
懒汉式单例模式是指在第一次使用时才进行实例化,只有在需要用到对象时,才会去创建实例。Spring源码中通过使用volatile和双重检查锁定方式来实现线程安全的懒汉式单例模式,例如DefaultListableBeanFactory这个类中:private volatile Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); -
注册式单例模式:
注册式单例模式是指通过容器来管理对象的创建和销毁,并使用唯一的标识来区分不同的对象。Spring源码中使用了单例对象注册表来实现注册式单例模式,例如DefaultSingletonBeanRegistry这个类中:private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); -
枚举单例模式:
枚举单例模式是指使用枚举类来实现单例模式,枚举类的实例是在类加载时创建的,枚举类的构造函数是私有的,只能在枚举类内部使用。Spring源码中使用枚举类实现了一些单例对象,例如ScopeTypeEnum这个枚举类。
总结:
Spring源码中使用的单例模式有饿汉式单例模式、懒汉式单例模式、注册式单例模式和枚举单例模式。这些实现方式都保证了在多线程环境下获取到的实例是唯一的,并且都通过类加载机制来保证实例的创建和获取是线程安全的。通过使用单例模式,可以节省资源并提高性能。1年前 -