BeanPostProcessor
在Bean的初始化阶段有前置和后置方法,这个方法是通过BeanPostProcessor来管理的,下面我们对原有的项目结构做小小的更改。
对启动类作出修改,先检查有没有BeanPostProcessor的实现类,有的话就使用,没有就使用默认的。
// 2.判断是否是postProcessorfor (String name : allJavaFile) {Class<?> clazz = beanScanner.getClazz(name);if (beanScanner.isImplBeanPostProcessor(clazz)){Object bean = beanFactory.createBean(clazz);beanPostProcessor = (BeanPostProcessor) bean;beanFactory.getCreatedBeanSet().add(bean);}}if (beanPostProcessor == null){beanPostProcessor = new DefaultBeanPostProcessor();}
第二次循环先检查是不是postProcessor,是的话就跳过就行
// 3.判断是否有Componentfor (String name : allJavaFile) {if (beanScanner.isComponent(name)) {// 4.是Component就开始bean的生命周期Class<?> clazz = beanScanner.getClazz(name);boolean flag = false;for (Object o : beanFactory.getCreatedBeanSet()) {if (o.getClass().equals(clazz)) {flag = true;break;}}if (flag){continue;}Object bean = beanFactory.createBean(clazz);String beanName = beanFactory.parseBeanName(bean);// 查看@Value注解// 1.找到所有@Value的字段......
最后执行这些代码
// initbeanFactory.getBeanMap().put(beanName,bean);beanPostProcessor.postProcessBeforeInitialization(bean);beanFactory.doInitialization(bean,beanScanner);beanPostProcessor.postProcessAfterInitialization(bean);
BeanFactory
这些方法都大差不差,思路就是找注解/接口,去invoke执行方法
/*** 进行初始化** @param bean 豆* @throws Exception 例外*/public void doInitialization(Object bean, BeanScanner beanScanner) throws Exception {// @PostConstructList<Method> postConstruct = beanScanner.getPostConstruct(bean);for (Method method : postConstruct) {method.invoke(bean);}// InitializingBeanif (beanScanner.isImplInitializing(bean)) {doInitialize(bean);}}
测试类
package com.ez4sterben.spring.test;import com.ez4sterben.spring.ioc.annotation.Component;
import com.ez4sterben.spring.ioc.factory.BeanPostProcessor;/*** 测试bean后处理器** @author ez4sterben* @date 2023/09/02*/
@Component
public class TestBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean) {System.out.println("postProcessBeforeInitialization");return BeanPostProcessor.super.postProcessBeforeInitialization(bean);}@Overridepublic Object postProcessAfterInitialization(Object bean) {System.out.println("postProcessAfterInitialization");return BeanPostProcessor.super.postProcessAfterInitialization(bean);}
}
package com.ez4sterben.spring.test;import com.ez4sterben.spring.ioc.annotation.Component;
import com.ez4sterben.spring.ioc.annotation.Value;
import com.ez4sterben.spring.ioc.context.ApplicationContext;
import com.ez4sterben.spring.ioc.factory.BeanPostProcessor;
import com.ez4sterben.spring.ioc.factory.InitializingBean;
import com.ez4sterben.spring.ioc.factory.aware.ApplicationContextAware;
import com.ez4sterben.spring.ioc.factory.aware.BeanFactoryAware;
import com.ez4sterben.spring.ioc.factory.aware.BeanNameAware;
import com.ez4sterben.spring.ioc.factory.support.BeanFactory;
import lombok.Data;import javax.annotation.PostConstruct;/*** 测试组件** @author ez4sterben* @date 2023/09/01*/
@Component
@Data
public class TestComponent implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean {@Value("ez4sterben")private String name;private String age;@Overridepublic void setBeanName(String beanName) {System.out.println(beanName);}@PostConstructpublic void init(){System.out.println("postConstruct");}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) {System.out.println(applicationContext.getBeanMap().toString());}@Overridepublic void setBeanFactory(BeanFactory beanFactory) {System.out.println(beanFactory.getBeanMap().toString());}@Overridepublic void afterPropertiesSet() {System.out.println("afterPropertiesSet");}
}
结果