当前位置: 首页 > news >正文

外贸网站建设平台有哪些办公室装修设计公司哪家好

外贸网站建设平台有哪些,办公室装修设计公司哪家好,php实验报告企业网站开发,手机建网站免费域名空间前言 本章开始分析finishBeanFactoryInitialization(beanFactory)方法#xff0c;直译过来就是完成Bean工厂的初始化#xff0c;这中间就是非lazy单例Bean的实例化流程。ConversionService在第十章已经提前分析了。重点就是最后一句#xff0c;我们的bean实例化分析就从这里…前言 本章开始分析finishBeanFactoryInitialization(beanFactory)方法直译过来就是完成Bean工厂的初始化这中间就是非lazy单例Bean的实例化流程。ConversionService在第十章已经提前分析了。重点就是最后一句我们的bean实例化分析就从这里开始。 本章主要是实例化流程的分析不会太深入到细节 protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {// Initialize conversion service for this context.// ConversionService转换服务在spring框架中用于处理类型转换的任务。它提供了一种统一的方式来执行各种类型之间的转换操作// 包括字符串到其他类型的转换、日期和时间的转换、数字类型的转换等。if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {beanFactory.setConversionService(beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));}// Register a default embedded value resolver if no BeanFactoryPostProcessor// (such as a PropertySourcesPlaceholderConfigurer bean) registered any before:// at this point, primarily for resolution in annotation attribute values.if (!beanFactory.hasEmbeddedValueResolver()) {beanFactory.addEmbeddedValueResolver(strVal - getEnvironment().resolvePlaceholders(strVal));}// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.String[] weaverAwareNames beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);for (String weaverAwareName : weaverAwareNames) {getBean(weaverAwareName);}// Stop using the temporary ClassLoader for type matching.beanFactory.setTempClassLoader(null);// Allow for caching all bean definition metadata, not expecting further changes.beanFactory.freezeConfiguration();// Instantiate all remaining (non-lazy-init) singletons.beanFactory.preInstantiateSingletons();}预处理单例bean preInstantiateSingletons() 是 Spring 容器的一个方法它用于预实例化所有的单例SingletonBean。 Overridepublic void preInstantiateSingletons() throws BeansException {if (logger.isTraceEnabled()) {logger.trace(Pre-instantiating singletons in this);}// Iterate over a copy to allow for init methods which in turn register new bean definitions.// While this may not be part of the regular factory bootstrap, it does otherwise work fine.ListString beanNames new ArrayList(this.beanDefinitionNames);// Trigger initialization of all non-lazy singleton beans...for (String beanName : beanNames) {// 通过bean的名称去获取bean的定义信息RootBeanDefinition bd getMergedLocalBeanDefinition(beanName);// 不是抽象类是单例的不是懒加载if (!bd.isAbstract() bd.isSingleton() !bd.isLazyInit()) {if (isFactoryBean(beanName)) {Object bean getBean(FACTORY_BEAN_PREFIX beanName);if (bean instanceof FactoryBean) {FactoryBean? factory (FactoryBean?) bean;boolean isEagerInit;if (System.getSecurityManager() ! null factory instanceof SmartFactoryBean) {isEagerInit AccessController.doPrivileged((PrivilegedActionBoolean) ((SmartFactoryBean?) factory)::isEagerInit,getAccessControlContext());}else {isEagerInit (factory instanceof SmartFactoryBean ((SmartFactoryBean?) factory).isEagerInit());}if (isEagerInit) {getBean(beanName);}}}else {getBean(beanName);}}}// Trigger post-initialization callback for all applicable beans...for (String beanName : beanNames) {Object singletonInstance getSingleton(beanName);if (singletonInstance instanceof SmartInitializingSingleton) {StartupStep smartInitialize this.getApplicationStartup().start(spring.beans.smart-initialize).tag(beanName, beanName);SmartInitializingSingleton smartSingleton (SmartInitializingSingleton) singletonInstance;if (System.getSecurityManager() ! null) {AccessController.doPrivileged((PrivilegedActionObject) () - {smartSingleton.afterSingletonsInstantiated();return null;}, getAccessControlContext());}else {smartSingleton.afterSingletonsInstantiated();}smartInitialize.end();}}}这个预处理单例bean的实例化其实非常简单我们简要分析一下整体流程 获取到当前所有的beanDefinitionNames用于迭代。循环所有的beanDefinitionNames也就是beanNamess获取beanName的定义信息用于后续判断如果是抽象类或者不是单例或者是懒加载bean就不处理。其他情况下会进入getBean逻辑这个方法我们经常用来从 BeanFactory 中获取一个 Bean而初始化的过程也封装到了这个方法里。 getBean/doGetBean Spring源码中很多时候真正做事的是do开头的个人经验。 Overridepublic Object getBean(String name) throws BeansException {return doGetBean(name, null, null, false);}transformedBeanName 获取beanName如果是FactoryBean是会以开头也就是说这里返回的就是单纯的beanName String beanName transformedBeanName(name);public static String transformedBeanName(String name) {Assert.notNull(name, name must not be null);if (!name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {return name;}return transformedBeanNameCache.computeIfAbsent(name, beanName - {do {beanName beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());}while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));return beanName;});}getSingleton 这里就是常说的三级缓存。在实例化的时候先要去三级缓存中查看是否存在了需不需要走后续的实例化流程。 Object sharedInstance getSingleton(beanName);Nullableprotected Object getSingleton(String beanName, boolean allowEarlyReference) {// Quick check for existing instance without full singleton lockObject singletonObject this.singletonObjects.get(beanName);if (singletonObject null isSingletonCurrentlyInCreation(beanName)) {singletonObject this.earlySingletonObjects.get(beanName);if (singletonObject null allowEarlyReference) {synchronized (this.singletonObjects) {// Consistent creation of early reference within full singleton locksingletonObject this.singletonObjects.get(beanName);if (singletonObject null) {singletonObject this.earlySingletonObjects.get(beanName);if (singletonObject null) {ObjectFactory? singletonFactory this.singletonFactories.get(beanName);if (singletonFactory ! null) {singletonObject singletonFactory.getObject();this.earlySingletonObjects.put(beanName, singletonObject);this.singletonFactories.remove(beanName);}}}}}}return singletonObject;}从上述代码中我们知道有三个Map对象也就是我们常说的三级缓存实际上就是以下这三个Map。 /** Cache of singleton objects: bean name to bean instance. */private final MapString, Object singletonObjects new ConcurrentHashMap(256);/** Cache of singleton factories: bean name to ObjectFactory. */private final MapString, ObjectFactory? singletonFactories new HashMap(16);/** Cache of early singleton objects: bean name to bean instance. */private final MapString, Object earlySingletonObjects new ConcurrentHashMap(16);一级缓存singletonObjects缓存的是单例对象key是beanName value是bean的实例。二级缓存earlySingletonObjects缓存的是早期的单例对象key是beanName value是bean的实例。三级缓存singletonFactories缓存的是ObjectFactorykey是beanName value是ObjectFactory。 回到getSingleton方法首先spring尝试通过beanName从一级缓存中获取如果一级缓存中不存在并且当前bean并不是正在创建中的话直接返回null。只有当bean正在创建中的时候才回去从二级缓存中获取如果二级缓存中获取不到再从三级缓存中去获取。此处我们先大概有这么一个概念后续会在循环依赖章节详细描述。 如果当前获取到的sharedInstance不为空且不是FacotyBean的话就直接返回回去了。如果为空继续走后续的逻辑。 dependsOn 根据BeanDefinition获取是否有依赖的bean如果有的话先把其他bean实例化完成。DependsOn的注解的作用就在这里可以体现。 String[] dependsOn mbd.getDependsOn();if (dependsOn ! null) {for (String dep : dependsOn) {if (isDependent(beanName, dep)) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,Circular depends-on relationship between beanName and dep );}registerDependentBean(dep, beanName);try {getBean(dep);}catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, beanName depends on missing bean dep , ex);}}}singletonsCurrentlyInCreation 如果是单例的就会调用 getSingleton(String beanName, ObjectFactory? singletonFactory)方法。 第一个参数就是beanName第二个参数是一个FunctionalInterface if (mbd.isSingleton()) {// 先执行getSingleton方法然后在代码中会调用createBean方法sharedInstance getSingleton(beanName, () - {try {return createBean(beanName, mbd, args);}catch (BeansException ex) {destroySingleton(beanName);throw ex;}});beanInstance getObjectForBeanInstance(sharedInstance, name, beanName, mbd);}执行getSingleton方法代码如下 public Object getSingleton(String beanName, ObjectFactory? singletonFactory) {Assert.notNull(beanName, Bean name must not be null);synchronized (this.singletonObjects) {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 );}// 对象是否正在创建中 singletonsCurrentlyInCreation避免循环依赖的一种操作beforeSingletonCreation(beanName);boolean newSingleton false;boolean recordSuppressedExceptions (this.suppressedExceptions null);if (recordSuppressedExceptions) {this.suppressedExceptions new LinkedHashSet();}try {// 在这里调用外出的createBean返回的就是springbean然后后续执行缓存的相关操作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;}afterSingletonCreation(beanName);}if (newSingleton) {addSingleton(beanName, singletonObject);}}return singletonObject;}}首先这个放回加了一个synchronized的锁锁的是一级缓存对象也就是说对象的实例化是线程安全的不让其在实例化的时候出现重复创建。获取锁之后会再从缓存中去获取当前beanName是否存在实例如果没有的话会执行beforeSingletonCreation(beanName)。 protected void beforeSingletonCreation(String beanName) {if (!this.inCreationCheckExclusions.contains(beanName) !this.singletonsCurrentlyInCreation.add(beanName)) {throw new BeanCurrentlyInCreationException(beanName);}}此处的核心就是singletonsCurrentlyInCreation集合的add方法就是提前曝光的地方。 private final SetString singletonsCurrentlyInCreation Collections.newSetFromMap(new ConcurrentHashMap(16));直接就是会调用传入的FunctionalInterface也就是执行createBean方法。 try {// 在这里调用外出的createBean返回的就是springbean然后后续执行缓存的相关操作singletonObject singletonFactory.getObject();newSingleton true;}createBean/doCreateBean 在执行doCreateBean的时候会调用构造方法 if (instanceWrapper null) {instanceWrapper createBeanInstance(beanName, mbd, args);}然后判断beanDefinition信息是单例的并且是允许循环依赖的默认为允许并且当前的bean是正在创建中就会进入if的逻辑。 addSingletonFactory,从名字开发猜测是否和我们的三级缓存有关联。 boolean earlySingletonExposure (mbd.isSingleton() this.allowCircularReferences isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isTraceEnabled()) {logger.trace(Eagerly caching bean beanName to allow for resolving potential circular references);}addSingletonFactory(beanName, () - getEarlyBeanReference(beanName, mbd, bean));}addSingletonFactory方法的参数如下 beanNamebean的名称ObjectFactory函数式接口之前看getSingleton(name)方法的时候也知道会调用这个getObject方法来获取对象放入二级缓存中。 一级缓存中不存在就将这个ObjectFactory放入三级缓存同时清理掉二级缓存和加入已注册的有序集合中。 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);}}}populateBean 填充bean也就是属性注入。这里会去解析bean的依赖从而继续去走getBean的逻辑来让当前bean实例化完整。 protected void populateBean(String beanName, RootBeanDefinition mbd, Nullable BeanWrapper bw) {if (bw null) {if (mbd.hasPropertyValues()) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, Cannot apply property values to null instance);}else {// property mean 属性// Skip property population phase for null instance.return;}}// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the// state of the bean before properties are set. This can be used, for example,// to support styles of field injection.// 在属性填充之前给任意InstantiationAwareBeanPostProcessors一个机会去修改bean的状态// 后置处理器可以被使用在例如:支持字段注入的样式.if (!mbd.isSynthetic() hasInstantiationAwareBeanPostProcessors()) {for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {return;}}}PropertyValues pvs (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);int resolvedAutowireMode mbd.getResolvedAutowireMode();if (resolvedAutowireMode AUTOWIRE_BY_NAME || resolvedAutowireMode AUTOWIRE_BY_TYPE) {MutablePropertyValues newPvs new MutablePropertyValues(pvs);// Add property values based on autowire by name if applicable.if (resolvedAutowireMode AUTOWIRE_BY_NAME) {autowireByName(beanName, mbd, bw, newPvs);}// Add property values based on autowire by type if applicable.if (resolvedAutowireMode AUTOWIRE_BY_TYPE) {autowireByType(beanName, mbd, bw, newPvs);}pvs newPvs;}boolean hasInstAwareBpps hasInstantiationAwareBeanPostProcessors();boolean needsDepCheck (mbd.getDependencyCheck() ! AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);PropertyDescriptor[] filteredPds null;if (hasInstAwareBpps) {if (pvs null) {pvs mbd.getPropertyValues();}// 实例化感知的BeanPostProcessorfor (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {// 默认的情况下resolvedAutowireMode是为0的所以默认是AutoWiredAnnotationBeanPostProcessor进行自动装配PropertyValues pvsToUse bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);if (pvsToUse null) {if (filteredPds null) {filteredPds filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);}// 在这里面回去校验属性的版本信息BeanNotOfRequiredTypeException/***Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:* Error creating bean with name b: Unsatisfied dependency expressed through field a;* nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException:* Bean named a is expected to be of type com.qhyu.cloud.circlarRefrence.A but was actually of type com.sun.proxy.$Proxy34*/pvsToUse bp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);if (pvsToUse null) {return;}}pvs pvsToUse;}}if (needsDepCheck) {if (filteredPds null) {filteredPds filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);}checkDependencies(beanName, mbd, filteredPds, pvs);}if (pvs ! null) {// 属性填充的最后回对spel进行解析applyPropertyValues(beanName, mbd, bw, pvs);}}initializeBean 如果 bean 实现了 BeanNameAware、BeanClassLoaderAware 或 BeanFactoryAware 接口回调invokeAwareMethods(beanName, bean) 处理 bean 中定义的 init-method或者如果 bean 实现了 InitializingBean 接口调用 afterPropertiesSet() 方法 protected Object initializeBean(String beanName, Object bean, Nullable RootBeanDefinition mbd) {if (System.getSecurityManager() ! null) {AccessController.doPrivileged((PrivilegedActionObject) () - {invokeAwareMethods(beanName, bean);return null;}, getAccessControlContext());}else {invokeAwareMethods(beanName, bean);}Object wrappedBean bean;if (mbd null || !mbd.isSynthetic()) {// 先执行beanpostProcessor的before// 通过BeanPostProcessor在bean初始化之前做点事情wrappedBean applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {// 再执行initmethods// 调用bean的初始化方法进行初始化invokeInitMethods(beanName, wrappedBean, mbd);}catch (Throwable ex) {throw new BeanCreationException((mbd ! null ? mbd.getResourceDescription() : null),beanName, Invocation of init method failed, ex);}if (mbd null || !mbd.isSynthetic()) {// 再执行BeanPostProcessor的After方法// 通过BeanPostProcessor在bean初始化之后做点事情// aop的切入点wrappedBean applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;}getSingleton 如果创建没有任何问题newSingleton true继续执行 addSingleton(beanName, singletonObject)方法 public Object getSingleton(String beanName, ObjectFactory? singletonFactory) {Assert.notNull(beanName, Bean name must not be null);synchronized (this.singletonObjects) {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 );}// 对象是否正在创建中 singletonsCurrentlyInCreation避免循环依赖的一种操作beforeSingletonCreation(beanName);boolean newSingleton false;boolean recordSuppressedExceptions (this.suppressedExceptions null);if (recordSuppressedExceptions) {this.suppressedExceptions new LinkedHashSet();}try {// 在这里调用外出的createBean返回的就是springbean然后后续执行缓存的相关操作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;}afterSingletonCreation(beanName);}if (newSingleton) {addSingleton(beanName, singletonObject);}}return singletonObject;} }将创建好的bean放入一级缓存移除一二级缓存。 protected void addSingleton(String beanName, Object singletonObject) {// 线程安全的synchronized (this.singletonObjects) {// 放入一级缓存this.singletonObjects.put(beanName, singletonObject);// 移除三级缓存this.singletonFactories.remove(beanName);// 移除二级缓存this.earlySingletonObjects.remove(beanName);// 已注册的map中把新初始化的bean放入this.registeredSingletons.add(beanName);}}总结 整个bean的初始化流程就如下图所示下面的这个图是一个最简单的单例bean的实例化流程不涉及到循环依赖问题循环依赖和三级缓存将在下一章详细分析。本章主要了解bean实例化的整体流程了解和熟悉spring工作模式。
http://www.dnsts.com.cn/news/5168.html

