Java???????????????????
?????Linux???? ???????[ 2017/1/10 11:27:06 ] ??????????????????? Java
??????? ??????????
????Java?е????????????????????????????????????????????????????????????????
??????????????????????????????????????????濪????????????????????????????????????????á?
??????????????????????
????1. ?????????
public class Singleton {
private static final Singleton singleton= new Singleton();
private Singleton(){
}
public Singleton getSingleton(){
return singleton;
}
}
????????????????????????????????????????????????????????java??????????????????????????????????????????????????????????ú??????????????Щ??????ν?????У????????????????в?????κζ???????????Э????????????????????????????????????????????
????2???????????
public class Singleton {
private Singleton() {}
private static Singleton single=null;
public static Singleton getInstance() {
if (single == null) {
single = new Singleton();
}
return single;
}
}
??????????????????????????????£??????????single?????????????????????????????
public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
????????????????spring?е????
????DefaultSingletonBeanRegistry ???е?getSingleton??????
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String?? Object> singletonObjects = new ConcurrentHashMap<String?? Object>(64);
/** Cache of singleton factories: bean name --> ObjectFactory */
private final Map<String?? ObjectFactory<?>> singletonFactories = new HashMap<String?? ObjectFactory<?>>(16);
/** Cache of early singleton objects: bean name --> bean instance */
private final Map<String?? Object> earlySingletonObjects = new HashMap<String?? Object>(16);
protected Object getSingleton(String beanName?? boolean allowEarlyReference) {
// map?????в??????????
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName?? singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
???????????????п??????spring????????bean???????????????ж?????????????????????map????singletonObjects?л?????????????????????singletonObjects?????????map????earlySingletonObjects????????????????null?????????bean??????? earlySingletonObjects ?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11