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