首页天道酬勤spring初始化自定义bean,springbean初始化过程

spring初始化自定义bean,springbean初始化过程

张世龙 05-04 21:58 115次浏览

在初始化springbean时执行某些方法完成特定初始化操作的项目中,通常在容器启动时完成特定初始化操作,如加载资源文件。

一 实现的方式有三种:

使用作用于void方法的@PostConstruct注释

2 .在性能分析中设置init-method方法

bean id=' student ' class=' com.demo.spring.entity.student ' init-method=' init2' property name=' name ' valude

package com.demo.spring.entity; import javax.annotation.post construct; importorg.spring framework.beans.factory.initializing bean; importorg.spring framework.stereotype.com ponent; /** * @author chenyk * @date 2018年5月8日*/@component('student ' ) publicclassstudentimplementsinitializingbean { privation 隐私学校; public String getName () { return name; }公共语音集名称(字符串名称) { this.name=name; } public int getAge () { return age; }公共语音设置(intage ) { this.age=age; }公共school getschool () {返回school; } publicvoidsetschool (schoolschool ) { this.school=school; //1.postconstrtct注释@PostConstruct public void init () { System.out.println (“运行init方法”); //2.XML配置文件中的init-method方法public void init2((system.out.println ) )“运行init2方法”; //3.initializingbean接口公共语音适配器() throws exception (system.out.println ) (运行init3方法) }执行结果:

执行init方法

2018-06-1110336009336016,195 debug [ abstractautowirecapablebeanfactory.Java :1671 ] : invokingafterprpropertiesssiory

执行init3方法

2018-06-1110336009336036,459 debug [ abstractautowirecapablebeanfactory.Java :1731 ] : invokinginitmethod ' ining

执行init2方法

二 实现原理:

在以上运行结果中,首先运行@PostConstruct注释的方法,然后运行实现InitializingBean接口的afterPropertiesSet方法,最后运行放置在配置文件中的init-method 关于为什么是这个顺序,可以看到源代码:

在abstractautowirecapablebeanfactory类中

保护对象初始化bean (finalstringbeanname,final Object bean, rootbeandefinitionmbd (if (system.getsecuritymanage=null (访问控制器. do权限对象) ) aremethododect () overrridepublicobjectrun ) ) return null; },getAccessControlContext (); } else { invokeawaremethods (beanname,bean ); }对象扭曲bean=bean; if(mbd==null|! mbd.isSynthetic () wrapped bean=applybeanpostprocessorsbeforeinitialization (wrapped bean,beanName ); } try { //初始化方法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; }然后转到invokeInitMethods方法

protectedvoidinvokeinitmethods (字符串beanname,final Object bean,RootBeanDefinition mbd ) throws throwable { booleanisinitialion mbd.isexternallymanagedinitmethod (afterpropertiesset ) ) if ) logger.isdebugenabled ) ) logger.debug ) isdebugenabug=null(try ) access controller.do privileged (newprivilegedexceptionactionobject ) ) { @ override public object run } } throoooo },getAccessControlContext (); } catch (privilegedactionexceptionpae ) { throw pae.getException ); }else{//InitializingBean接口的自适应propertiesset方法((initializingbean ).afterPropertiesSet ); (if ) mbd!=null (stringinitmethodname=mbd.getinitmethodname ); INit方法名称!=空! (isinitializingbean ' afterpropertiesset '.equals (init方法名称)! mbd.isexternallymanagedinitmethod (init method name ) /进入此方法时,将init-method方法invokecustominitmethod )、beanname,

mysql初始化data目录,redis主从配置