or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application-bootstrap.mdauto-configuration.mdconfiguration-properties.mddata-access.mdindex.mdlogging-diagnostics.mdtask-execution.mdweb-framework.md

application-bootstrap.mddocs/

0

# Application Bootstrap & Lifecycle

1

2

Core application startup, lifecycle management, and command line argument processing functionality that forms the foundation of every Spring Boot application.

3

4

## Capabilities

5

6

### SpringApplication

7

8

The main class for bootstrapping and launching a Spring application from a Java main method.

9

10

```java { .api }

11

/**

12

* Class that can be used to bootstrap and launch a Spring application from

13

* a Java main method. By default class will perform the following steps:

14

* - Create an ApplicationContext instance

15

* - Register a CommandLinePropertySource to expose command line arguments

16

* - Refresh the ApplicationContext

17

* - Trigger any CommandLineRunner beans

18

*/

19

public class SpringApplication {

20

/**

21

* Static convenience method to run a SpringApplication from a main method

22

* @param primarySource the primary source to load

23

* @param args the application arguments

24

* @return the running ApplicationContext

25

*/

26

public static ConfigurableApplicationContext run(Class<?> primarySource, String... args);

27

28

/**

29

* Static convenience method to run a SpringApplication from a main method

30

* @param primarySources the primary sources to load

31

* @param args the application arguments

32

* @return the running ApplicationContext

33

*/

34

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String... args);

35

36

/**

37

* Create a new SpringApplication instance

38

* @param primarySources the primary bean sources

39

*/

40

public SpringApplication(Class<?>... primarySources);

41

42

/**

43

* Create a new SpringApplication instance with a ResourceLoader

44

* @param resourceLoader the resource loader to use

45

* @param primarySources the primary bean sources

46

*/

47

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources);

48

49

/**

50

* Run the Spring application

51

* @param args the application arguments

52

* @return the running ApplicationContext

53

*/

54

public ConfigurableApplicationContext run(String... args);

55

56

/**

57

* Set the banner to be displayed when the application runs

58

* @param banner the banner to display

59

*/

60

public void setBanner(Banner banner);

61

62

/**

63

* Set the mode used to display the banner when the application runs

64

* @param bannerMode the mode to use

65

*/

66

public void setBannerMode(Banner.Mode bannerMode);

67

68

/**

69

* Set additional profile values to use

70

* @param profiles the profiles to add

71

*/

72

public void setAdditionalProfiles(String... profiles);

73

74

/**

75

* Set the type of web application

76

* @param webApplicationType the web application type

77

*/

78

public void setWebApplicationType(WebApplicationType webApplicationType);

79

80

/**

81

* Add ApplicationContextInitializer instances

82

* @param initializers the initializers to add

83

*/

84

public void addInitializers(ApplicationContextInitializer<?>... initializers);

85

86

/**

87

* Add ApplicationListener instances

88

* @param listeners the listeners to add

89

*/

90

public void addListeners(ApplicationListener<?>... listeners);

91

92

/**

93

* Return the additional profiles to use

94

* @return the additional profiles (never null)

95

*/

96

public Set<String> getAdditionalProfiles();

97

98

/**

99

* Set whether to allow bean definition overriding

100

* @param allowBeanDefinitionOverriding whether to allow overriding

101

*/

102

public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding);

103

104

/**

105

* Set whether to allow circular references between beans

106

* @param allowCircularReferences whether to allow circular references

107

*/

108

public void setAllowCircularReferences(boolean allowCircularReferences);

109

110

/**

111

* Set whether to enable lazy initialization

112

* @param lazyInitialization whether to enable lazy initialization

113

*/

114

public void setLazyInitialization(boolean lazyInitialization);

115

116

/**

117

* Set whether the JVM should be kept alive after the context is closed

118

* @param keepAlive whether to keep the JVM alive

119

*/

120

public void setKeepAlive(boolean keepAlive);

121

122

/**

123

* Return whether the JVM should be kept alive after the context is closed

124

* @return true if the JVM should be kept alive

125

*/

126

public boolean isKeepAlive();

127

128

/**

129

* Set the ApplicationStartup to use for collecting startup metrics

130

* @param applicationStartup the startup metrics collector

131

*/

132

public void setApplicationStartup(ApplicationStartup applicationStartup);

133

134

/**

135

* Return the ApplicationStartup used for collecting startup metrics

136

* @return the startup metrics collector

137

*/

138

public ApplicationStartup getApplicationStartup();

139

140

/**

141

* Return the shutdown handlers that can be used to register shutdown hooks

142

* @return the shutdown handlers

143

*/

144

public static SpringApplicationShutdownHandlers getShutdownHandlers();

145

}

146

```

147

148

**Usage Examples:**

