配置類
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();
}
}