【spring】BeanFactory的实现

目录

        • 一、beanFactory
        • 二、代码示例
        • 三、后处理器排序
          • 3.1 说明
          • 3.2 代码示例

一、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);
	}

}

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 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));
	}

}

你可能感兴趣的:(spring,spring)