【Spring源码解析】生命周期-@Bean指定初始化和销毁方法 2021-03-09 spring bean, spring, 注解, 源码, 生命周期 0 评论 实现 定义bean 1234567891011121314public class Car { public Car() { System.out.println("car constructor..."); } public void init() { System.out.println("car init..."); } public void destroy() { System.out.println("car destory..."); }} 注册bean 12345678@Configurationpublic class BeanConfigOfLifeCycle { @Bean(initMethod = "init", destroyMethod = "destroy") public Car car() { return new Car(); }} 测试 1234567891011121314/** * car constructor... * car init... * 容器创建完成 * car destory... */@Testpublic void test06() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfigOfLifeCycle.class); System.out.println("容器创建完成"); // 关闭容器 applicationContext.close();} 最后 本篇到此结束,欢迎大家关注公众号【当我遇上你】。