• 【spring】BeanFactory的实现


    一、beanFactory
    • 1.不会主动调用BeanFactory后处理器
    • 2.不会主动添加Bean后处理器
    • 3.不会主动初始化单例
    • 4.不会解析${}占位符和#{}表达式
    • 5.bean后处理器会有排序的逻辑
    二、代码示例
    package com.learning.bean_factory;
    
    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 java.util.Map;
    
    public class TestBeanFactory {
    	public static void main(String[] args) {
    		DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
    		// bean的定义,描述bean的类型、单多例、初始化、销毁
    		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton");
    		AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
    		defaultListableBeanFactory.registerBeanDefinition("config", beanDefinition);
    
    		// 给BeanFactory添加一些常用的后置处理器,解析@Configuration @Bean等注解
    		AnnotationConfigUtils.registerAnnotationConfigProcessors(defaultListableBeanFactory);
    
    		// BeanFactory的后置处理器主要功能补充了一些bean定义
    		Map beanFactoryPostProcessorMap = defaultListableBeanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
    		beanFactoryPostProcessorMap.values().forEach(beanFactoryPostProcessor ->{
    			beanFactoryPostProcessor.postProcessBeanFactory(defaultListableBeanFactory);
    		});
    
    		String[] beanDefinitionNames = defaultListableBeanFactory.getBeanDefinitionNames();
    		for (String beanDefinitionName : beanDefinitionNames) {
    			System.out.println(beanDefinitionName);
    		}
    
    //		BeanOne beanOne = defaultListableBeanFactory.getBean(BeanOne.class);
    //		System.out.println(beanOne);
    //
    //		BeanTwo beanTwo = beanOne.getBeanTwo();
    //		System.out.println(beanTwo);
    
    		// Bean的后置处理器,针对bean的生命周期的各个阶段提供扩展,例如依赖注入的注解 @Autowired @Resource
    		defaultListableBeanFactory.getBeansOfType(BeanPostProcessor.class).values().forEach(defaultListableBeanFactory::addBeanPostProcessor);
    		// 准备好所有单例
    		defaultListableBeanFactory.preInstantiateSingletons();
    
    		BeanOne beanOne = defaultListableBeanFactory.getBean(BeanOne.class);
    		System.out.println(beanOne);
    
    		BeanTwo beanTwo = beanOne.getBeanTwo();
    		System.out.println(beanTwo);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    package com.learning.bean_factory;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class Config{
    	@Bean
    	public BeanOne beanOne(){
    		return new BeanOne();
    	}
    	@Bean
    	public BeanTwo beanTwo(){
    		return new BeanTwo();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    package com.learning.bean_factory;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class BeanOne{
    	@Autowired
    	private BeanTwo beanTwo;
    	public BeanOne(){
    		System.out.println("beanOne构造方法");
    	}
    
    	public BeanTwo getBeanTwo(){
    		return this.beanTwo;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    package com.learning.bean_factory;
    
    public class BeanTwo{
    	public BeanTwo(){
    		System.out.println("beanTwo构造方法");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    三、后处理器排序
    3.1 说明
    • 1. BeanPostProcessor中order属性,数值越大,优先级越低
    • 2. AutowiredAnnotationBeanPostProcessor的order为 2^31-1-2即:2147483647-2 = 2147483645
    int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
    private int order = Ordered.LOWEST_PRECEDENCE - 2;
    
    • 1
    • 2
    • 3. CommonAnnotationBeanPostProcessor的order属性为Ordered.LOWEST_PRECEDENCE - 3,即:2^31 - 1 - 3 = 2147483644
    public CommonAnnotationBeanPostProcessor() {
    		// 设置顺序为最低 2147483644
    		setOrder(Ordered.LOWEST_PRECEDENCE - 3);
    		setInitAnnotationType(PostConstruct.class);
    		setDestroyAnnotationType(PreDestroy.class);
    		ignoreResourceType("javax.xml.ws.WebServiceContext");
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    3.2 代码示例
    package com.learning.bean_factory;
    
    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.core.Ordered;
    
    import java.util.Map;
    
    public class TestBeanFactory {
    	public static void main(String[] args) {
    		DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
    		// bean的定义,描述bean的类型、单多例、初始化、销毁
    		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(Config.class).setScope("singleton");
    		AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
    		defaultListableBeanFactory.registerBeanDefinition("config", beanDefinition);
    
    		// 给BeanFactory添加一些常用的后置处理器,解析@Configuration @Bean等注解
    		AnnotationConfigUtils.registerAnnotationConfigProcessors(defaultListableBeanFactory);
    
    		// BeanFactory的后置处理器主要功能补充了一些bean定义
    		Map beanFactoryPostProcessorMap = defaultListableBeanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
    		beanFactoryPostProcessorMap.values().forEach(beanFactoryPostProcessor ->{
    			beanFactoryPostProcessor.postProcessBeanFactory(defaultListableBeanFactory);
    		});
    
    		String[] beanDefinitionNames = defaultListableBeanFactory.getBeanDefinitionNames();
    		for (String beanDefinitionName : beanDefinitionNames) {
    			System.out.println(beanDefinitionName);
    		}
    
    		// Bean的后置处理器,针对bean的生命周期的各个阶段提供扩展,例如依赖注入的注解 @Autowired @Resource
    		defaultListableBeanFactory.getBeansOfType(BeanPostProcessor.class).values().stream()
    				.sorted(defaultListableBeanFactory.getDependencyComparator())
    				.forEach(beanPostProcessor -> {
    					System.out.println("beanPostProcessor-->" + beanPostProcessor);
    					defaultListableBeanFactory.addBeanPostProcessor(beanPostProcessor);
    				});
    
    		System.out.println("CommonAnnotationBeanPostProcessor Order->" + (Ordered.LOWEST_PRECEDENCE -3));
    		System.out.println("AutowiredAnnotationBeanPostProcessor Order->" + (Ordered.LOWEST_PRECEDENCE -2));
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法
    C语言第十八课:初阶结构体
    基于GCC的工具objdump实现反汇编
    安杰思医学冲刺科创板:​年营收3亿 拟募资7.7亿
    Kafka安装启动(含安装包)
    Android的多线程和异步处理
    flutter Chip 组件
    在AOSP中根据设备特性进行个性化定制:利用getPackageManager().hasSystemFeature()接口实现
    内卷?泡沫?变革?十个问题直击“元宇宙”核心困惑丨《问Ta-王雷元宇宙时间》精华实录...
    T1064 奥运奖牌计数(信息学一本通C++)
  • 原文地址:https://blog.csdn.net/qq_32088869/article/details/134032982