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

TAKETODAY / today-infrastructure / 20632861616

01 Jan 2026 04:53AM UTC coverage: 84.18% (-0.3%) from 84.439%
20632861616

push

github

TAKETODAY
:sparkles: ApplicationType 支持通过 SPI 获取

55643 of 70608 branches covered (78.81%)

Branch coverage included in aggregate %.

130472 of 150485 relevant lines covered (86.7%)

3.73 hits per line

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

65.85
today-context/src/main/java/infra/context/annotation/AnnotationConfigApplicationContext.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.annotation;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.function.Supplier;
23

24
import infra.beans.factory.config.BeanDefinition;
25
import infra.beans.factory.config.BeanDefinitionCustomizer;
26
import infra.beans.factory.support.BeanDefinitionRegistry;
27
import infra.beans.factory.support.BeanNameGenerator;
28
import infra.beans.factory.support.StandardBeanFactory;
29
import infra.context.AnnotationConfigRegistry;
30
import infra.context.ApplicationContext;
31
import infra.context.BootstrapContext;
32
import infra.context.ConfigurableApplicationContext;
33
import infra.context.support.GenericApplicationContext;
34
import infra.context.support.GenericXmlApplicationContext;
35
import infra.core.env.ConfigurableEnvironment;
36
import infra.lang.Assert;
37
import infra.stereotype.Component;
38

39
/**
40
 * Standalone application context, accepting <em>component classes</em> as input &mdash;
41
 * in particular {@link Configuration @Configuration}-annotated classes, but also plain
42
 * {@link Component @Component} types and JSR-330 compliant
43
 * classes using {@code jakarta.inject} annotations.
44
 *
45
 * <p>Allows for registering classes one by one using {@link #register(Class...)}
46
 * as well as for classpath scanning using {@link #scan(String...)}.
47
 *
48
 * <p>In case of multiple {@code @Configuration} classes, {@link Component @Component} methods
49
 * defined in later classes will override those defined in earlier classes. This can
50
 * be leveraged to deliberately override certain bean definitions via an extra
51
 * {@code @Configuration} class.
52
 *
53
 * <p>See {@link Configuration @Configuration}'s javadoc for usage examples.
54
 *
55
 * @author Juergen Hoeller
56
 * @author Chris Beams
57
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
58
 * @see #register
59
 * @see #scan
60
 * @see AnnotatedBeanDefinitionReader
61
 * @see ClassPathBeanDefinitionScanner
62
 * @see GenericXmlApplicationContext
63
 * @since 2018-09-06 13:47
64
 */
65
public class AnnotationConfigApplicationContext extends GenericApplicationContext
66
        implements ConfigurableApplicationContext, BeanDefinitionRegistry, AnnotationConfigRegistry {
67

68
  private final AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(this);
18✔
69

70
  private final ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this);
18✔
71

72
  /**
73
   * Create a new AnnotationConfigApplicationContext that needs to be populated
74
   * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
75
   */
76
  public AnnotationConfigApplicationContext() { }
3✔
77

78
  /**
79
   * Create a new AnnotationConfigApplicationContext with the given StandardBeanFactory.
80
   *
81
   * @param beanFactory the StandardBeanFactory instance to use for this context
82
   */
83
  public AnnotationConfigApplicationContext(StandardBeanFactory beanFactory) {
84
    super(beanFactory);
3✔
85
  }
1✔
86

87
  /**
88
   * Create a new AnnotationConfigApplicationContext with the given parent.
89
   *
90
   * @param parent the parent application context
91
   * @see #registerBeanDefinition(String, BeanDefinition)
92
   * @see #refresh()
93
   */
94
  public AnnotationConfigApplicationContext(@Nullable ApplicationContext parent) {
×
95
    setParent(parent);
×
96
  }
×
97

98
  /**
99
   * Create a new AnnotationConfigApplicationContext with the given StandardBeanFactory.
100
   *
101
   * @param beanFactory the StandardBeanFactory instance to use for this context
102
   * @param parent the parent application context
103
   * @see #registerBeanDefinition(String, BeanDefinition)
104
   * @see #refresh()
105
   */
106
  public AnnotationConfigApplicationContext(StandardBeanFactory beanFactory, ApplicationContext parent) {
107
    this(beanFactory);
×
108
    setParent(parent);
×
109
  }
×
110

111
  /**
112
   * Create a new AnnotationConfigApplicationContext, deriving bean definitions
113
   * from the given component classes and automatically refreshing the context.
114
   *
115
   * @param components one or more component classes &mdash; for example,
116
   * {@link Configuration @Configuration} classes
117
   * @see #refresh()
118
   * @see #register(Class[])
119
   */
