概述
针对单实例bean的话,容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法
针对原型bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受 IOC容器的管理.
方式一: 自行指定bean的初始化方法和bean的销毁方法
【beans】
package com.artisan.base.lifeCycle; public class A1 { public void init(){ System.out.println("A1 init"); } public void destory(){ System.out.println("A1 destory"); } }
package com.artisan.base.lifeCycle; public class A2 { public void init(){ System.out.println("A2 init"); } public void destory(){ System.out.println("A2 destory"); } }
【配置类】
package com.artisan.base.lifeCycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration public class LCConfig { @Bean(initMethod = "init",destroyMethod = "destory") public A1 a1(){ return new A1(); } @Scope("prototype") @Bean public A2 a2(){ return new A2(); } }
【测试】
package com.artisan.base.lifeCycle; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.concurrent.TimeUnit; /** * @author 小工匠 * @version 1.0 * @description: TODO * @date 2020/10/11 22:58 * @mark: show me the code , change the world */ public class LCTest { public static void main(String[] args) throws InterruptedException { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(LCConfig.class); TimeUnit.SECONDS.sleep(3); // 容器销毁 ac.close(); } }
方式二: 通过 InitializingBean和DisposableBean 接口实现bean的初始化以及销毁方法
package com.artisan.base.lifeCycle; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; /** * @author 小工匠 * @version 1.0 * @description: TODO * @date 2020/10/11 23:06 * @mark: show me the code , change the world */ public class A3 implements InitializingBean, DisposableBean { public A3() { System.out.println("A3 init"); } @Override public void destroy() throws Exception { System.out.println("A3 destroy"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("A3 afterPropertiesSet"); } }
执行上面的测试代码
方式三: 通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法
package com.artisan.base.lifeCycle; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; /** * @author 小工匠 * @version 1.0 * @description: TODO * @date 2020/10/11 23:12 * @mark: show me the code , change the world */ public class A4 { public A4() { System.out.println("A4 Construct "); } @PostConstruct public void init(){ System.out.println("A4 init"); } @PreDestroy public void destory(){ System.out.println("A4 destory"); } }
测试结果
方式四:通过Spring的BeanPostProcessor的 bean的后置处理器会拦截所有bean创建过程
public class A5 { public A5() { System.out.println("A5 Construct"); } public void init(){ System.out.println("A5 init"); } public void destory(){ System.out.println("A5 destory"); } }
【bean後置處理器】
package com.artisan.base.lifeCycle; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class A5BeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof A5){ System.out.println("A5BeanPostProcessor...postProcessBeforeInitialization:"+beanName); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof A5){ System.out.println("A5BeanPostProcessor...postProcessAfterInitialization:"+beanName); } return bean; } }
【config】
【test】
简单说一下 BeanPostProcessor的执行时机
看看invokeInitMethods