• java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

  • java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

spring-ioc 部分interface接口

  • 内容
  • apis
    • SmartInitializingSingleton
      • 类注释
      • 方法注释
    • 调用过程
      • 场景及实例

内容

介绍部分ioc中的接口功能及其使用场景

apis

SmartInitializingSingleton

所有单例bean初始化完成后调用

类注释

  • Callback interface triggered at the end of the singleton pre-instantiation phase
  • during {@link BeanFactory} bootstrap. This interface can be implemented by
  • singleton beans in order to perform some initialization after the regular
  • singleton instantiation algorithm, avoiding side effects with accidental early
  • initialization (e.g. from {@link ListableBeanFactory#getBeansOfType} calls).
  • In that sense, it is an alternative to {@link InitializingBean} which gets
  • triggered right at the end of a bean’s local construction phase.
  • This callback variant is somewhat similar to

  • {@link org.springframework.context.event.ContextRefreshedEvent} but doesn’t
  • require an implementation of {@link org.springframework.context.ApplicationListener},
  • with no need to filter context references across a context hierarchy etc.
  • It also implies a more minimal dependency on just the {@code beans} package
  • and is being honored by standalone {@link ListableBeanFactory} implementations,
  • not just in an {@link org.springframework.context.ApplicationContext} environment.
  • NOTE: If you intend to start/manage asynchronous tasks, preferably

  • implement {@link org.springframework.context.Lifecycle} instead which offers
  • a richer model for runtime management and allows for phased startup/shutdown.

方法注释

  • Invoked right at the end of the singleton pre-instantiation phase,
  • with a guarantee that all regular singleton beans have been created
  • already. {@link ListableBeanFactory#getBeansOfType} calls within
  • this method won’t trigger accidental side effects during bootstrap.
  • NOTE: This callback won't be triggered for singleton beans

  • lazily initialized on demand after {@link BeanFactory} bootstrap,
  • and not for any other bean scope either. Carefully use it for beans
  • with the intended bootstrap semantics only.

调用过程

ApplicationContext 在 refresh 过程中会调用 finishBeanFactoryInitialization(beanFactory) 来提前初始化单例bean,具体方法是调用 preInstantiateSingletons,
该方法定义在接口 ConfigurableListableBeanFactory 中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public void preInstantiateSingletons() throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("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();
}
}
}
}

场景及实例

spring boot rocketmq中,ListenerContainerConfiguration 实现ApplicationContextAware, SmartInitializingSingleton两个接口,将MQListenerContainer注册到了spring中

spring-概览
spring-mapper代理对象的创建
  1. 1. 内容
  2. 2. apis
    1. 2.1. SmartInitializingSingleton
      1. 2.1.1. 类注释
      2. 2.1.2. 方法注释
    2. 2.2. 调用过程
      1. 2.2.1. 场景及实例
© 2023 haoxp
Hexo theme