设计模式课程 设计模式精讲 8-11 单例模式源码解析(jdk+spring+mybaties)

1    源码解析

1.1  单例解析1

1.2  单例解析2(容器单例)

1.3  单例解析3

1.4  单例解析4

1    源码解析
1.1  单例解析1

java.lang.Runtime

/**
 *    饿汉式加载,初始化的时候,就已经new出了对象
 */
private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() {
        return currentRuntime;
    }
1.2  单例解析2(容器单例)

java.awt.Desktop(cs)

 /**
     * Returns the <code>Desktop</code> instance of the current
     * browser context.  On some platforms the Desktop API may not be
     * supported; use the {@link #isDesktopSupported} method to
     * determine if the current desktop is supported.
     * @return the Desktop instance of the current browser context
     * @throws HeadlessException if {@link
     * GraphicsEnvironment#isHeadless()} returns {@code true}
     * @throws UnsupportedOperationException if this class is not
     * supported on the current platform
     * @see #isDesktopSupported()
     * @see java.awt.GraphicsEnvironment#isHeadless
     */

    /*
     *    同步锁,context取对象,如果该对象为为null,new出新的对象,然后放入context
     */
    public static synchronized Desktop getDesktop(){
        if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
        if (!Desktop.isDesktopSupported()) {
            throw new UnsupportedOperationException("Desktop API is not " +
                                                    "supported on the current platform");
        }

        sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
        Desktop desktop = (Desktop)context.get(Desktop.class);

        if (desktop == null) {
            desktop = new Desktop();
            context.put(Desktop.class, desktop);
        }

    /**
     *  context put的时候加上同步锁,可以避免多线程put异常
     */
    public Object put(Object var1, Object var2) {
        HashMap var3 = this.table;
        synchronized(this.table) {
            MostRecentKeyValue var4 = this.mostRecentKeyValue;
            if (var4 != null && var4.key == var1) {
                var4.value = var2;
            }

            return this.table.put(var1, var2);
        }
    }
1.3  单例解析3(Spring框架获取单例对象)

spring中的单例是bean作用域中的一个,作用域在每个应用程序的上下文中只创建一个我们设置属性的实例,

和我们的单例的区别是:spring将实例的数量限制的作用域在整个应用程序的上下文,而java应用程序中,是将类加载器的数量限制在给定的类加载器的整个空间里。

所以说,在spring中启动多个容器的时候,每个容器即使是单例的,都可以拿到这个对象。

  public final T getObject() throws Exception {
        if (this.isSingleton()) {
            return this.initialized ? this.singletonInstance : this.getEarlySingletonInstance();
        } else {
            return this.createInstance();
        }
    }

/*
 *    如果被初始化,获取早期的单例对象
 *
 */
//通过代理去拿新对象
    private T getEarlySingletonInstance() throws Exception {
        Class<?>[] ifcs = this.getEarlySingletonInterfaces();
        if (ifcs == null) {
            throw new FactoryBeanNotInitializedException(this.getClass().getName() + " does not support circular references");
        } else {
            if (this.earlySingletonInstance == null) {
                this.earlySingletonInstance = Proxy.newProxyInstance(this.beanClassLoader, ifcs, new AbstractFactoryBean.EarlySingletonInvocationHandler());
            }

            return this.earlySingletonInstance;
        }
    }
1.4  单例解析4(基于threadLocal的线程案例)(mybaties获取单例对象)

mybaties上下文保证了每个线程各自的数据,每个线程自己的上下文,自己保存好

  private static final ThreadLocal<ErrorContext> LOCAL = new ThreadLocal<ErrorContext>();

 private ErrorContext() {
  }

  public static ErrorContext instance() {
    ErrorContext context = LOCAL.get();
if (context == null) {
      context = new ErrorContext();
      LOCAL.set(context);
    }
    return context;
  }

原文地址:https://www.cnblogs.com/1446358788-qq/p/11450813.html

时间: 2024-08-25 01:53:06

设计模式课程 设计模式精讲 8-11 单例模式源码解析(jdk+spring+mybaties)的相关文章

设计模式课程 设计模式精讲 4-3 简单工厂源码解析

1 源码解析 1.1 Calendar源码解析 1.2 DriverManager源码解析 1 源码解析 1.1 Calendar源码解析 /** * Gets a calendar using the specified time zone and default locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the d

设计模式课程 设计模式精讲 6-3 抽象工厂源码解析

1 源码解析 1.1 mysql源码解析 1.2 mybaties 的sqlsession源码解析 1 源码解析 1.1 mysql源码解析 1.2 mybaties 的sqlsession源码解析 原文地址:https://www.cnblogs.com/1446358788-qq/p/11295158.html

设计模式课程 设计模式精讲 10-2 外观模式源码解析

1 源码解析 1.1 源码解析1(jdk中的JDBCUtils工具类) 1.2 源码解析2 1.3 源码解析3 1.4 源码解析4 1 源码解析 1.1 源码解析1(jdk中的JDBCUtils工具类) jdbc在springJDBC中的封装 /** * Close the given JDBC Connection and ignore any thrown exception. * This is useful for typical finally blocks in manual JDB

设计模式课程 设计模式精讲 15-3 桥接模式源码解析

1 桥接模式源码解析 1.1 源码解析1 jdk中的应用(驱动类) 1 桥接模式源码解析 1.1 源码解析1 jdk中的应用(驱动类) 步骤: class.forName 调取驱动接口的静态块,触发驱动管理类DriverManager 的注册驱动方法,从而将该驱动放到CopyOnWriteArrayList中. getConnect方法是通过传入url用户名密码. 针对不同的数据库,通过driverManager中的不同方法,获取的都是相同的接口,jdbc在最初的时候设计了一套接口,再由各个数据

设计模式课程 设计模式精讲 17-3 模板方法模式源码解析

1 源码解析 1.1 源码解析1(在jdk中的使用) 1.2 源码解析2(在servlet中的应用) 1.3 源码解析3(在mybaties中的应用) 1 源码解析 1.1 源码解析1(在jdk中的使用) AbstractList(父类) public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { //get方法为抽象方法,完全交给子类去实现 abstr

设计模式课程 设计模式精讲 16-5 代理模式源码解析

1 源码解析 1.1 源码解析1(jdk中的应用) 1.2 源码解析2(spring中的应用) 1.3 源码解析3(mybaties中的应用) 1 源码解析 1.1 源码解析1(jdk中的应用) java.lang.reflect.Proxy public class Proxy implements java.io.Serializable { protected Proxy(InvocationHandler h) { doNewInstanceCheck(); this.h = h; }

设计模式课程 设计模式精讲 18-3 迭代器模式源码解析

1 源码解析 1.1 源码解析1(jdk中的应用) 1.2 源码解析2(mybaties中的应用)   1 源码解析 1.1 源码解析1(jdk中的应用) java.util.Iterator(接口) public interface Iterator<E> { /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #nex

设计模式课程 设计模式精讲 19-3 策略模式源码解析

1 源码解析 1.1 源码解析1(jdk中的应用1) 1.2 源码解析2(jdk中的应用2) 1.3 源码解析3(Spring中的应用1) 1.4 源码解析4(Spring中的应用2) 1 源码解析 1.1 源码解析1(jdk中的应用1) java.util.Comparator(策略类)作为比较器的应用 package java.util; public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Obj

设计模式课程 设计模式精讲 12-3 适配器模式源码解析

1 源码解析 1.1 源码解析1(在jdk中的应用) 1.2 源码解析2(Spring中的通知管理) 1.3 源码解析3(SpringMVC中的应用) 1 源码解析 1.1 源码解析1(在jdk中的应用) xmlAdapter(此类是用于适配xml的一个类,是处理xml序列化和反序列化的一个类) public abstract class XmlAdapter<ValueType,BoundType> { /** * Do-nothing constructor for the derived