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构造方法");
}
}
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
private int order = Ordered.LOWEST_PRECEDENCE - 2;
public CommonAnnotationBeanPostProcessor() {
// 设置顺序为最低 2147483644
setOrder(Ordered.LOWEST_PRECEDENCE - 3);
setInitAnnotationType(PostConstruct.class);
setDestroyAnnotationType(PreDestroy.class);
ignoreResourceType("javax.xml.ws.WebServiceContext");
}
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));
}
}
更多【spring-【spring】BeanFactory的实现】相关视频教程:www.yxfzedu.com