120
  public AnnotationConfigApplicationContext(Class<?>... components) {
2✔
121
    register(components);
3✔
122
    refresh();
2✔
123
  }
1✔
124

125
  /**
126
   * Create a new AnnotationConfigApplicationContext, scanning for components
127
   * in the given packages, registering bean definitions for those components,
128
   * and automatically refreshing the context.
129
   *
130
   * @param basePackages the packages to scan for component classes
131
   * @see #refresh()
132
   */
133
  public AnnotationConfigApplicationContext(String... basePackages) {
×
134
    scan(basePackages);
×
135
    refresh();
×
136
  }
×
137

138
  //---------------------------------------------------------------------
139
  // Implementation of AnnotationConfigRegistry
140
  //---------------------------------------------------------------------
141

142
  /**
143
   * Register one or more component classes to be processed.
144
   * <p>Note that {@link #refresh()} must be called in order for the context
145
   * to fully process the new classes.
146
   *
147
   * @param components one or more component classes &mdash; for example,
148
   * {@link Configuration @Configuration} classes
149
   * @see #scan(String...)
150
   * @see #refresh()
151
   */
152
  @Override
153
  public void register(Class<?>... components) {
154
    Assert.notEmpty(components, "At least one component class must be specified");
3✔
155
    reader.register(components);
4✔
156
  }
1✔
157

158
  /**
159
   * Perform a scan within the specified base packages.
160
   * <p>Note that {@link #refresh()} must be called in order for the context
161
   * to fully process the new classes.
162
   *
163
   * @param basePackages the packages to scan for component classes
164
   * @see #register(Class...)
165
   * @see #refresh()
166
   */
167
  @Override
168
  public void scan(String... basePackages) {
169
    Assert.notEmpty(basePackages, "At least one base package must be specified");
3✔
170
    scanner.scan(basePackages);
5✔
171
  }
1✔
172

173
  /**
174
   * Provide a custom {@link BeanNameGenerator} for use with {@link AnnotatedBeanDefinitionReader}
175
   * and/or {@link BootstrapContext}, if any.
176
   * <p>Default is {@link AnnotationBeanNameGenerator}.
177
   * <p>Any call to this method must occur prior to calls to {@link #register(Class...)}
178
   * and/or {@link #scan(String...)}.
179
   *
180
   * @see AnnotationBeanNameGenerator
181
   * @see FullyQualifiedAnnotationBeanNameGenerator
182
   * @see BootstrapContext#setBeanNameGenerator(BeanNameGenerator)
183
   * @see AnnotatedBeanDefinitionReader#setBeanNameGenerator(BeanNameGenerator)
184
   */
185
  public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
186
    Assert.notNull(beanNameGenerator, "BeanNameGenerator is required");
3✔
187

188
    reader.setBeanNameGenerator(beanNameGenerator);
4✔
189
    scanner.setBeanNameGenerator(beanNameGenerator);
4✔
190
    getBootstrapContext().setBeanNameGenerator(beanNameGenerator);
4✔
191

192
    getBeanFactory().registerSingleton(
5✔
193
            AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, beanNameGenerator);
194
  }
1✔
195

196
  /**
197
   * Set the {@link ScopeMetadataResolver} to use for registered component classes.
198
   * <p>The default is an {@link AnnotationScopeMetadataResolver}.
199
   * <p>Any call to this method must occur prior to calls to {@link #register(Class...)}
200
   * and/or {@link #scan(String...)}.
201
   */
202
  public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver) {
203
    reader.setScopeMetadataResolver(scopeMetadataResolver);
×
204
    scanner.setScopeMetadataResolver(scopeMetadataResolver);
×
205
    getBootstrapContext().setScopeMetadataResolver(scopeMetadataResolver);
×
206
  }
×
207

208
  /**
209
   * Propagate the given custom {@code Environment} to the underlying
210
   * {@link AnnotatedBeanDefinitionReader} and {@link ClassPathBeanDefinitionScanner}.
211
   */
212
  @Override
213
  public void setEnvironment(ConfigurableEnvironment environment) {
214
    super.setEnvironment(environment);
3✔
215
    reader.setEnvironment(environment);
4✔
216
    scanner.setEnvironment(environment);
4✔
217
  }
1✔
218

219
  //---------------------------------------------------------------------
220
  // Adapt superclass registerBean calls to AnnotatedBeanDefinitionReader
221
  //---------------------------------------------------------------------
222

223
  @Override
224
  public <T> void registerBean(@Nullable String beanName, Class<T> beanClass,
225
          @Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
226
    reader.registerBean(beanClass, beanName, supplier, customizers);
7✔
227
  }
1✔
228

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

© 2026 Coveralls, Inc