149

150

```java

151

// Simple application startup

152

@SpringBootApplication

153

public class MyApplication {

154

public static void main(String[] args) {

155

SpringApplication.run(MyApplication.class, args);

156

}

157

}

158

159

// Custom SpringApplication configuration

160

@SpringBootApplication

161

public class CustomApplication {

162

public static void main(String[] args) {

163

SpringApplication app = new SpringApplication(CustomApplication.class);

164

app.setBannerMode(Banner.Mode.OFF);

165

app.setWebApplicationType(WebApplicationType.NONE);

166

app.run(args);

167

}

168

}

169

```

170

171

### Spring Boot Configuration Annotations

172

173

Meta-annotations that configure the basic structure of a Spring Boot application.

174

175

```java { .api }

176

/**

177

* Meta-annotation that combines @Configuration, @EnableAutoConfiguration,

178

* and @ComponentScan with sensible defaults

179

*/

180

@Target(ElementType.TYPE)

181

@Retention(RetentionPolicy.RUNTIME)

182

@interface SpringBootApplication {

183

/**

184

* Exclude specific auto-configuration classes

185

* @return the classes to exclude

186

*/

187

Class<?>[] exclude() default {};

188

189

/**

190

* Exclude specific auto-configuration class names

191

* @return the class names to exclude

192

*/

193

String[] excludeName() default {};

194

195

/**

196

* Base packages to scan for components

197

* @return the base packages

198

*/

199

String[] scanBasePackages() default {};

200

201

/**

202

* Base package classes to scan for components

203

* @return the base package classes

204

*/

205

Class<?>[] scanBasePackageClasses() default {};

206

207

/**

208

* Whether to proxy @Bean methods to enforce bean lifecycle behavior

209

* @return true if bean methods should be proxied

210

*/

211

boolean proxyBeanMethods() default true;

212

213

/**

214

* The BeanNameGenerator class to be used for naming detected components

215

* @return the BeanNameGenerator class

216

*/

217

Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

218

}

219

220

/**

221

* Indicates that a class provides Spring Boot application configuration

222

*/

223

@Target(ElementType.TYPE)

224

@Retention(RetentionPolicy.RUNTIME)

225

@interface SpringBootConfiguration {

226

/**

227

* Whether to proxy @Bean methods to enforce bean lifecycle behavior

228

* @return true if bean methods should be proxied

229

*/

230

boolean proxyBeanMethods() default true;

231

}

232

```

233

234

### Application Arguments

235

236

Interface for accessing command line arguments in a structured way.

237

238

```java { .api }

239

/**

240

* Provides access to the arguments that were used to run a SpringApplication

241

*/

242

public interface ApplicationArguments {

243

/**

244

* Return the raw unprocessed arguments that were passed to the application

245

* @return the source arguments

246

*/

247

String[] getSourceArgs();

248

249

/**

250

* Return the names of all option arguments

251

* @return the option names

252

*/

253

Set<String> getOptionNames();

254

255

/**

256

* Return whether the named option is present

257

* @param name the name to check

258

* @return true if the option is present

259

*/

260

boolean containsOption(String name);

261

262

/**

263

* Return the collection of values associated with the arguments option

264

* @param name the name to check

265

* @return a list of values (never null)

266

*/

267

List<String> getOptionValues(String name);

268

269

/**

270

* Return the collection of non-option arguments parsed

271

* @return the non-option arguments (never null)

272

*/

273

List<String> getNonOptionArgs();

274

}

275

276

/**

277

* Default implementation of ApplicationArguments

278

*/

279

public class DefaultApplicationArguments implements ApplicationArguments {

280

/**

281

* Create a new DefaultApplicationArguments instance

282

* @param args the source arguments

283

*/

284

public DefaultApplicationArguments(String... args);

285

}

286

```

287

288

### Lifecycle Runner Interfaces

289

290

Interfaces for executing code after the Spring application has started.

291

292

```java { .api }

293

/**

294

* Interface used to indicate that a bean should run when it is contained

295

* within a SpringApplication. Multiple CommandLineRunner beans can be

296

* defined within the same application context and can be ordered using

297

* the Ordered interface or @Order annotation.

298

*/

299

@FunctionalInterface

300

public interface CommandLineRunner {

301

/**

302

* Callback used to run the bean

303

* @param args incoming main method arguments

304

* @throws Exception on error

305

*/

306

void run(String... args) throws Exception;

307

}

308

309

/**

310

* Interface used to indicate that a bean should run when it is contained

311

* within a SpringApplication. Multiple ApplicationRunner beans can be

312

* defined within the same application context and can be ordered using

313

* the Ordered interface or @Order annotation.

314

*/

315

@FunctionalInterface

316

public interface ApplicationRunner {

317

/**

318

* Callback used to run the bean

319

* @param args incoming application arguments

320

* @throws Exception on error

321

*/

322

void run(ApplicationArguments args) throws Exception;

323

}

324

```

