@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
Autowire autowire() default Autowire.NO;
/**
* Bean的初始化方法
*/
String initMethod() default "";
/**
* Bean的销毁方法
*/
String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
}
(1)、Student
/**
* @Description:
* @Auther: zhurongsheng
* @Date: 2020/2/27 21:14
*/
public class Student {
public Student(){
System.out.println("Student Construct");
}
public void init(){
System.out.println("Student init");
}
public void destroy(){
System.out.println("Student destroy");
}
}
(2)、配置文件
/**
* @Description:
* @Auther: zhurongsheng
* @Date: 2020/2/27 21:11
*/
@Configuration
public class SpringConfig {
/**
*initMethod:bean的初始化方法
*destroyMethod: bean的销毁方法
*/
@Bean(initMethod = "init",destroyMethod = "destroy")
public Student student(){
return new Student();
}
}
(3)、运行类
public class SpringApplicationContext {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(SpringConfig.class);
applicationContext.close();
}
}
/**
* BeanPostProcessor 用于增强Bean
*/
public interface BeanPostProcessor {
/**
* 在Bean的init方法之前调用
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* 在Bean的init方法之后调用
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
(1)、创建 ZrsBeanPostProcessor
/**
* @Description: bean增强类
* @Auther: zhurongsheng
* @Date: 2020/2/27 21:24
*/
public class ZrsBeanPostProcessor implements BeanPostProcessor {
/**
* 功能描述 Bean的前置处理器
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + ":Bean的前置处理器");
return bean;
}
/**
* 功能描述 Bean的后置处理器
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + ":Bean的后置处理器");
return bean;
}
}
(2)、修改配置文件,把ZrsBeanPostProcessor加入到IOC中
/**
* @Description:
* @Auther: zhurongsheng
* @Date: 2020/2/27 21:11
*/
@Configuration
@Import(ZrsBeanPostProcessor.class)
public class SpringConfig {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Student student(){
return new Student();
}
}
(3)、 运行类
public class SpringApplicationContext {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(SpringConfig.class);
applicationContext.close();
}
}
3.1、AnnotationConfigApplicationContext 构造方法
3.2、AbstractApplicationContext refresh方法
3.3、AbstractApplicationContext finishBeanFactoryInitialization
3.4、DefaultListableBeanFactory preInstantiateSingletons()
3.5、AbstractBeanFactory doGetBean ,打断点找到name为Student的Bean
3.6、调用 AbstractAutowireCapableBeanFactory createBean方法
3.7、AbstractAutowireCapableBeanFactory doCreateBean 方法
3.8、AbstractAutowireCapableBeanFactory initializeBean 方法
3.9、打断点进入Bean的后置处理器
AbstractAutowireCapableBeanFactory applyBeanPostProcessorsAfterInitialization