DefaultListableBeanFactory

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

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


相关文章
|
6月前
|
计算机视觉
detectMultiScale
【6月更文挑战第8天】
318 4
我应该使用 NULL 还是 0?
我应该使用 NULL 还是 0?
The Sandwich Rule
目标:训练一个可以直接以任意宽度运行的单一网络。其实是在权重共享的条件下,我们可以根据不同的硬件设备挑选不同宽度的网络,不再重训练一个权重。
127 0
The Sandwich Rule
|
机器人
DefaultRobotHWSim::initSim函数详解
DefaultRobotHWSim::initSim函数详解
DefaultRobotHWSim::initSim函数详解
|
人工智能
Colorful Slimes
题目描述 Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.
98 0