325

326

**Usage Examples:**

327

328

```java

329

@Component

330

public class DataSetupRunner implements CommandLineRunner {

331

@Override

332

public void run(String... args) throws Exception {

333

System.out.println("Setting up initial data...");

334

// Setup logic here

335

}

336

}

337

338

@Component

339

@Order(1)

340

public class PriorityRunner implements ApplicationRunner {

341

@Override

342

public void run(ApplicationArguments args) throws Exception {

343

if (args.containsOption("setup")) {

344

System.out.println("Running priority setup...");

345

}

346

}

347

}

348

```

349

350

### Application Context Factories

351

352

Factories for creating appropriate ApplicationContext instances based on application type.

353

354

```java { .api }

355

/**

356

* Strategy interface for creating the ApplicationContext used by a SpringApplication

357

*/

358

@FunctionalInterface

359

public interface ApplicationContextFactory {

360

/**

361

* Creates the ApplicationContext for the given application type

362

* @param webApplicationType the web application type

363

* @return a new ApplicationContext instance

364

*/

365

ConfigurableApplicationContext create(WebApplicationType webApplicationType);

366

367

/**

368

* Default factory implementation

369

*/

370

ApplicationContextFactory DEFAULT = new DefaultApplicationContextFactory();

371

}

372

373

/**

374

* Default implementation of ApplicationContextFactory

375

*/

376

public class DefaultApplicationContextFactory implements ApplicationContextFactory {

377

@Override

378

public ConfigurableApplicationContext create(WebApplicationType webApplicationType);

379

}

380

```

381

382

### Bootstrap Context & Registry

383

384

Bootstrap-time registry for sharing objects between ApplicationContextInitializers and other bootstrap code.

385

386

```java { .api }

387

/**

388

* A simple bootstrap context that is available during startup and Environment

389

* post-processing up to the point that the ApplicationContext is prepared

390

*/

391

public interface BootstrapContext {

392

/**

393

* Return an instance from the registry

394

* @param type the instance type

395

* @return the instance

396

* @throws IllegalStateException if the type is not registered

397

*/

398

<T> T get(Class<T> type) throws IllegalStateException;

399

400

/**

401

* Return an instance from the registry if available

402

* @param type the instance type

403

* @return the instance or null

404

*/

405

<T> T getOrElse(Class<T> type, T other);

406

407

/**

408

* Return if a registration exists for the given type

409

* @param type the instance type

410

* @return true if registered

411

*/

412

<T> boolean isRegistered(Class<T> type);

413

}

414

415

/**

416

* A configurable bootstrap context that is available during startup and

417

* Environment post-processing up to the point that the ApplicationContext is prepared

418

*/

419

public interface ConfigurableBootstrapContext extends BootstrapContext {

420

/**

421

* Register a specific type with the registry

422

* @param type the instance type

423

* @param instanceSupplier the instance supplier

424

*/

425

<T> void register(Class<T> type, InstanceSupplier<T> instanceSupplier);

426

427

/**

428

* Register a specific type with the registry if not already present

429

* @param type the instance type

430

* @param instanceSupplier the instance supplier

431

*/

432

<T> void registerIfAbsent(Class<T> type, InstanceSupplier<T> instanceSupplier);

433

434

/**

435

* Add a BootstrapContextClosedEvent listener

436

* @param listener the listener to add

437

*/

438

void addCloseListener(ApplicationListener<BootstrapContextClosedEvent> listener);

439

}

440

441

/**

442

* Supplier that can be used to create an instance when needed

443

*/

444

@FunctionalInterface

445

public interface InstanceSupplier<T> {

446

/**

447

* Factory method to create the instance

448

* @param context the bootstrap context

449

* @return the instance

450

*/

451

T get(BootstrapContext context);

452

}

453

```

454

455

### Web Application Type Detection

456

457

Enumeration for different types of web applications supported by Spring Boot.

458

459

```java { .api }

460

/**

461

* An enumeration of possible types of web application

462

*/

463

public enum WebApplicationType {

464

/**

465

* The application is not a web application and should not start an embedded web server

466

*/

467

NONE,

468

469

/**

470

* The application is a servlet-based web application and should start an embedded servlet web server

471

*/

472

SERVLET,

473

474

/**

475

* The application is a reactive web application and should start an embedded reactive web server

476

*/

477

REACTIVE;

478

479

/**

480

* Determine the web application type based on the classpath

481

* @return the web application type

482

*/

483

static WebApplicationType deduceFromClasspath();

484

}

485

```