【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<String, BeanFactoryPostProcessor> 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);
	}

}

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();
	}
}
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;
	}
}
package com.learning.bean_factory;

public class BeanTwo{
	public BeanTwo(){
		System.out.println("beanTwo构造方法");
	}
}
三、后处理器排序
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;
  • 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");
	}

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<String, BeanFactoryPostProcessor> 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));
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值