• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.25
today-context/src/main/java/infra/context/support/ApplicationListenerDetector.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.support;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Map;
23
import java.util.concurrent.ConcurrentHashMap;
24

25
import infra.beans.factory.InitializationBeanPostProcessor;
26
import infra.beans.factory.config.DestructionAwareBeanPostProcessor;
27
import infra.beans.factory.support.MergedBeanDefinitionPostProcessor;
28
import infra.beans.factory.support.RootBeanDefinition;
29
import infra.context.ApplicationListener;
30
import infra.context.event.ApplicationEventMulticaster;
31
import infra.logging.Logger;
32
import infra.logging.LoggerFactory;
33
import infra.util.ObjectUtils;
34

35
/**
36
 * {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
37
 * interface. This catches beans that can't reliably be detected by {@code getBeanNamesForType}
38
 * and related operations which only work against top-level beans.
39
 *
40
 * <p>With standard Java serialization, this post-processor won't get serialized as part of
41
 * {@code DisposableBeanAdapter} to begin with. However, with alternative serialization
42
 * mechanisms, {@code DisposableBeanAdapter.writeReplace} might not get used at all, so we
43
 * defensively mark this post-processor's field state as {@code transient}.
44
 *
45
 * @author Juergen Hoeller
46
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
47
 * @since 4.0
48
 */
49
final class ApplicationListenerDetector
50
        implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, InitializationBeanPostProcessor {
51

52
  private static final Logger logger = LoggerFactory.getLogger(ApplicationListenerDetector.class);
4✔
53

54
  private final transient AbstractApplicationContext applicationContext;
55

56
  private final transient Map<String, Boolean> singletonNames = new ConcurrentHashMap<>(256);
6✔
57

58
  ApplicationListenerDetector(AbstractApplicationContext applicationContext) {
2✔
59
    this.applicationContext = applicationContext;
3✔
60
  }
1✔
61

62
  @Override
63
  public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
64
    if (ApplicationListener.class.isAssignableFrom(beanType)) {
4✔
65
      this.singletonNames.put(beanName, beanDefinition.isSingleton());
8✔
66
    }
67
  }
1✔
68

69
  @Override
70
  public Object postProcessBeforeInitialization(Object bean, String beanName) {
71
    return bean;
2✔
72
  }
73

74
  @Override
75
  public Object postProcessAfterInitialization(Object bean, String beanName) {
76
    // FIXME 优化 singletonNames
77
    if (bean instanceof ApplicationListener) {
3✔
78
      // potentially not detected as a listener by getBeanNamesForType retrieval
79
      Boolean flag = singletonNames.get(beanName);
6✔
80
      if (Boolean.TRUE.equals(flag)) {
4✔
81
        // singleton bean (top-level or inner): register on the fly
82
        applicationContext.addApplicationListener((ApplicationListener<?>) bean);
6✔
83
      }
84
      else if (Boolean.FALSE.equals(flag)) {
4✔
85
        if (logger.isWarnEnabled() && !applicationContext.containsBean(beanName)) {
3!
86
          // inner bean with other scope - can't reliably process events
87
          logger.warn("Inner bean '{}' implements ApplicationListener interface " +
×
88
                  "but is not reachable for event multicasting by its containing ApplicationContext " +
89
                  "because it does not have singleton scope. Only top-level listener beans are allowed " +
90
                  "to be of non-singleton scope.", beanName);
91
        }
92
        singletonNames.remove(beanName);
5✔
93
      }
94
    }
95
    return bean;
2✔
96
  }
97

98
  @Override
99
  public void postProcessBeforeDestruction(Object bean, String beanName) {
100
    if (bean instanceof ApplicationListener) {
3!
101
      try {
102
        ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
4✔
103
        multicaster.removeApplicationListener((ApplicationListener<?>) bean);
4✔
104
        multicaster.removeApplicationListenerBean(beanName);
3✔
105
      }
106
      catch (IllegalStateException ex) {
×
107
        // ApplicationEventMulticaster not initialized yet - no need to remove a listener
108
      }
1✔
109
    }
110
  }
1✔
111

112
  @Override
113
  public boolean requiresDestruction(Object bean) {
114
    return (bean instanceof ApplicationListener);
3✔
115
  }
116

117
  @Override
118
  public boolean equals(@Nullable Object other) {
119
    return (this == other || (other instanceof ApplicationListenerDetector &&
16!
120
            this.applicationContext == ((ApplicationListenerDetector) other).applicationContext));
121
  }
122

123
  @Override
124
  public int hashCode() {
125
    return ObjectUtils.nullSafeHashCode(this.applicationContext);
×
126
  }
127

128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc