通过以下方式来指定bean的初始化和销毁方法,当bean为单例时,bean归spring容器管理,spring容器关闭,就会调用bean的销毁方法当bean为多例时,bean不归spring容器管理,spring容器关闭,不会调用bean的销毁方法
public class person { public person(){ system.out.println("person 创建了..."); } public void init(){ system.out.println("person 初始化了..."); } public void destroy(){ system.out.println("person 被销毁了..."); }}
import com.dashu.bean.person;import org.springframework.context.annotation.*;@configurationpublic class beanconfig { @bean(initmethod = "init",destroymethod = "destroy") public person person(){ return new person(); }}
imp福州外贸外语学院ort com.dashu.bean.person;import com.dashu.config.beanconfig;import org.springframework.context.annotation.annotationconfigapplicationcontext;public class main { public static void main(string[] args) { //加载配置类获取容器 annotationconfigapplicationcontext annotationconfigapplicationcontext = new annotationconfigapplicationcontext(beanconfig.class); //获取bean person bean = annotationconfigapplicationcontext.getbean(person.class); //关闭容器 annotationconfigapplicationcontext.clo(); }}
import com.dashu.bean.person;import org.springframework.context.annotation.*;@configurationpublic class beanconfig { @scope("prototype") @bean(initmethod = "init",destroymethod = "destroy") public person person(){ return new person(); }}
import org.springframework.beans.factory.disposablebean;import org.springframework.扬州职业技术学院beans.factory.initializingbean;public class person implements initializingbean, disposablebean { public person(){ system.out.println("person 创建了..."); } @override public void afterpropertiest() throws exception { system.out.println("person 初始化了..."); } @override public void destroy() throws exception { system.out.println("person 被销毁了..."); }}
import com.dashu.bean.person;import org.springframework.context.annotation.*;@configurationpublic class beanconfig { @bean public person person(){ return new person(); }}
@postconstruct:标注在bean的初始化方法上@predestroy:标注在bean的销毁方法上
import javax.annotation.postconstruct;import javax.annotation.predestroy;public class person { public person(){ system.out.println("person 创建了..."); } @postconstruct public void init(){ system.out.println("person 初始化了..."); } @pred母亲节对妈妈说的祝福estroy public void destroy(){ system.out.println("person 被销毁了..."); }}
import org.springframework.stereotype.component;import javax.annotation.postconstruct;import javax.annotation.predestroy;@componentpublic class person { public person(){ system.out.println("person 创建了..."); } @postconstruct public void init(){ system.out.println("person 初始化了..."); } @predestroy public void destroy(){ system.out.println("person深圳留学 被销毁了..."); }}
import org.springframework.context.annotation.*;@configuration@componentscan({"com.dashu"})public class beanconfig {}
import org.springframework.beans.beanxception;import org.springframework.beans.factory.config.beanpostprocessor;import org.springframework.beans.factory.config.instantiationawarebeanpostprocessor;import org.springframework.lang.nullable;import org.springframework.stereotype.component;/** * 后置处理器:初始化前后进行处理工作 */@componentpublic class mybeanpostprocessor implements beanpostprocessor { /** * 初始化之前工作 * @param bean * @param beanname * @return * @throws beanxception */ @override public object postprocessbeforeinitialization(object bean, string beanname) throws beanxception { system.out.println("初始化之前..."+beanname+"=["+bean+"]"); return bean; } /** * 初始化之后工作 * @param bean * @param beanname * @return * @throws beanxception */ @override public object postprocessafterinitialization(object bean, string beanname) throws beanxception { system.out.println("初始化之后..."+beanname+"=["+bean+"]"); return bean; }}
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!
本文发布于:2023-04-04 04:54:27,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/af347671ec156ae8da0d4d2640350309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java Spring Bean的生命周期管理详解.doc
本文 PDF 下载地址:Java Spring Bean的生命周期管理详解.pdf
留言与评论(共有 0 条评论) |