Spring底层原理(三)
Bean
的生命周期
@SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); context.close(); } } @Slf4j @Component public class LifeCycleBean { public LifeCycleBean(){ log.info("构造"); } @Autowired public void autowire(@Value("${JAVA_HOME}") String home){ log.info("依赖注入:{}",home); } @PostConstruct public void init(){ log.info("初始化"); } @PreDestroy public void destroy(){ log.info("销毁"); } }
启动容器后会得到以下结果
Bean的生命周期为:构造方法 -> 依赖注入->初始化 ->销毁
与Bean生命周期相关的后置处理器
PostProcessor
中文意思为后置处理器
InstantiationAwareBeanPostProcessor
与DestructionAwareBeanPostProcessor
都是BeanPostProcessor
的子接口
@Slf4j @Component public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor { @Override public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException { if (beanName.equals("lifeCycleBean")){ log.info("<<<< 销毁前执行,如@PreDestory"); } } @Override public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { if (beanName.equals("lifeCycleBean")){ log.debug("<<<< 实例化之前执行,这里返回的对象会替换掉原本的bean"); } return null; } @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { if (beanName.equals("lifeCycleBean")){ log.debug("<<<< 实例化后执行,这里返回false会跳过依赖注入阶段"); } return true; } @Override public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { if (beanName.equals("lifeCycleBean")){ log.debug("<<<< 依赖注入阶段执行,如@Autowired、@Value、@Resource"); } return pvs; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (beanName.equals("lifeCycleBean")){ log.debug("<<<< 初始化之前执行,这里返回的对象会替换掉原本的bean,如@PostConstruct、@ConfigurationProperties"); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (beanName.equals("lifeCycleBean")){ log.debug("<<<< 初始化之后执行,这里返回的对象会替换掉原本的bean,如代理增强"); } return bean; } }
Bean
后置处理器与模板方法模式
public class MethodTemplateTest { public static void main(String[] args) { MyBeanFactory factory = new MyBeanFactory(); factory.addProcessor(bean -> System.out.println("解析@Autowired")); factory.addProcessor(bean -> System.out.println("解析@Resources")); factory.getBean(); } static class MyBeanFactory { private List<BeanPostProcessor> processors = new ArrayList<>(); public void addProcessor(BeanPostProcessor postProcessor) { processors.add(postProcessor); } public Object getBean() { Object bean = new Object(); System.out.println("构造:" + bean); System.out.println("依赖注入:" + bean); for (BeanPostProcessor processor : processors) { processor.inject(bean); } System.out.println("初始化:" + bean); return bean; } } interface BeanPostProcessor { void inject(Object bean);//对依赖注入阶段进行拓展 } }
-
将代码中不确定的部分抽象成接口,供后续拓展
常见的bean
后置处理器
public class A4Application { public static void main(String[] args) { //GenericApplicationContext是一个干净的容器,没有添加BeanFactory后置处理器和Bean后置处理器 GenericApplicationContext context = new GenericApplicationContext(); //注册bean context.registerBean("bean1",Bean1.class); context.registerBean("bean2",Bean2.class); context.registerBean("bean3",Bean3.class); //初始化容器 context.refresh();//执行beanFactory后置处理器,添加bean后置处理器,初始化所有单例bean //销毁容器 context.close(); } }
添加AutowiredAnnotationBeanPostProcessor
context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver()); context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
-
ContextAnnotationAutowireCandidateResolver
:用于支持解析@Value
-
AutowiredAnnotationBeanPostProcessor
:该处理器用于解析@Autowired
@Value
添加CommonAnnotationBeanPostProcessor
context.registerBean(CommonAnnotationBeanPostProcessor.class);
-
CommonAnnotationBeanPostProcessor
:用于解析@Resurce
@PostConstruct
@PreDestroy
文章来源:https://www.toymoban.com/news/detail-721030.html
ConfigurationPropertiesBindingPostProcessor.register(context);
-
ConfigurationPropertiesBindingPostProcessor
:用于解析@ConfigurationProperties
文章来源地址https://www.toymoban.com/news/detail-721030.html
到了这里,关于Spring底层原理(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!