or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

annotation-configuration.mdapplication-context.mdcaching.mdconfiguration-properties.mddependency-injection.mdevent-handling.mdformatting.mdindex.mdlifecycle-management.mdscheduling.mdvalidation.md

application-context.mddocs/

0

# Application Context

1

2

Core ApplicationContext interfaces and implementations providing the foundation for dependency injection and application configuration. The ApplicationContext is the central interface in Spring that provides configuration for an application and manages the complete lifecycle of beans.

3

4

## Capabilities

5

6

### ApplicationContext Interface

7

8

The central interface providing configuration for an application. It extends multiple interfaces to provide comprehensive functionality including bean factory operations, resource loading, event publishing, and message resolution.

9

10

```java { .api }

11

/**

12

* Central interface to provide configuration for an application.

13

* This is read-only while the application is running, but may be

14

* reloaded if the implementation supports this.

15

*/

16

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory,

17

HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver {

18

19

/**

20

* Return the unique id of this application context.

21

* @return the unique id of the context, or null if none

22

*/

23

String getId();

24

25

/**

26

* Return a name for the deployed application that this context belongs to.

27

* @return a name for the deployed application, or the empty String by default

28

*/

29

String getApplicationName();

30

31

/**

32

* Return a friendly name for this context.

33

* @return a display name for this context (never null)

34

*/

35

String getDisplayName();

36

37

/**

38

* Return the timestamp when this context was first loaded.

39

* @return the timestamp (ms) when this context was first loaded

40

*/

41

long getStartupDate();

42

43

/**

44

* Return the parent context, or null if there is no parent

45

* and this is the root of the context hierarchy.

46

* @return the parent context, or null if there is no parent

47

*/

48

ApplicationContext getParent();

49

50

/**

51

* Expose AutowireCapableBeanFactory functionality for this context.

52

* @return the AutowireCapableBeanFactory for this context

53

* @throws IllegalStateException if the context does not support the AutowireCapableBeanFactory interface

54

*/

55

AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

56

}

57

```

58

59

### ConfigurableApplicationContext Interface

60

61

SPI interface for configurable application contexts. Provides configuration methods as well as lifecycle methods. This interface should not be used in normal application code.

62

63

```java { .api }

64

/**

65

* SPI interface to be implemented by most if not all application contexts.

66

* Provides facilities to configure an application context in addition

67

* to the application context client methods in the ApplicationContext interface.

68

*/

69

public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {

70

71

/** Delimiter for multiple config locations */

72

String CONFIG_LOCATION_DELIMITERS = ",; \t\n";

73

74

/** Name of the ConversionService bean in the factory */

75

String CONVERSION_SERVICE_BEAN_NAME = "conversionService";

76

77

/** Name of the LoadTimeWeaver bean in the factory */

78

String LOAD_TIME_WEAVER_BEAN_NAME = "loadTimeWeaver";

79

80

/** Name of the Environment bean in the factory */

81

String ENVIRONMENT_BEAN_NAME = "environment";

82

83

/** Name of the System Properties bean in the factory */

84

String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties";

85

86

/** Name of the System Environment bean in the factory */

87

String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";

88

89

/** Name of the ApplicationStartup bean in the factory */

90

String APPLICATION_STARTUP_BEAN_NAME = "applicationStartup";

91

92

/** Name of the shutdown hook thread */

93

String SHUTDOWN_HOOK_THREAD_NAME = "SpringContextShutdownHook";

94

95

/**

96

* Set the unique id of this application context.

97

* @param id the unique id of the context

98

*/

99

void setId(String id);

100

101

/**

102

* Set the parent of this application context.

103

* @param parent the parent context

104

*/

105

void setParent(ApplicationContext parent);

106

107

/**

108

* Set the Environment for this application context.

109

* @param environment the new environment

110

*/

111

void setEnvironment(ConfigurableEnvironment environment);

112

113

/**

114

* Return the Environment for this application context in configurable form.

115

* @return the configurable environment

116

*/

117

ConfigurableEnvironment getEnvironment();

118

119

/**

120

* Set the ApplicationStartup for this application context.

121

* @param applicationStartup the new ApplicationStartup

122

*/

123

void setApplicationStartup(ApplicationStartup applicationStartup);

124

125

/**

126

* Return the ApplicationStartup for this application context.

127

* @return the ApplicationStartup

128

*/

129

ApplicationStartup getApplicationStartup();

130

131

/**

132

* Add a new BeanFactoryPostProcessor that will get applied to the internal

133

* bean factory of this application context on refresh.

134

* @param postProcessor the factory processor to register

135

*/

136

void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor);

137

138

/**

139

* Add a new ApplicationListener that will be notified on context events.

140

* @param listener the ApplicationListener to register

141

*/

142

void addApplicationListener(ApplicationListener<?> listener);

143

144

/**

145

* Remove the given ApplicationListener from this context's set of listeners.

146

* @param listener the ApplicationListener to remove

147

*/

148

void removeApplicationListener(ApplicationListener<?> listener);

149

150

/**

151

* Specify the ClassLoader to load class path resources and bean classes with.

152

* @param classLoader the class loader to use

153

*/

154

void setClassLoader(ClassLoader classLoader);

155

156

/**

157

* Add a ProtocolResolver to this application context.

158

* @param resolver the ProtocolResolver to register

159

*/

160

void addProtocolResolver(ProtocolResolver resolver);

161

162

/**

163

* Load or refresh the persistent representation of the configuration.

164

* @throws BeansException if the bean factory could not be initialized

165

* @throws IllegalStateException if already initialized and multiple refresh attempts are not supported

166

*/

167

void refresh() throws BeansException, IllegalStateException;

168

169

/**

170

* Register a shutdown hook with the JVM runtime.

171

*/

172

void registerShutdownHook();

173

174

/**

175

* Close this application context, releasing all resources and locks.

176

*/

177

void close();

178

179

/**

180

* Determine whether this application context is closed.

181

* @return whether this application context is closed

182

*/

183

boolean isClosed();

184

185

/**

186

* Determine whether this application context is active.

187

* @return whether this application context is active

188

*/

189

boolean isActive();

190

191

/**

192

* Return the internal bean factory of this application context.

193

* @return the underlying bean factory

194

* @throws IllegalStateException if the context is not active

195

*/

196

ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

197

}

198

```

