单例模式JDK、Spring、mybatis源码样例

This commit is contained in:
2018-09-25 16:57:54 +08:00
parent 644292961b
commit 25fcce6248
10 changed files with 60 additions and 8 deletions

39
pom.xml
View File

@@ -14,6 +14,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<org.springframework.version>5.1.0.RELEASE</org.springframework.version>
</properties>
<dependencies>
@@ -53,6 +54,44 @@
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
<build>

View File

@@ -2,7 +2,9 @@ package top.fjy8018.designpattern.pattern.creational.singleton;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
@@ -10,6 +12,11 @@ import java.util.Map;
* 基于容器(集合)实现单例
* 适用于类加载时有多个对象需要实例化场景
*
* JDK源码样例{@link Desktop#getDesktop()}
* 其{@link sun.awt.AppContext} 通过{@link HashMap}实现,使用同步保证单例线程安全
* Spring 源码样例:{@link AbstractFactoryBean#getObject()} 在156行判断当前类是否已经实例化
* 若没有实例化则在{@link AbstractFactoryBean#getEarlySingletonInstance()}中通过代理方式实例化对象
*
* @author F嘉阳
* @date 2018-09-24 22:31
*/

View File

@@ -9,6 +9,8 @@ import java.io.Serializable;
* 优点:写法简单,类加载时初始化,线程安全
* 缺点:若类不被使用则会造成内存浪费
*
* JDK源码样例{@link Runtime} 其实例在类加载时实例化并通过{@link Runtime#getRuntime()} 获取
*
* @author F嘉阳
* @date 2018-09-24 15:38
*/

View File

@@ -1,8 +1,12 @@
package top.fjy8018.designpattern.pattern.creational.singleton;
import org.apache.ibatis.executor.ErrorContext;
/**
* 基于{@link ThreadLocalInstance} 的(伪)单例模式,只能保证线程内单例唯一(空间换时间思想)
*
* mybatis源码样例{@link ErrorContext#instance()} 返回通过ThreadLocal实现的单例对象LOCAL
*
* @author F嘉阳
* @date 2018-09-25 16:24
*/

View File

@@ -57,7 +57,7 @@ class ContainerSingletonTest {
e.printStackTrace();
}
Object singleton = ContainerSingleton.getInstance("object");
log.info(Thread.currentThread().getName() + " " + singleton);
log.info(" " + singleton);
}
}
@@ -73,7 +73,7 @@ class ContainerSingletonTest {
}
Object singleton = ContainerSingletonHashtable.getInstance("object");
log.info(Thread.currentThread().getName() + " " + singleton);
log.info(" " + singleton);
}
}
}

View File

@@ -34,7 +34,7 @@ class LazyDoubleCheckSingletonTest {
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(Thread.currentThread().getName() + " " + lazySingleton);
log.info(" " + lazySingleton);
}
}
}

View File

@@ -34,7 +34,7 @@ class LazySingletonSynchronizedTest {
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(Thread.currentThread().getName() + " " + lazySingleton);
log.info(" " + lazySingleton);
}
}
}

View File

@@ -41,7 +41,7 @@ class LazySingletonTest {
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info(Thread.currentThread().getName() + " " + lazySingleton);
log.info(" " + lazySingleton);
}
}
}

View File

@@ -29,7 +29,7 @@ class StaticInnerClassSingletonTest {
} catch (Exception e) {
e.printStackTrace();
}
log.info(Thread.currentThread().getName() + " " + lazySingleton);
log.info(" " + lazySingleton);
}
}
}

View File

@@ -18,7 +18,7 @@ class ThreadLocalInstanceTest {
// 在主线程中取
for (int i = 0; i < 5; i++) {
ThreadLocalInstance instance = ThreadLocalInstance.getInstance();
log.info(Thread.currentThread().getName() + " " + instance);
log.info(" " + instance);
}
Thread thread1 = new Thread(new ThreadLocalInstanceTest.MyRunnable());
@@ -40,7 +40,7 @@ class ThreadLocalInstanceTest {
} catch (Exception e) {
e.printStackTrace();
}
log.info(Thread.currentThread().getName() + " " + instance);
log.info(" " + instance);
}
}
}