配置類
package com.ysy.config;
import com.ysy.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author shanyangyang
* @date 2020/5/31
* Bean生命週期:
* Bean創建—————初始化——————銷燬的過程
* 容器管理Bean的生命週期:
* 我們可以自定義初始化和銷燬方法,容器在進行到對應的步驟時,會調用我們自定義的方法
* 1)指定初始化和銷燬方法
* 通過@Bean指定init-method="getTestStr" destroy-method="getTestStr"
*
* 2)通過讓bean實現InitializingBean接口和DisposableBean接口
*
* 3)通過使用JSR250
* 1)@PostConstruct:在Bean創建完成並且屬性賦值完成,來執行初始化方法;
* 2)@PreDestroy:在容器銷燬Bean之前通知進行清理工作;
*
* 4)BeanPostProcessor:bean的後置處理器
* 在Bean的初始化前後進行一些處理工作
* postProcessBeforeInitialization:在初始化調用之前
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method).
* postProcessAfterInitialization:在初始化之後
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
* * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* * or a custom init-method).
*
*構造(對象實例)
* 1)單實例:在容器啓動的時候創建對象
* 2)多實例:在每次獲取的時候創建對象
*
* BeanPostProcessor.postProcessBeforeInitialization
*初始化:
* 對象創建完成,並賦值完成,調用初始化方法。。。。
*
* BeanPostProcessor.postProcessAfterInitialization
* 銷燬:
* 單實例:容器關閉的時候
* 多實例:容器不會管理這個Bean,容器不會調用銷燬方法
*
* populateBean之後執行下面的方法:
* 給Bean屬性賦值
*
*遍歷得到容器所有的BeanPostProcessor,挨個執行BeforeInitialization,一旦返回null,跳出for循環,不會執行後面的BeanPostProcessor
* if (mbd == null || !mbd.isSynthetic()) {
* wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
* }
*
* try {
* //執行初始化方法
* invokeInitMethods(beanName, wrappedBean, mbd);
* }
* catch (Throwable ex) {
* throw new BeanCreationException(
* (mbd != null ? mbd.getResourceDescription() : null),
* beanName, "Invocation of init method failed", ex);
* }
* if (mbd == null || !mbd.isSynthetic()) {
* wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
* }
*
*
* Spring底層對BeanPostProcessor的使用,
* bean賦值,注入其他組件,@Autowired,生命週期註解功能,@Sync,等等;
*
*/
@Configuration
@ComponentScan("com.ysy.bean")
public class MainConfigOfLifeCycle {
//@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public Car car(){
return new Car();
}
}
後置處理器類
package com.ysy.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
* 後置處理器:初始化前後進行處理
* bean the new bean instance
* beanName the name of the bean
* @author shanyangyang
* @date 2020/5/31
*/
@Component
public class MyBeanPostProccessor implements BeanPostProcessor {
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization......."+beanName+".....>>"+bean);
return bean;
}
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization......."+beanName+".....>>"+bean);
return bean;
}
}
測試類
package com.ysy.test;
import com.ysy.config.MainConfigOfLifeCycle;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author shanyangyang
* @date 2020/5/31
*/
public class IOCTestLifeCycle {
@Test
public void test01(){
//創建IOC容器
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("容器創建完成。。。。");
context.getBean("car");
context.close();
}
}