什么是bean的生命周期?
生活随笔
收集整理的這篇文章主要介紹了
什么是bean的生命周期?
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
bean的創(chuàng)建----->初始化----->銷毀方法
由容器管理Bean的生命周期,我們可以通過(guò)自己指定bean的初始化方法和bean的銷毀方法
package com.leon.testbeanlifecycle;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope;@Configuration @ComponentScan(basePackages = "com.leon.testbeanlifecycle") public class MainConfig {@Scope(value = "prototype")@Bean(initMethod = "init",destroyMethod = "destroy")public Car car() {return new Car();}@Bean(initMethod = "init")public leonLog leonLog() {return new leonLog();}@Beanpublic leonBeanPostProcessor leonBeanPostProcessor() {return new leonBeanPostProcessor();} }針對(duì)單實(shí)例bean的話,容器啟動(dòng)的時(shí)候,bean的對(duì)象就創(chuàng)建了,而且容器銷毀的時(shí)候,也會(huì)調(diào)用Bean的銷毀方法
針對(duì)多實(shí)例bean的話,容器啟動(dòng)的時(shí)候,bean是不會(huì)被創(chuàng)建的而是在獲取bean的時(shí)候被創(chuàng)建,而且bean的銷毀不受IOC容器的管理.
?
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的什么是bean的生命周期?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 往IOC 容器中添加组件的方式
- 下一篇: 通过@Value + @Property