Spring学习笔记之bean生命周期
程序员文章站
2023-12-09 14:38:51
前言
主要学习了下bean的配置、注入、自定义属性编辑器,今天来熟悉bean的生命周期。
任何一个事物都有自己的生命周期,生命的开始、生命中、生命结束。大家最熟悉的应该...
前言
主要学习了下bean的配置、注入、自定义属性编辑器,今天来熟悉bean的生命周期。
任何一个事物都有自己的生命周期,生命的开始、生命中、生命结束。大家最熟悉的应该是servlet 的生命周期吧。和 servlet 一样 spring bean 也有自己的生命周期。
在开发中生命周期是一个很常见的名词,基本每种编程语言都能找到与它关联的。关于bean的生命周期我在网上也找了好多,基本都差不多。这里我主要是想通过代码来验证,毕竟学的知识都是一样的,都是学的java,最重要的是动手练习,这样印象更深。
下面是它生命周期的描述,我们通过demo来进行验证。
下图是它执行的顺序。
一、创建liftcycle类实现beanfactoryaware,beannameaware,initializingbean,disposablebean,applicationcontextaware5个接口方法
package cuiyw.spring.service; import org.springframework.beans.beansexception; import org.springframework.beans.factory.beanfactory; import org.springframework.beans.factory.beanfactoryaware; import org.springframework.beans.factory.beannameaware; import org.springframework.beans.factory.disposablebean; import org.springframework.beans.factory.initializingbean; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; public class lifecycle implements beanfactoryaware,beannameaware,initializingbean,disposablebean,applicationcontextaware{ private string name; public string getname() { system.out.println("getname name="+name); return name; } public void setname(string name) { system.out.println("setname name="+name); this.name = name; } public void afterpropertiesset() throws exception { // todo auto-generated method stub system.out.println("initializingbean.afterpropertiesset()"); } public void setbeanname(string arg0) { // todo auto-generated method stub system.out.println("beannameaware.setbeanname"); } public void setbeanfactory(beanfactory arg0) throws beansexception { // todo auto-generated method stub system.out.println("beanfactoryaware.setbeanfactory"); } public void destroy() throws exception { // todo auto-generated method stub system.out.println("disposablebean.destroy"); } public void myinit() { system.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法"); } public void mydestory() { system.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法"); } public void setapplicationcontext(applicationcontext arg0) throws beansexception { // todo auto-generated method stub system.out.println("applicationcontextaware.setapplicationcontext"); } }
二、注册instantiationawarebeanpostprocessor接口
package cuiyw.spring.service; import java.beans.propertydescriptor; import org.springframework.beans.beansexception; import org.springframework.beans.propertyvalues; import org.springframework.beans.factory.config.instantiationawarebeanpostprocessor; public class myinstantiationawarebeanpostprocessor implements instantiationawarebeanpostprocessor{ public object postprocessafterinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("instantiationawarebeanpostprocessor.postprocessafterinitialization"); return bean; } public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("instantiationawarebeanpostprocessor.postprocessbeforeinitialization"); return bean; } public boolean postprocessafterinstantiation(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("instantiationawarebeanpostprocessor.postprocessafterinstantiation"); return true; } public object postprocessbeforeinstantiation(class<?> beanclass, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("instantiationawarebeanpostprocessor.postprocessbeforeinstantiation"); return null; } public propertyvalues postprocesspropertyvalues(propertyvalues pvs, propertydescriptor[] pds, object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("instantiationawarebeanpostprocessor.postprocesspropertyvalues"); return pvs; } }
三、注册beanpostprocessor接口
其实instantiationawarebeanpostprocessor继承beanpostprocessor,所以在上面我也实现了beanpostprocessor接口的方法
package cuiyw.spring.service; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanpostprocessor; public class mybeanpostprocessor implements beanpostprocessor { public object postprocessafterinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("beanpostprocessor.postprocessafterinitialization "); return bean; } public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println("beanpostprocessor.postprocessbeforeinitialization"); return bean; } }
四、注册beanfactorypostprocessor接口
package cuiyw.spring.service; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanfactorypostprocessor; import org.springframework.beans.factory.config.configurablelistablebeanfactory; public class mybeanfactorypostprocessor implements beanfactorypostprocessor { public void postprocessbeanfactory(configurablelistablebeanfactory arg0) throws beansexception { // todo auto-generated method stub system.out.println("beanfactorypostprocessor.postprocessbeanfactory"); } }
五、在上下文中配置
这里还是在上一个博客demo的基础上进行修改,为了有其他干扰,我先把service去掉了。
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="beanpostprocessor" class="cuiyw.spring.service.mybeanpostprocessor"></bean> <bean id="instantiationawarebeanpostprocessor" class="cuiyw.spring.service.myinstantiationawarebeanpostprocessor"></bean> <bean id="beanfactorypostprocessor" class="cuiyw.spring.service.mybeanfactorypostprocessor"></bean> <bean id="lifecycle" class="cuiyw.spring.service.lifecycle" init-method="myinit" destroy-method="mydestory"> <property name="name" value="cuiyw1"></property> </bean> </beans>
六、在main中使用bean
package cuiyw.springaop; import org.springframework.beans.factory.beanfactory; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import cuiyw.spring.iservice.iservice; import cuiyw.spring.service.lifecycle; public class app { public static void main( string[] args ) { applicationcontext context=new classpathxmlapplicationcontext(new string[]{"applicationcontext.xml"}); beanfactory factory=context; lifecycle lifecycle=factory.getbean("lifecycle",lifecycle.class); lifecycle.setname("cuiyw2"); system.out.println("lifecycle.name="+lifecycle.getname()); ((classpathxmlapplicationcontext)factory).registershutdownhook(); /*service=(iservice)factory.getbean("servicea"); service.service("cuiyw servicea"); service=(iservice)factory.getbean("serviceimpl"); service.service("cuiyw serviceimpl"); */ } }
七、输入打印结果
可以发现输出的顺序和上面图的顺序基本一致。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。