199

200

### AnnotationConfigApplicationContext

201

202

Standalone application context accepting component classes as input. Allows for registering classes one by one using register() as well as for classpath scanning using scan().

203

204

```java { .api }

205

/**

206

* Standalone application context, accepting component classes as input.

207

* Allows for registering classes one by one using register(Class...)

208

* as well as for classpath scanning using scan(String...).

209

*/

210

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {

211

212

/**

213

* Create a new AnnotationConfigApplicationContext that needs to be populated

214

* through register(Class...) calls and then manually refreshed.

215

*/

216

public AnnotationConfigApplicationContext() {}

217

218

/**

219

* Create a new AnnotationConfigApplicationContext with the given DefaultListableBeanFactory.

220

* @param beanFactory the DefaultListableBeanFactory instance to use for this context

221

*/

222

public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory) {}

223

224

/**

225

* Create a new AnnotationConfigApplicationContext, deriving bean definitions

226

* from the given component classes and automatically refreshing the context.

227

* @param componentClasses one or more component classes

228

*/

229

public AnnotationConfigApplicationContext(Class<?>... componentClasses) {}

230

231

/**

232

* Create a new AnnotationConfigApplicationContext, scanning for components

233

* in the given packages and automatically refreshing the context.

234

* @param basePackages the packages to scan for component classes

235

*/

236

public AnnotationConfigApplicationContext(String... basePackages) {}

237

238

/**

239

* Set the BeanNameGenerator to use for detected bean classes.

240

* @param beanNameGenerator the BeanNameGenerator to apply

241

*/

242

public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {}

243

244

/**

245

* Set the ScopeMetadataResolver to use for registered component classes.

246

* @param scopeMetadataResolver the ScopeMetadataResolver to apply

247

*/

248

public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver) {}

249

250

/**

251

* Register one or more component classes to be processed.

252

* @param componentClasses one or more component classes

253

*/

254

public void register(Class<?>... componentClasses) {}

255

256

/**

257

* Perform a scan within the specified base packages.

258

* @param basePackages the packages to scan for component classes

259

*/

260

public void scan(String... basePackages) {}

261

}

262

```

263

264

### GenericApplicationContext

265

266

Generic ApplicationContext implementation that holds a single internal DefaultListableBeanFactory instance and does not assume a specific bean definition format.

267

268

