欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring《二》 Bean的生命周期

发布时间:2025/7/14 javascript 62 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring《二》 Bean的生命周期 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Bean初始化

1、bean中实现public void init();方法,config.xml中增加init-method="init" 属性。

2、bean实现接口InitializingBean,实现方法afterPropertiesSet,配置文件无需改动。

Bean的使用

1、

HelloWorld helloWorld=new HelloWorld();

BeanWrapper bw=new BeanWrapperImpl(helloWorld);

bw.setPropertyValue("msg","HelloWorld");

2、

InputStream is=new FileInputStream("config.xml");

XmlBeanFactory factory=new XmlBeanFactory(is);

HelloWorld helloWorld=(HelloWorld)factory.getBean("HelloWorld");

3、

ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");

HelloWorld hw=(HelloWorld)actx.getBean("HelloWorld");

Bean的销毁

1、bean中实现方法public void cleanup();,bean配置中增加destroy-method="cleanup" 属性

2、bean类实现DisposableBean,并复写public void destroy();配置文档无需变动。

转载于:https://www.cnblogs.com/swordyt/p/7001144.html

总结

以上是生活随笔为你收集整理的Spring《二》 Bean的生命周期的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。