相关文章:

  • 网站策划书编写wordpress 实时表单
  • 发帖子最好的几个网站看2d影片最好的地方
  • 河南省建设厅网站考试成绩查询对于网站运营应该如何做
  • 网站营销培训洛阳网站建设seo
  • 网站 被攻击_主业篡改 被黑了 织梦做的站wordpress front
  • 新品销售网站建设wordpress中调用分类目录文章列表
  • 怎么0成本做网站网站开发 群
  • 微商城网站建设手写字体在线生成器
  • 南昌做微信网站wordpress查看管理员密码
  • 青海省公路建设总公司网站重庆智能网站建设费用
  • 自己做qq头像网站wordpress实名
  • 陕西泰烜建设集团网站wordpress自定义导航菜单
  • 电商网站设计趋势jfinal网站开发模板
  • 潢川微信网站建设深圳建设工程交易服务网老网站
  • 长沙免费建站模板网站建设实训分析总结
  • 网站开发基础知识广州seo排名收费
  • 网站 网页区别是什么wordpress 侧边栏调整
  • 厦门哪些做鲜花的网站织梦手机网站教程视频
  • 网站建设主要产品兰州网站优化公司
  • 无锡网站公司电话企业网站建设步骤是什么
  • 公司建网站流程logo制作软件免费版
  • 西安网站开发技术深圳代做网站
  • 什么网站可以做旅行行程私人做网站的流程
  • 本地wordpress站点上传文件哈尔滨网站建设运营
  • 高碑店建设局网站dw网页设计与制作步骤
  • 人物摄影网站苏州网站搜索引擎优化
  • 广东省公路建设有限公司网站广西壮族自治区皮肤病医院
  • 驾校视频网站模板教你如何做网络营销推广
  • 许昌 网站开发建筑企业办公系统公司
  • 做百度个人网站小地方网站建设公司