```java { .api }

269

/**

270

* Generic ApplicationContext implementation that holds a single internal

271

* DefaultListableBeanFactory instance and does not assume a specific bean definition format.

272

*/

273

public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {

274

275

/**

276

* Create a new GenericApplicationContext.

277

*/

278

public GenericApplicationContext() {}

279

280

/**

281

* Create a new GenericApplicationContext with the given DefaultListableBeanFactory.

282

* @param beanFactory the DefaultListableBeanFactory instance to use for this context

283

*/

284

public GenericApplicationContext(DefaultListableBeanFactory beanFactory) {}

285

286

/**

287

* Create a new GenericApplicationContext with the given parent.

288

* @param parent the parent application context

289

*/

290

public GenericApplicationContext(ApplicationContext parent) {}

291

292

/**

293

* Create a new GenericApplicationContext with the given DefaultListableBeanFactory and parent.

294

* @param beanFactory the DefaultListableBeanFactory instance to use for this context

295

* @param parent the parent application context

296

*/

297

public GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent) {}

298

299

/**

300

* Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.

301

* @param parent the parent context

302

*/

303

public void setParent(ApplicationContext parent) {}

304

305

/**

306

* Set a custom DefaultListableBeanFactory for this ApplicationContext.

307

* @param beanFactory the DefaultListableBeanFactory instance

308

*/

309

public final void setBeanFactory(DefaultListableBeanFactory beanFactory) {}

310

311

/**

312

* Return the underlying bean factory of this context.

313

* @return the underlying bean factory

314

*/

315

public final DefaultListableBeanFactory getDefaultListableBeanFactory() {}

316

317

/**

318

* Register a bean definition with this registry.

319

* @param beanName the name of the bean instance to register

320

* @param beanDefinition definition of the bean instance to register

321

*/

322

public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)

323

throws BeanDefinitionStoreException {}

324

325

/**

326

* Remove the BeanDefinition for the given name.

327

* @param beanName the name of the bean instance to register

328

*/

329

public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {}

330

331

/**

332

* Return the BeanDefinition for the given bean name.

333

* @param beanName name of the bean to find a definition for

334

* @return the BeanDefinition for the given name

335

*/

336

public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {}

337

338

/**

339

* Check if this registry contains a bean definition with the given name.

340

* @param beanName the name of the bean to look for

341

* @return if this registry contains a bean definition with the given name

342

*/

343

public boolean containsBeanDefinition(String beanName) {}

344

}

345

```

346

347

### ApplicationContext Implementations

348

349

Additional ApplicationContext implementations for different configuration sources and use cases.

350

351

```java { .api }

352

/**

353

* Standalone XML application context, taking the context definition files

354

* from the class path, interpreting plain paths as class path resource names.

355

*/

356

public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {

357

/**

358

* Create a new ClassPathXmlApplicationContext, loading the definitions

359

* from the given XML file and automatically refreshing the context.

360

* @param configLocation resource location

361

*/

362

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {}

363

364

/**

365

* Create a new ClassPathXmlApplicationContext, loading the definitions

366

* from the given XML files and automatically refreshing the context.

367

* @param configLocations array of resource locations

368

*/

369

public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}

370

371

/**

372

* Create a new ClassPathXmlApplicationContext with the given parent,

373

* loading the definitions from the given XML files.

374

* @param configLocations array of resource locations

375

* @param parent the parent context

376

*/

377

public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {}

378

}

379

380

/**

381

* Standalone XML application context, taking the context definition files

382

* from the file system or from URLs, interpreting plain paths as relative

383

* file system locations.

384

*/

385

public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {

386

/**

387

* Create a new FileSystemXmlApplicationContext, loading the definitions

388

* from the given XML file and automatically refreshing the context.

389

* @param configLocation file path

390

*/

391

public FileSystemXmlApplicationContext(String configLocation) throws BeansException {}

392

393

/**

394

* Create a new FileSystemXmlApplicationContext, loading the definitions

395

* from the given XML files and automatically refreshing the context.

396

* @param configLocations array of file paths

397

*/

398

public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {}

399

400

/**

401

* Create a new FileSystemXmlApplicationContext with the given parent,

402

* loading the definitions from the given XML files.

403

* @param configLocations array of file paths

404

* @param parent the parent context

405

*/

406

public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {}

407

}

408

```

409

410

### ApplicationContext Events

411

412

Built-in events published by ApplicationContext implementations during lifecycle operations.

413

414

