单例模式JDK、Spring、mybatis源码样例
This commit is contained in:
39
pom.xml
39
pom.xml
@@ -14,6 +14,7 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.source>1.7</maven.compiler.source>
|
<maven.compiler.source>1.7</maven.compiler.source>
|
||||||
<maven.compiler.target>1.7</maven.compiler.target>
|
<maven.compiler.target>1.7</maven.compiler.target>
|
||||||
|
<org.springframework.version>5.1.0.RELEASE</org.springframework.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -53,6 +54,44 @@
|
|||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package top.fjy8018.designpattern.pattern.creational.singleton;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.config.AbstractFactoryBean;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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嘉阳
|
* @author F嘉阳
|
||||||
* @date 2018-09-24 22:31
|
* @date 2018-09-24 22:31
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import java.io.Serializable;
|
|||||||
* 优点:写法简单,类加载时初始化,线程安全
|
* 优点:写法简单,类加载时初始化,线程安全
|
||||||
* 缺点:若类不被使用则会造成内存浪费
|
* 缺点:若类不被使用则会造成内存浪费
|
||||||
*
|
*
|
||||||
|
* JDK源码样例:{@link Runtime} 其实例在类加载时实例化并通过{@link Runtime#getRuntime()} 获取
|
||||||
|
*
|
||||||
* @author F嘉阳
|
* @author F嘉阳
|
||||||
* @date 2018-09-24 15:38
|
* @date 2018-09-24 15:38
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package top.fjy8018.designpattern.pattern.creational.singleton;
|
package top.fjy8018.designpattern.pattern.creational.singleton;
|
||||||
|
|
||||||
|
import org.apache.ibatis.executor.ErrorContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于{@link ThreadLocalInstance} 的(伪)单例模式,只能保证线程内单例唯一(空间换时间思想)
|
* 基于{@link ThreadLocalInstance} 的(伪)单例模式,只能保证线程内单例唯一(空间换时间思想)
|
||||||
*
|
*
|
||||||
|
* mybatis源码样例:{@link ErrorContext#instance()} 返回通过ThreadLocal实现的单例对象LOCAL
|
||||||
|
*
|
||||||
* @author F嘉阳
|
* @author F嘉阳
|
||||||
* @date 2018-09-25 16:24
|
* @date 2018-09-25 16:24
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class ContainerSingletonTest {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
Object singleton = ContainerSingleton.getInstance("object");
|
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");
|
Object singleton = ContainerSingletonHashtable.getInstance("object");
|
||||||
log.info(Thread.currentThread().getName() + " " + singleton);
|
log.info(" " + singleton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ class LazyDoubleCheckSingletonTest {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
log.info(" " + lazySingleton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ class LazySingletonSynchronizedTest {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
log.info(" " + lazySingleton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ class LazySingletonTest {
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
log.info(" " + lazySingleton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ class StaticInnerClassSingletonTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
log.info(Thread.currentThread().getName() + " " + lazySingleton);
|
log.info(" " + lazySingleton);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ class ThreadLocalInstanceTest {
|
|||||||
// 在主线程中取
|
// 在主线程中取
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
ThreadLocalInstance instance = ThreadLocalInstance.getInstance();
|
ThreadLocalInstance instance = ThreadLocalInstance.getInstance();
|
||||||
log.info(Thread.currentThread().getName() + " " + instance);
|
log.info(" " + instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread thread1 = new Thread(new ThreadLocalInstanceTest.MyRunnable());
|
Thread thread1 = new Thread(new ThreadLocalInstanceTest.MyRunnable());
|
||||||
@@ -40,7 +40,7 @@ class ThreadLocalInstanceTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
log.info(Thread.currentThread().getName() + " " + instance);
|
log.info(" " + instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user