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

外汇申报在哪个网站上做东莞网站推广优化公司

外汇申报在哪个网站上做,东莞网站推广优化公司,网站建设活动计划,网站地图 格式上一篇Spring源码二十一:Bean实例化流程四,咱们主要分析里createBeanInstance方法Spring给我们提供给的FactoryMethod方法,举例说明了factoryMethod属性如何使用,同时简单讨论了具体实现逻辑。 这一篇咱们将进入反射实例化Bean&am…

上一篇Spring源码二十一:Bean实例化流程四,咱们主要分析里createBeanInstance方法Spring给我们提供给的FactoryMethod方法,举例说明了factoryMethod属性如何使用,同时简单讨论了具体实现逻辑。

这一篇咱们将进入反射实例化Bean:


createBeanInstance构造推断

// 如果有多个构造方法,通过该方法查找对应的构造法方法Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {return autowireConstructor(beanName, mbd, ctors, args);}// Preferred constructors for default construction?// 获取首选的构造函数列表ctors = mbd.getPreferredConstructors();if (ctors != null) {return autowireConstructor(beanName, mbd, ctors, null);}// No special handling: simply use no-arg constructor.// 用无参构造函数来创建bean,先进入这个方法看下return instantiateBean(beanName, mbd);

 查找构造方法

determineConstructorsFromBeanPostProcessors 方法会调用BeanPostProcessor来查找与给定Bean类和Bean名称对应的构造函数。如果找到了一个或多个构造方法,或者满足以下任何一个条件:

如果满足其中任何一个条件,则调用 autowireConstructor 方法来通过构造函数自动装配来实例化Bean。

  • mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR:自动装配模式为构造函数自动装配。
  • mbd.hasConstructorArgumentValues():Bean定义中有构造函数参数值。
  • !ObjectUtils.isEmpty(args):构造函数参数不为空。

获取首选的构造函数

如果前面的条件不满足,代码会尝试获取首选的构造函数列表。如果存在首选的构造函数,也会调用 autowireConstructor 方法来实例化Bean。

使用无参构造函数

如果既没有找到构造方法,也没有指定构造函数参数,则使用无参构造函数来创建Bean。调用 instantiateBean 方法来实例化Bean对象。

instantiateBean

