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();
}
@Bean
public leonBeanPostProcessor leonBeanPostProcessor() {
return new leonBeanPostProcessor();
}
}
针对单实例bean的话,容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法
针对多实例bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受IOC容器的管理.