```java { .api }

415

/**

416

* Base class for events raised for an ApplicationContext.

417

*/

418

public abstract class ApplicationContextEvent extends ApplicationEvent {

419

/**

420

* Create a new ContextStartedEvent.

421

* @param source the ApplicationContext that the event is raised for

422

*/

423

public ApplicationContextEvent(ApplicationContext source) {}

424

425

/**

426

* Get the ApplicationContext that the event was raised for.

427

* @return the ApplicationContext

428

*/

429

public final ApplicationContext getApplicationContext() {}

430

}

431

432

/**

433

* Event raised when an ApplicationContext gets initialized or refreshed.

434

*/

435

public class ContextRefreshedEvent extends ApplicationContextEvent {

436

/**

437

* Create a new ContextRefreshedEvent.

438

* @param source the ApplicationContext that has been initialized or refreshed

439

*/

440

public ContextRefreshedEvent(ApplicationContext source) {}

441

}

442

443

/**

444

* Event raised when an ApplicationContext gets started.

445

*/

446

public class ContextStartedEvent extends ApplicationContextEvent {

447

/**

448

* Creates a new ContextStartedEvent.

449

* @param source the ApplicationContext that has been started

450

*/

451

public ContextStartedEvent(ApplicationContext source) {}

452

}

453

454

/**

455

* Event raised when an ApplicationContext gets stopped.

456

*/

457

public class ContextStoppedEvent extends ApplicationContextEvent {

458

/**

459

* Creates a new ContextStoppedEvent.

460

* @param source the ApplicationContext that has been stopped

461

*/

462

public ContextStoppedEvent(ApplicationContext source) {}

463

}

464

465

/**

466

* Event raised when an ApplicationContext gets closed.

467

*/

468

public class ContextClosedEvent extends ApplicationContextEvent {

469

/**

470

* Creates a new ContextClosedEvent.

471

* @param source the ApplicationContext that has been closed

472

*/

473

public ContextClosedEvent(ApplicationContext source) {}

474

}

475

```

476

477

### Aware Interfaces

478

479

Marker interfaces that beans can implement to be notified of ApplicationContext-related objects.

480

481

```java { .api }

482

/**

483

* Interface to be implemented by any object that wishes to be notified

484

* of the ApplicationContext that it runs in.

485

*/

486

public interface ApplicationContextAware extends Aware {

487

/**

488

* Set the ApplicationContext that this object runs in.

489

* @param applicationContext the ApplicationContext object to be used by this object

490

* @throws ApplicationContextException in case of context initialization errors

491

* @throws BeansException if thrown by application context methods

492

*/

493

void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

494

}

495

496

/**

497

* Interface to be implemented by any object that wishes to be notified of

498

* the ApplicationEventPublisher (typically the ApplicationContext) that it runs in.

499

*/

500

public interface ApplicationEventPublisherAware extends Aware {

501

/**

502

* Set the ApplicationEventPublisher that this object runs in.

503

* @param applicationEventPublisher the ApplicationEventPublisher object to be used by this object

504

*/

505

void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);

506

}

507

508

/**

509

* Interface to be implemented by any object that wishes to be notified of

510

* the Environment that it runs in.

511

*/

512

public interface EnvironmentAware extends Aware {

513

/**

514

* Set the Environment that this object runs in.

515

* @param environment the Environment object to be used by this object

516

*/

517

void setEnvironment(Environment environment);

518

}

519

520

/**

521

* Interface to be implemented by any object that wishes to be notified of

522

* the MessageSource (typically the ApplicationContext) that it runs in.

523

*/

524

public interface MessageSourceAware extends Aware {

525

/**

526

* Set the MessageSource that this object runs in.

527

* @param messageSource the MessageSource object to be used by this object

528

*/

529

void setMessageSource(MessageSource messageSource);

530

}

531

532

/**

533

* Interface to be implemented by any object that wishes to be notified of

534

* the ResourceLoader (typically the ApplicationContext) that it runs in.

535

*/

536

public interface ResourceLoaderAware extends Aware {

537

/**

538

* Set the ResourceLoader that this object runs in.

539

* @param resourceLoader the ResourceLoader object to be used by this object

540

*/

541

void setResourceLoader(ResourceLoader resourceLoader);

542

}

543

```

544

545

### Usage Examples

546

547

**Basic ApplicationContext Usage:**

548

549

```java

550

import org.springframework.context.ApplicationContext;

551

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

552

553

// Create context with configuration class

554

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

555

556

// Retrieve beans

557

MyService service = context.getBean(MyService.class);

558

MyRepository repository = context.getBean("myRepository", MyRepository.class);

559

560

// Check bean existence

561

boolean hasBean = context.containsBean("myService");

562

563

// Get bean names

564

String[] beanNames = context.getBeanDefinitionNames();

565

566

// Close context

567

((ConfigurableApplicationContext) context).close();

568

```

569

570

**Hierarchical Context Setup:**

571

572

```java

573

// Create parent context

574

ApplicationContext parentContext = new AnnotationConfigApplicationContext(ParentConfig.class);

575

576

// Create child context with parent

577

AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext();

578

childContext.setParent(parentContext);

579

childContext.register(ChildConfig.class);

580

childContext.refresh();

581

582

// Child can access parent beans

583

ParentBean parentBean = childContext.getBean(ParentBean.class);

584

```