美文网首页
Java8 LAMBDA表达式使用:

Java8 LAMBDA表达式使用:

作者: 码而优则仕 | 来源:发表于2020-05-11 20:00 被阅读0次

    Java8 LAMBDA表达式使用:

    protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
       Assert.notNull(singletonFactory, "Singleton factory must not be null");
       synchronized (this.singletonObjects) {
          if (!this.singletonObjects.containsKey(beanName)) {
             this.singletonFactories.put(beanName, singletonFactory);
             this.earlySingletonObjects.remove(beanName);
             this.registeredSingletons.add(beanName);
          }
       }
    }
    
    //java8之前的使用:
    addSingletonFactory(beanNarne, new ObjectFactory() {
    public Object getObject() throws BeansException {
    return getEarlyBeanReference(beanNarne,rnbd,bean);
    }
    });
       //java8的使用
    addSingletonFactory(beanName, () ->
            getEarlyBeanReference(beanName, mbd, bean));
    
    public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
            Assert.notNull(beanName, "Bean name must not be null");
            //全局变量需要同步
            synchronized (this.singletonObjects) {
                //首先检查对应的bean是否已经加载过, 因为singleton模式其实就是复用已经创建的bean. 所以这一步是必须的
                Object singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    if (this.singletonsCurrentlyInDestruction) {
                        throw new BeanCreationNotAllowedException(beanName,
                                "Singleton bean creation not allowed while singletons of this factory are in destruction " +
                                "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
                    }
                    //记 录加载状态,也就是通过 this.singletonsCurrentlyInCreation.add(beanName)将当前正要创建的 bean记录在缓存中,
                    // 这样便可以对循环依赖进行检测。
                    beforeSingletonCreation(beanName);
                    boolean newSingleton = false;
                    boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
                    if (recordSuppressedExceptions) {
                        this.suppressedExceptions = new LinkedHashSet<>();
                    }
                    try {
                        //初始化bean
                        singletonObject = singletonFactory.getObject();
                        newSingleton = true;
                    }
                    catch (IllegalStateException ex) {
                        // Has the singleton object implicitly appeared in the meantime ->
                        // if yes, proceed with it since the exception indicates that state.
                        singletonObject = this.singletonObjects.get(beanName);
                        if (singletonObject == null) {
                            throw ex;
                        }
                    }
                    catch (BeanCreationException ex) {
                        if (recordSuppressedExceptions) {
                            for (Exception suppressedException : this.suppressedExceptions) {
                                ex.addRelatedCause(suppressedException);
                            }
                        }
                        throw ex;
                    }
                    finally {
                        if (recordSuppressedExceptions) {
                            this.suppressedExceptions = null;
                        }
                        //当 bean加载结束后需要移除缓存中对该 bean 的正在加载状态的记录。
                        afterSingletonCreation(beanName);
                    }
                    if (newSingleton) {
                        //加入缓存
                        addSingleton(beanName, singletonObject);
                    }
                }
                return singletonObject;
            }
        }
    
    //java8 的使用
    sharedInstance = getSingleton(beanName, () -> {
                            try {
                                //做创建bean的准备工作
                                return createBean(beanName, mbd, args);
                            }
                            catch (BeansException ex) {
                                // Explicitly remove instance from singleton cache: It might have been put there
                                // eagerly by the creation process, to allow for circular reference resolution.
                                // Also remove any beans that received a temporary reference to the bean.
                                destroySingleton(beanName);
                                throw ex;
                            }
                        });
    
    
    
    

    相关文章

      网友评论

          本文标题:Java8 LAMBDA表达式使用:

          本文链接:https://www.haomeiwen.com/subject/nlupnhtx.html