SpringIOC——②③④⑤⑥

② prepareRefresh (): initialize before the IOC container ready (to configure the environment parameters, create a listener container, create event container)

    protected void prepareRefresh() {
        // 容器开始时间+容器关闭开启状态
        this.startupDate = System.currentTimeMillis();
        this.closed.set(false);
        this.active.set(true);

        if (logger.isDebugEnabled()) {
            if (logger.isTraceEnabled()) {
                logger.trace("Refreshing " + this);
            }
            else {
                logger.debug("Refreshing " + getDisplayName());
            }
        }

        //Some environmental parameter initialization IOC containers, empty default method, a subclass can override
         // here can create a standard environment variable StandardEnvironment, and then set some environmental parameters
         @ and specify the parameters to be configured 
        initPropertySources (); 

        // if not created, create a standard environment variable StandardEnvironment
         // whether the specified parameters must verify the above configurations have been configured, no exception is thrown 
        getEnvironment () validateRequiredProperties ();. 

        // configure some refresh () before listeners 
        IF ( the this .earlyApplicationListeners == null ) {
             the this .earlyApplicationListeners = new new a LinkedHashSet <> ( the this .applicationListeners); 
        } 
        the else {
             // Reset local application listeners to pre-refresh state.
            this.applicationListeners.clear();
            this.applicationListeners.addAll(this.earlyApplicationListeners);
        }

        //创建是一个事件集合
        this.earlyApplicationEvents = new LinkedHashSet<>();
    }

③ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory (): Control refresh number of calls.

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory () {
         // abstract method, IOC containers controlling refresh () times
         // allow only refresh () once again being given refresh: GenericApplicationContext.java
         // reuse allowed refresh (): AbstractRefreshableApplicationContext.java 
        refreshBeanFactory () ;
         // abstract method
         // DefaultListableBeanFactory created return ApplicationContext container class creates an instance 
        return the getBeanFactory (); 
    }

④prepareBeanFactory (beanFactory): set properties DefaultListableBeanFactory instance (class loader, ignoring the interface, the BeanPostProcessor, environment variables into a single register embodiment in beanfactory)

    protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        // Tell the internal bean factory to use the context's class loader etc.
        //设置类加载器
        beanFactory.setBeanClassLoader(getClassLoader());
        beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
        beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

        // Configure the bean factory with context callbacks.
        //设置BeanPostProcessor(后置处理器)
        beanFactory.addBeanPostProcessor(new new ApplicationContextAwareProcessor ( the this ));
         // set to be ignored interfaces, classes that implement these interfaces are not instantiated IOC container 
        beanFactory.ignoreDependencyInterface (EnvironmentAware. class ); 
        beanFactory.ignoreDependencyInterface (EmbeddedValueResolverAware. class ); 
        beanFactory.ignoreDependencyInterface (The ResourceLoaderAware . class ); 
        beanFactory.ignoreDependencyInterface (ApplicationEventPublisherAware. class ); 
        beanFactory.ignoreDependencyInterface (the MessageSourceAware. class ); 
        beanFactory.ignoreDependencyInterface (the ApplicationContextAware. class );

        // BeanFactory interface of the type not resolvable in Registered AS A Factory's Plain.
         // the MessageSource Registered (and found for autowiring) AS A bean.
         // BeanFactory interface can not be registered as a simple factory parsing type
         // automatically transferred with bean 
        beanFactory.registerResolvableDependency (the beanFactory. class , beanFactory); 
        beanFactory.registerResolvableDependency (the ResourceLoader. class , the this ); 
        beanFactory.registerResolvableDependency (ApplicationEventPublisher. class , the this ); 
        . beanFactory.registerResolvableDependency (the ApplicationContext class ,this);

        // Register early post-processor for detecting inner beans as ApplicationListeners.
        //设置BeanPostProcessor后置处理器
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

        // Detect a LoadTimeWeaver and prepare for weaving, if found.
        if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            // Set a temporary ClassLoader for type matching.
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }
//将环境单例StandardEnvironment注册到beanfactory单例容器中
        if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
        }
    }

⑤postProcessBeanFactory (beanFactory): abstract method, embodied subclass

Select AnnotationConfigApplication parent GenericWebApplicationContext:

The serlvet associated postprocessor scope types, the bean factory setting of the servlet to create the beanfactory

Example of a single container serlvet servletContext, servletConfig, contextParameters, contexAttributes registered in beanfactory

    protected  void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) {
         IF ( the this ! .servletContext = null ) {
             // register associated processor servletContext
             // Ignore servletContext associated interface 
            beanFactory.addBeanPostProcessor ( new new ServletContextAwareProcessor ( the this .servletContext)); 
            beanFactory.ignoreDependencyInterface (ServletContextAware . class ); 
        } 
        // Register the servlet beanfactory scope instance three, and the associated specified servlet type of bean plants created 
        WebApplicationContextUtils.registerWebApplicationScopes (beanFactory,the this .servletContext);
         // servletContex, ServletConfig, contextParameters, ContextAttributes singleton instance is registered in the container to beanfactory 
        WebApplicationContextUtils.registerEnvironmentBeans (beanFactory, the this .servletContext); 
    } 

    / * WebApplicationContextUtils.registerWebApplicationScopes (beanFactory, this.servletContext); * / 
    public  static  void registerWebApplicationScopes (ConfigurableListableBeanFactory beanFactory, 
            @Nullable the ServletContext sc) { 
        // Register servlet session scoped request
         // request: a request to create a request
         // session: a session to create a session
        beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
        beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
        if (sc != null) {
            //注册servlet作用域 application
            //application:所有用户共享
            ServletContextScope appScope = new ServletContextScope(sc);
            beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
            // Register as ServletContext attribute, for ContextCleanupListener to detect it.
            sc.setAttribute(ServletContextScope.class.getName(), appScope);
        }
        //注册对应类型的实例工厂
        beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
        beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
        beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
        beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
        if (jsfPresent) {
            FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
        }
    }

    /* WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext); */
    public static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf,
            @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {
        //将servletContext实例注册到beanfactory单例容器中
        if (servletContext != null && !bf.containsBean(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME)) {
            bf.registerSingleton(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME, servletContext);
        } 

        // register ServletConfig singleton instance to beanfactory vessel 
        IF (ServletConfig =! Null &&! Bf.containsBean (ConfigurableWebApplicationContext.SERVLET_CONFIG_BEAN_NAME)) { 
            bf.registerSingleton (ConfigurableWebApplicationContext.SERVLET_CONFIG_BEAN_NAME, ServletConfig); 
        } 

        // will be converted to a contextParameters Example Map single vessel registered in beanfactory 
        IF (! bf.containsBean (WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME)) { 
            Map <String, String> = the parameterMap new new the HashMap <> ();
             IF (! where servletContext = null ) { 
                the Enumeration<?> paramNameEnum = servletContext.getInitParameterNames();
                while (paramNameEnum.hasMoreElements()) {
                    String paramName = (String) paramNameEnum.nextElement();
                    parameterMap.put(paramName, servletContext.getInitParameter(paramName));
                }
            }
            if (servletConfig != null) {
                Enumeration<?> paramNameEnum = servletConfig.getInitParameterNames();
                while (paramNameEnum.hasMoreElements()) {
                    String paramName = (String) paramNameEnum.nextElement();
                    parameterMap.put (paramName, servletConfig.getInitParameter (paramName)); 
                } 
            } 
            bf.registerSingleton (WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME, 
                    ; Collections.unmodifiableMap (the parameterMap)) 
        } 

        // in the register Map contextAttributes converted to a single embodiment of the container to beanfactory 
        IF ( ! bf.containsBean (WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME)) { 
            the Map <String, Object> = attributeMap new new HashMap <> ();
             IF ! (servletContext = null ) { 
                Enumeration <?> = attrNameEnum servletContext.getAttributeNames();
                while (attrNameEnum.hasMoreElements()) {
                    String attrName = (String) attrNameEnum.nextElement();
                    attributeMap.put(attrName, servletContext.getAttribute(attrName));
                }
            }
            bf.registerSingleton(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME,
                    Collections.unmodifiableMap(attributeMap));
        }
    }

 ⑥ invokeBeanFactoryPostProcessors(beanFactory);

 

Guess you like

Origin www.cnblogs.com/wqff-biubiu/p/12375521.html