beanFactory不会做的事情
bean后处理器会有排序的逻辑,后处理都实现order接口实现的,有的是解析@Autowired注解有的是解析@Resource注解
- package com.example;
-
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
- import org.springframework.beans.factory.config.BeanPostProcessor;
- import org.springframework.beans.factory.support.AbstractBeanDefinition;
- import org.springframework.beans.factory.support.BeanDefinitionBuilder;
- import org.springframework.beans.factory.support.DefaultListableBeanFactory;
- import org.springframework.context.annotation.AnnotationConfigUtils;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import java.util.logging.Logger;
-
- public class TestBeanFactory {
- public static void main(String[] args) {
- DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
- // 为beanFactory添加一些bean的定义,beanFactory根据添加的bean的定义来传创建bean对象
- // 定义包括:bean的类型(class)、bean的作用域(scope)、bean是否含有初始化、销毁
- AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton").getBeanDefinition();
- beanFactory.registerBeanDefinition("config", beanDefinition);
- for (String name : beanFactory.getBeanDefinitionNames()) {
- System.out.println(name); // config todo 目前beanFactory还缺少解析注解的功能
- }
- // 给beanFactory添加一些常用的后处理器
- AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
- for (String name : beanFactory.getBeanDefinitionNames()) {
- System.out.println(name); /*config
- org.springframework.context.annotation.internalConfigurationAnnotationProcessor
- org.springframework.context.annotation.internalAutowiredAnnotationProcessor
- org.springframework.context.annotation.internalCommonAnnotationProcessor
- org.springframework.context.event.internalEventListenerProcessor
- org.springframework.context.event.internalEventListenerFactory*/
- }
- // 获取beanFactory后处理器,补充了一些bean的定义
- beanFactory.getBeansOfType(BeanFactoryPostProcessor.class).values().stream().forEach(beanFactoryPostProcessor -> {
- beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
- });
- // bean的后处理器,针对bean的生命周期的各个阶段来拓展一些功能,例如@Autowired、@Resource....
- beanFactory.getBeansOfType(BeanPostProcessor.class).values().stream().forEach(beanFactory::addBeanPostProcessor);
- for (String name : beanFactory.getBeanDefinitionNames()) {
- System.out.println(name);
- }/*config
- org.springframework.context.annotation.internalConfigurationAnnotationProcessor
- org.springframework.context.annotation.internalAutowiredAnnotationProcessor
- org.springframework.context.annotation.internalCommonAnnotationProcessor
- org.springframework.context.event.internalEventListenerProcessor
- org.springframework.context.event.internalEventListenerFactory
- bean1
- bean2*/
- beanFactory.preInstantiateSingletons();// 准备好所有单例。初始化单例对象
- System.out.println("+++++++++++++++++++++++++++++++++++");
- System.out.println(beanFactory.getBean(Bean1.class).getBean2());
- }
-
- @Configuration
- static class Config {
- @Bean
- public Bean1 bean1() {
- return new Bean1();
- }
-
- @Bean
- public Bean2 bean2() {
- return new Bean2();
- }
- }
-
- static class Bean1 {
- private static final org.slf4j.Logger log = LoggerFactory.getLogger(Bean1.class);
-
- public Bean1() {
- log.info("构造Bean1()");
- }
-
- @Autowired
- private Bean2 bean2;
-
- public Bean2 getBean2() {
- return bean2;
- }
- }
-
- static class Bean2 {
- private static final org.slf4j.Logger log = LoggerFactory.getLogger(Bean2.class);
-
- public Bean2() {
- log.info("构造Bean2()");
- }
- }
- }