什么是bean的生命周期?
生活随笔
收集整理的這篇文章主要介紹了
什么是bean的生命周期?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
bean的創建----->初始化----->銷毀方法
由容器管理Bean的生命周期,我們可以通過自己指定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();} }針對單實例bean的話,容器啟動的時候,bean的對象就創建了,而且容器銷毀的時候,也會調用Bean的銷毀方法
針對多實例bean的話,容器啟動的時候,bean是不會被創建的而是在獲取bean的時候被創建,而且bean的銷毀不受IOC容器的管理.
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的什么是bean的生命周期?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 往IOC 容器中添加组件的方式
- 下一篇: 通过@Value + @Property