DefaultListableBeanFactory-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

DefaultListableBeanFactory 是一个完整的、功能成熟的 IoC 容器如果你的需求很简单甚至可以直接使用 DefaultListableBeanFactory如果你的需求比较复杂那么通过扩展 DefaultListableBeanFactory 的功能也可以达到可以说 DefaultListableBeanFactory 是整个 Spring IoC 容器的始祖。

新建两个实体类User和UserMessage类和一个配置类MyConfig

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserMessage {

    private String phone;
    private String address;
}


@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
   private String username;

   @Autowired
   private UserMessage userMessage;
}


public class MyConfig {

    @Bean
    public User user(){
        return new User();
    }

    @Bean
    public UserMessage userMessage(){
        return new UserMessage("17860397188","123456");
    }
}

 动态注册 Bean这是 DefaultListableBeanFactory 的功能之一不过准确来说应该是动态注册 BeanDefinition 。

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(MyConfig.class).getBeanDefinition();
        beanFactory.registerBeanDefinition("config",beanDefinition);

        AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory); //为beanfactory添加后处理器
        //beanfactory 后处理器解析@Bean等注解
        Map<String, BeanFactoryPostProcessor> beansOfType = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        beansOfType.values().forEach(i->i.postProcessBeanFactory(beanFactory));
        //Bean后处理器针对Bean生命周期的各个阶段提供扩展例如@Autowired
        beanFactory.getBeansOfType(BeanPostProcessor.class).values().forEach(beanFactory::addBeanPostProcessor);

        Arrays.stream(beanFactory.getBeanDefinitionNames()).forEach(i-> System.out.println(i));
        User user = (User) beanFactory.getBean("user");
        System.out.println(user.getUserMessage());

Bean 的懒加载。这个时候先把 BeanDefinition 定义好等到真正调用 Bean 的时候才会去初始化 Bean。在 DefaultListableBeanFactory 中还有一个 preInstantiateSingletons 方法可以提前注册 Bean该方法是在 ConfigurableListableBeanFactory 接口中声明的DefaultListableBeanFactory 类实现了 ConfigurableListableBeanFactory 接口并实现了接口中的方法。

@Override
public void preInstantiateSingletons() throws BeansException {
	if (logger.isTraceEnabled()) {
		logger.trace("Pre-instantiating singletons in " + this);
	}
	// Iterate over a copy to allow for init methods which in turn register new bean definitions.
	// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
	List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
	// Trigger initialization of all non-lazy singleton beans...
	for (String beanName : beanNames) {
		RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
		if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
			if (isFactoryBean(beanName)) {
				Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
				if (bean instanceof FactoryBean) {
					final FactoryBean<?> factory = (FactoryBean<?>) bean;
					boolean isEagerInit;
					if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
						isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
										((SmartFactoryBean<?>) factory)::isEagerInit,
								getAccessControlContext());
					}
					else {
						isEagerInit = (factory instanceof SmartFactoryBean &&
								((SmartFactoryBean<?>) factory).isEagerInit());
					}
					if (isEagerInit) {
						getBean(beanName);
					}
				}
			}
			else {
				getBean(beanName);
			}
		}
	}
	// Trigger post-initialization callback for all applicable beans...
	for (String beanName : beanNames) {
		Object singletonInstance = getSingleton(beanName);
		if (singletonInstance instanceof SmartInitializingSingleton) {
			final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
					smartSingleton.afterSingletonsInstantiated();
					return null;
				}, getAccessControlContext());
			}
			else {
				smartSingleton.afterSingletonsInstantiated();
			}
		}
	}
}

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6