创建类实例的第六种方式

版权声明:请尊重原创作者劳动成果,转载请标明出处,谢谢 https://blog.csdn.net/qq_31802135/article/details/85230831

今天在阅读springboot源码时,发现创建对象不同于以往new关键字Constructor创建clone反射调用以及反序列化的第六种写法。

声明

public class ApplicationConversionService extends FormattingConversionService {

	private static volatile ApplicationConversionService sharedInstance;

通过点语法创建实例

public static ConversionService getSharedInstance() {
		ApplicationConversionService sharedInstance = ApplicationConversionService.sharedInstance;
		if (sharedInstance == null) {
			synchronized (ApplicationConversionService.class) {
				sharedInstance = ApplicationConversionService.sharedInstance;
				if (sharedInstance == null) {
					sharedInstance = new ApplicationConversionService();
					ApplicationConversionService.sharedInstance = sharedInstance;
				}
			}
		}
		return sharedInstance;
	}

猜你喜欢

转载自blog.csdn.net/qq_31802135/article/details/85230831
今日推荐