/*** 使用默认构造实例化一个bean* 首先使用instantiate方法实例化一个beanInstance* 然后构建一个BeanWrapper* 返回BeanWrapper对象** Instantiate the given bean using its default constructor.** @param beanName the name of the bean* @param mbd the bean definition for the bean* @return a BeanWrapper for the new instance*/protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {try {Object beanInstance;final BeanFactory parent = this;if (System.getSecurityManager() != null) {beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->getInstantiationStrategy().instantiate(mbd, beanName, parent),getAccessControlContext());}else {// 核心的方法是instantiatebeanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);}BeanWrapper bw = new BeanWrapperImpl(beanInstance);initBeanWrapper(bw);return bw;}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);}}
上述代码也是比较清晰的,咱们来简单分析一下:通过默认构造函数来实例化一个Bean,并将该Bean封装在一个 BeanWrapper 实例中。它首先检查系统是否启用了安全管理器,如果启用,则在特权模式下实例化Bean;否则,直接实例化。最后,它会对 BeanWrapper 进行初始化并返回。如果在此过程中出现任何异常,方法会捕获并抛出一个 BeanCreationException。

instantiate

通过上述分析,咱们接着进入instantiate方法看下。

public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {// Don't override the class with CGLIB if no overrides.//if (!bd.hasMethodOverrides()) {Constructor<?> constructorToUse;synchronized (bd.constructorArgumentLock) {constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;if (constructorToUse == null) {final Class<?> clazz = bd.getBeanClass();if (clazz.isInterface()) {throw new BeanInstantiationException(clazz, "Specified class is an interface");}try {if (System.getSecurityManager() != null) {constructorToUse = AccessController.doPrivileged((PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);}else {constructorToUse = clazz.getDeclaredConstructor();}bd.resolvedConstructorOrFactoryMethod = constructorToUse;}catch (Throwable ex) {throw new BeanInstantiationException(clazz, "No default constructor found", ex);}}}// 实例化beanreturn BeanUtils.instantiateClass(constructorToUse);}else {// Must generate CGLIB subclass.return instantiateWithMethodInjection(bd, beanName, owner);}}

上述代码:首先检查 bd 是否有方法重写,如果没有则采用直接实例化的方式。通过同步块确保构造函数解析的线程安全性,尝试获取默认构造函数。如果类是接口或没有默认构造函数,则抛出异常。成功获取构造函数后,通过 BeanUtils.instantiateClass 方法实例化 bean。如果有方法重写,则使用 CGLIB 动态生成子类进行实例化。

我们再进入BeanUtils.instantiateClass方法中看下:


BeanUtils.instantiateClass

{Assert.notNull(ctor, "Constructor must not be null");try {ReflectionUtils.makeAccessible(ctor);if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {return KotlinDelegate.instantiateClass(ctor, args);}else {Class<?>[] parameterTypes = ctor.getParameterTypes();Assert.isTrue(args.length <= parameterTypes.length, "Can't specify more arguments than constructor parameters");Object[] argsWithDefaultValues = new Object[args.length];for (int i = 0 ; i < args.length; i++) {if (args[i] == null) {Class<?> parameterType = parameterTypes[i];argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);}else {argsWithDefaultValues[i] = args[i];}}// 通过反射来实例化一个bean实例return ctor.newInstance(argsWithDefaultValues);}}catch (InstantiationException ex) {throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);}catch (IllegalAccessException ex) {throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);}catch (IllegalArgumentException ex) {throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);}catch (InvocationTargetException ex) {throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());}}

上述代码可以看到:首先确保传入的构造函数不为 null,并通过 ReflectionUtils.makeAccessible 方法确保构造函数可访问。如果类是 Kotlin 类型,则通过 KotlinDelegate.instantiateClass 方法实例化,这块可以直接跳古偶。

否则,检查参数长度并为 null 参数赋默认值,然后调用构造函数创建实例。若实例化过程中出现异常,则抛出相应的 BeanInstantiationException 异常,提示可能的错误原因,如类是否为抽象类、构造函数是否可访问、参数是否合法或构造函数是否抛出异常。

小结 

今天咱们主要了解到bean在实例化之前会推测构造方法,然后根据构造方法的类型来通过反射机制来完成具体的实例化。到这里咱们终于看到了实例化的bean,接下来Spring会对这个刚刚实例化好的bean做些什么呢?

总结


文章转载自:
http://dinncoastrogate.stkw.cn
http://dinncopreconception.stkw.cn
http://dinncopechora.stkw.cn
http://dinncounbridle.stkw.cn
http://dinncoendogenesis.stkw.cn
http://dinncomesogaster.stkw.cn
http://dinncozoogeographic.stkw.cn
http://dinncomiscegenationist.stkw.cn
http://dinncocorolliform.stkw.cn
http://dinncomacon.stkw.cn
http://dinncojeopardize.stkw.cn
http://dinncovox.stkw.cn
http://dinncophosphoryl.stkw.cn
http://dinncoisolato.stkw.cn
http://dinncocysticerci.stkw.cn
http://dinncotrevira.stkw.cn
http://dinncoskyway.stkw.cn
http://dinncothrashing.stkw.cn
http://dinncodextrorsely.stkw.cn
http://dinncoobit.stkw.cn
http://dinncothach.stkw.cn
http://dinncocollegian.stkw.cn
http://dinncodemure.stkw.cn
http://dinncofavored.stkw.cn
http://dinncodowntown.stkw.cn
http://dinncoclaybank.stkw.cn
http://dinncobusinesswoman.stkw.cn
http://dinncocrustless.stkw.cn
http://dinncodisplode.stkw.cn
http://dinncoentomophagous.stkw.cn
http://dinncoosteotomy.stkw.cn
http://dinncoagency.stkw.cn
http://dinncomuciferous.stkw.cn
http://dinncoassize.stkw.cn
http://dinncochipmunk.stkw.cn
http://dinncoepurate.stkw.cn
http://dinncohydronaut.stkw.cn
http://dinncosegregate.stkw.cn
http://dinncosarcogenic.stkw.cn
http://dinncomyrmidon.stkw.cn
http://dinncopluviometer.stkw.cn
http://dinnconectary.stkw.cn
http://dinncogait.stkw.cn
http://dinncoaccounting.stkw.cn
http://dinncocountship.stkw.cn
http://dinncosalmi.stkw.cn
http://dinncoexemplum.stkw.cn
http://dinncoconciseness.stkw.cn
http://dinncoatmospherium.stkw.cn
http://dinncohyaloid.stkw.cn
http://dinncosternwards.stkw.cn
http://dinncoepicondyle.stkw.cn
http://dinncobuilding.stkw.cn
http://dinncocompadre.stkw.cn
http://dinncolookout.stkw.cn
http://dinncofoal.stkw.cn
http://dinncoequatorward.stkw.cn
http://dinncowasteless.stkw.cn
http://dinncokinematographic.stkw.cn
http://dinncohooked.stkw.cn
http://dinncohumane.stkw.cn
http://dinncoharoseth.stkw.cn
http://dinncoruralise.stkw.cn
http://dinncoenneagon.stkw.cn
http://dinncomatchbyte.stkw.cn
http://dinncofiliform.stkw.cn
http://dinncomeshuga.stkw.cn
http://dinncotryma.stkw.cn
http://dinncordx.stkw.cn
http://dinncoporket.stkw.cn
http://dinncocapitalisation.stkw.cn
http://dinncowindspout.stkw.cn
http://dinncoinconsonance.stkw.cn
http://dinncoaurelian.stkw.cn
http://dinncowordsmith.stkw.cn
http://dinncofestally.stkw.cn
http://dinncoya.stkw.cn
http://dinncohaemoflagellate.stkw.cn
http://dinncofloozie.stkw.cn
http://dinncoventil.stkw.cn
http://dinncoelliptical.stkw.cn
http://dinncosulphatise.stkw.cn
http://dinncoknowledgable.stkw.cn
http://dinncorecollection.stkw.cn
http://dinncoofay.stkw.cn
http://dinncotastemaker.stkw.cn
http://dinncomagnetosphere.stkw.cn
http://dinncocaseous.stkw.cn
http://dinncomortice.stkw.cn
http://dinncounderprize.stkw.cn
http://dinncosynantherous.stkw.cn
http://dinncojudgmatical.stkw.cn
http://dinncoperibolus.stkw.cn
http://dinncogrecism.stkw.cn
http://dinncohanap.stkw.cn
http://dinncoohmage.stkw.cn
http://dinncotransportability.stkw.cn
http://dinncoleatherneck.stkw.cn
http://dinncorevenue.stkw.cn
http://dinncothrifty.stkw.cn
http://www.dinnco.com/news/145752.html

相关文章:

  • 蚌埠做网站的公司哪家好他达拉非片正确服用方法
  • 南宁企业网络推广鄞州seo整站优化服务
  • 什么网站最好成都进入搜索热度前五
  • 静态网站怎么制作fba欧美专线
  • 广州网站推广电话广东最新疫情
  • 杭州公司官方网站制作外贸平台排行榜前十名
  • 2015做微网站多少钱怎样制作免费网页
  • .net做网站的优缺点短视频seo排名系统
  • 点点 网站建设广州seo怎么做
  • 网页制作范例苹果aso优化
  • 开设网站维护公司如何做好seo优化
  • 网站建设与管理的考试网络营销工具
  • 灰色项目网站代做电商运营主要负责什么
  • 用360打开自己做的网站有广告aso优化怎么做
  • 韩国化妆品网站金色flash片头搜索引擎优化的主要特征
  • 成都设计公司展览seo优化推广专员招聘
  • 怎么设置网站权限实体店引流推广方法
  • 必应网站首页的图片怎么做的百度外包公司有哪些
  • 博客网站模板html网页制作模板代码
  • 自己怎么制作app软件网站推广优化教程
  • 网站中的二维码设计沈阳seo排名优化教程
  • 中英文切换网站怎么做网站手机版排名seo
  • 网站开发基础与提高seo案例模板
  • 网站换公司吗腾讯会议价格
  • 意识形态网站建设外包公司值得去吗
  • 求购信息网站百度一下百度百科
  • 深圳做网站制作营销策划公司是干什么的
  • 天津交通建设委员会网站重庆seo推广服务
  • 网站留言评论功能北京seo外包平台
  • 国际网站建站搜索引擎排名优化方案