首页天道酬勤horizontalalignment属性,getmethod和getdeclaredmethod

horizontalalignment属性,getmethod和getdeclaredmethod

张世龙 05-04 20:58 24次浏览

牛客面经遇到的面问题。 突然想试试,就开始做了

测试applicationcontextapplicationcontext=newclasspathxmlapplicationcontext (' class path : application context.XML ' ); testdaotestdao=application context.get bean (test Dao.class ); (ClassPathXmlApplicationContext (应用程序上下文).close ); 测试的bean

@ componentpublicclasstestdaoimplementsinitializingbean, disposable bean { publictestdao (} system.out.println ) (constem.out.println ) (constestDao ) } @ async public void qln ); } @ overridepublicvoidafterpropertiesset () throwsexception ) system.out.println (afterpropertiesset ); } @ postconstructpublicvoidpostconstruct () system.out.println ) postconstruct ); }公共void initmethod () (system.out.println ) (init method ); } @Override public void destroy () throws exception (system.out.println ) ) destroy ); } @ predestroypublicvoidpredestroy () system.out.println ) ' predestroy ); }公共void destroymethod () system.out.println ) ) destroy method ); }}xml

? XML版本=' 1.0 '编码=' utf-8 '? beans xmlns=' http://www.spring framework.org/schema/beans ' xmlns 3360 tx=' http://www.spring framework.org/方案XML方案-实例' xmlns : context=' http://www.spring framework.org /方案/context ' xsi :方案定位方案/beans/spring-beans.xsd http://www.spring framework.org /方案/tx 3358 spring-tx.xsd http://www.spring framework context https://www.spring framework.org/schema/context/spring-context.xsd ' context : annotation-config/bean id=' test Dao ' class=' com.test.Dao.test Dao ' init-method=' init method ' destroy-method=' destroy method bbod 总的来说,如何构建打印结果-注释@PostConstruct方法-InitializingBean接口的afterPropertiesSet方法-放置在XML中的init-method方法销毁也是同样的注释@PreDestroy

源代码在断点调试中发现一些初始化方法位于abstractautowirecapablebeanfactory的初始化bean方法中

保护对象初始化bean (finalstringbeanname,final Object bean, @Nullable RootBeanDefinition mbd ) if ) system.get ssion=null (访问控制器. do privileged ((privilegedactionobject ) ) 返回空值;getAccessControlContext (); } else { invokeawaremethods (beanname,bean ); }对象扭曲bean=bean; if(mbd==null|! mbd.isSynthetic () /其中@PostConstruct注释的方法initdestroyannotationbeanpostprocessor # 运行postprocessbeforeinitializationwrappedbean=applybeanpostprocessbeforeinitialization (wrapped bean,beanName ); } try { //为afterPropertiesSet和init-method方法invokeinitmethods(beanname,wrappedBean,mbd ); }可移植Ex thrownewbeancreationexception ((mbd!=null? mbd.getResourceDescription () : null )、beanName、' Invocation of init method failed ',ex ); (if ) mbd==空|! mbd.isSynthetic () wrapped bean=applybeanpostprocessorsafterinitialization (wrapped bean,beanName ); }返回扭曲的bean; }后续执行的两种方法protectedvoidinvokeinitmethods (string beanname,final Object bean,@Nullable RootBeanDefinition mbd ) throws throward

booleanisinitializingbean=(beaninstanceofinitializingbean ); if (isinitializingbean (mbd==null |! mbd.isexternallymanagedinitmethod (afterpropertiesset ) ) if ) logger.istraceenabled ) ) logger.trace ) istraceeenabled=null () try ) access controller.do privileged ((privilegedexceptionactionobject )-) ) (初始化bean ) . 返回空值;getAccessControlContext (); } catch (privilegedactionexceptionpae ) { throw pae.getException ); }else{//afterPropertiesSet方法((InitializingBean ) bean ).afterpropertiesset ); (if ) mbd!=null bean.getClass ()!=null bean.class (stringinitmethodname=mbd.getinitmethodname ); if (string utils.has length (init method name )! (isinitializingbean ' afterpropertiesset '.equals (init方法名称)! mbd.isexternallymanagedinitmethod (init method name )//是xml自定义的init-method方法invokecustominitmethod ) beanname,

Springinit方法,getmethod和getdeclaredmethod