/**
* 容器单例, 比如spring容器
*
* @author yangsx
* @version 1.0
* @date 2019/9/11
*/
public class S8_ContainerImplement {
private static List<S8_ContainerImplement> set = new ArrayList<>();
static {
set.add(new S8_ContainerImplement());
}
public static S8_ContainerImplement getInstance() {
return set.get(0);
}
}
type singleton struct{}
var ins *singleton = &singleton{}
func GetIns() *singleton{
return ins
}
type singleton struct{}
var ins *singleton
func GetIns() *singleton{
if ins == nil {
ins = &singleton{}
}
return ins
}
type singleton struct{}
var ins *singleton
var mu sync.Mutex
func GetIns() *singleton{
mu.Lock()
defer mu.Unlock()
if ins == nil {
ins = &singleton{}
}
return ins
}
type singleton struct{}
var ins *singleton
var mu sync.Mutex
func GetIns() *singleton{
if ins == nil {
mu.Lock()
defer mu.Unlock()
if ins == nil {
ins = &singleton{}
}
}
return ins
}
type singleton struct{}
var ins *singleton
var once sync.Once
func GetIns() *singleton {
once.Do(func(){
ins = &singleton{}
})
return ins
}
public class ErrorContext {
private static final String LINE_SEPARATOR = System.lineSeparator();
// 将实例放入到线程本地变量中,饿汉
private static final ThreadLocal<ErrorContext> LOCAL = ThreadLocal.withInitial(ErrorContext::new);
private ErrorContext stored;
private String resource;
private String activity;
private String object;
private String message;
private String sql;
private Throwable cause;
// 构造函数私有化
private ErrorContext() {
}
// 获取实例,也就是错误信息上下文
public static ErrorContext instance() {
return LOCAL.get();
}
public ErrorContext store() {
ErrorContext newContext = new ErrorContext();
newContext.stored = this;
LOCAL.set(newContext);
return LOCAL.get();
}
public ErrorContext recall() {
if (stored != null) {
LOCAL.set(stored);
stored = null;
}
return LOCAL.get();
}
public ErrorContext resource(String resource) {
this.resource = resource;
return this;
}
public ErrorContext activity(String activity) {
this.activity = activity;
return this;
}
public ErrorContext object(String object) {
this.object = object;
return this;
}
public ErrorContext message(String message) {
this.message = message;
return this;
}
public ErrorContext sql(String sql) {
this.sql = sql;
return this;
}
public ErrorContext cause(Throwable cause) {
this.cause = cause;
return this;
}
// thread-local在使用后,需要清空,mybatis通过try-catch-finally的机制(针对sql语句),在finally块中调用此方法清空thread-local
public ErrorContext reset() {
resource = null;
activity = null;
object = null;
message = null;
sql = null;
cause = null;
LOCAL.remove();
return this;
}
@Override
public String toString() {
StringBuilder description = new StringBuilder();
// message
if (this.message != null) {
description.append(LINE_SEPARATOR);
description.append("### ");
description.append(this.message);
}
// resource
if (resource != null) {
description.append(LINE_SEPARATOR);
description.append("### The error may exist in ");
description.append(resource);
}
// object
if (object != null) {
description.append(LINE_SEPARATOR);
description.append("### The error may involve ");
description.append(object);
}
// activity
if (activity != null) {
description.append(LINE_SEPARATOR);
description.append("### The error occurred while ");
description.append(activity);
}
// sql
if (sql != null) {
description.append(LINE_SEPARATOR);
description.append("### SQL: ");
description.append(sql.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim());
}
// cause
if (cause != null) {
description.append(LINE_SEPARATOR);
description.append("### Cause: ");
description.append(cause.toString());
}
return description.toString();
}
}