or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application.mdconfiguration.mdcontext.mdconversion.mdindex.mdjson.mdlogging.mdweb.md

application.mddocs/

0

# Application Management

1

2

Spring Boot application management covers the core application lifecycle, startup runners, and argument handling. This includes the main `SpringApplication` class for bootstrapping, runner interfaces for startup tasks, and application argument processing.

3

4

## Capabilities

5

6

### SpringApplication Class

7

8

The main entry point for Spring Boot applications, providing both static convenience methods and instance-based configuration.

9

10

```java { .api }

11

/**

12

* The main Spring Boot application class that can be used to bootstrap and launch

13

* a Spring application from a Java main method

14

*/

15

public class SpringApplication {

16

17

/**

18

* Create a new SpringApplication instance

19

* @param primarySources the primary bean sources

20

*/

21

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

22

23

/**

24

* Create a new SpringApplication instance with a custom ResourceLoader

25

* @param resourceLoader the resource loader to use

26

* @param primarySources the primary bean sources

27

*/

28

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

29

30

/**

31

* Static convenience method to run a Spring Boot application

32

* @param primarySource the primary source class

33

* @param args command line arguments

34

* @return the running ApplicationContext

35

*/

36

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

37

38

/**

39

* Run the Spring application with multiple source classes

40

* @param primarySources array of primary source classes

41

* @param args command line arguments

42

* @return the running ApplicationContext

43

*/

44

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

45

46

/**

47

* Run the Spring application (instance method)

48

* @param args command line arguments

49

* @return the running ApplicationContext

50

*/

51

public ConfigurableApplicationContext run(String... args);

52

53

/**

54

* Set the web application type

55

* @param webApplicationType the web application type to set

56

*/

57

public void setWebApplicationType(WebApplicationType webApplicationType);

58

59

/**

60

* Configure the banner mode

61

* @param bannerMode the banner mode to set

62

*/

63

public void setBannerMode(Banner.Mode bannerMode);

64

65

/**

66

* Set whether the application should run in headless mode

67

* @param headless true for headless mode

68

*/

69

public void setHeadless(boolean headless);

70

71

/**

72

* Set additional active profiles

73

* @param profiles profile names to activate

74

*/

75

public void setAdditionalProfiles(String... profiles);

76

77

/**

78

* Set default properties

79

* @param defaultProperties map of default properties

80

*/

81

public void setDefaultProperties(Map<String, Object> defaultProperties);

82

83

/**

84

* Add ApplicationContextInitializers to be applied to the context

85

* @param initializers the initializers to add

86

*/

87

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

88

89

/**

90

* Add ApplicationListeners to be applied to the SpringApplication

91

* @param listeners the listeners to add

92

*/

93

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

94

95

/**

96

* Set a custom ConfigurableEnvironment

97

* @param environment the environment to set

98

*/

99

public void setEnvironment(ConfigurableEnvironment environment);

100

101

/**

102

* Set the ApplicationContextFactory to use

103

* @param applicationContextFactory the context factory

104

*/

105

public void setApplicationContextFactory(ApplicationContextFactory applicationContextFactory);

106

107

/**

108

* Exit the application with the given exit code generators

109

* @param context the application context

110

* @param exitCodeGenerators exit code generators

111

* @return the exit code

112

*/

113

public static int exit(ApplicationContext context, ExitCodeGenerator... exitCodeGenerators);

114

}

115

```

116

117

**Usage Examples:**

118

119

```java

120

// Basic application startup

121

@SpringBootApplication

122

public class MyApplication {

123

public static void main(String[] args) {

124

SpringApplication.run(MyApplication.class, args);

125

}

126

}

127

128

// Customized application startup

129

@SpringBootApplication

130

public class MyApplication {

131

public static void main(String[] args) {

132

SpringApplication app = new SpringApplication(MyApplication.class);

133

app.setWebApplicationType(WebApplicationType.SERVLET);

134

app.setBannerMode(Banner.Mode.OFF);

135

app.setAdditionalProfiles("production");

136

137

Map<String, Object> defaultProps = new HashMap<>();

138

defaultProps.put("server.port", "8080");

139

app.setDefaultProperties(defaultProps);

140

141

app.run(args);

142

}

143

}

144

145

// Multiple source classes

146

public class MyApplication {

147

public static void main(String[] args) {

148

SpringApplication.run(new Class<?>[] {

149

MyApplication.class,

150

DatabaseConfig.class,

151

SecurityConfig.class

152

}, args);

153

}

154

}

155

```

156

157

### Application Runners

158

159

Interfaces for beans that need to run code after the Spring Boot application has started.

160

161

```java { .api }

162

/**

163

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

164

* a SpringApplication. Multiple ApplicationRunner beans can be defined and can be

165

* ordered using the @Order annotation or by implementing the Ordered interface.

166

*/

167

@FunctionalInterface

168

public interface ApplicationRunner {

169

/**

170

* Callback used to run the bean

171

* @param args incoming application arguments

172

* @throws Exception on error

173

*/

174

void run(ApplicationArguments args) throws Exception;

175

}

176

177

/**

178

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

179

* a SpringApplication. Multiple CommandLineRunner beans can be defined and can be

180

* ordered using the @Order annotation or by implementing the Ordered interface.

181

*/

182

@FunctionalInterface

183

public interface CommandLineRunner {

184

/**

185

* Callback used to run the bean

186

* @param args incoming main method arguments

187

* @throws Exception on error

188

*/

189

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

190

}

191

```

192

193

**Usage Examples:**

194

195

```java

196

import org.springframework.boot.ApplicationRunner;

197

import org.springframework.boot.ApplicationArguments;

198

import org.springframework.core.annotation.Order;

199

import org.springframework.stereotype.Component;

200

201

@Component

202

@Order(1)

203

public class DatabaseInitializer implements ApplicationRunner {

204

205

@Autowired

206

private UserRepository userRepository;

207

208

@Override

209

public void run(ApplicationArguments args) throws Exception {

210

if (args.containsOption("init-db")) {

211

// Initialize database with sample data

212

userRepository.save(new User("admin", "admin@example.com"));

213

System.out.println("Database initialized with sample data");

214

}

215

}

216

}

217

218

@Component

219

@Order(2)

220

public class CacheWarmer implements CommandLineRunner {

221

222

@Autowired

223

private CacheManager cacheManager;

224

225

@Override

226

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

227

// Warm up caches after application startup

228

cacheManager.getCache("users").put("admin", loadAdminUser());

229

System.out.println("Caches warmed up successfully");

230

}

231

}

232

```

233

234

### Application Arguments

235

236

Interface providing access to parsed application arguments.

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 arguments

246

*/

247

String[] getSourceArgs();

248

249

/**

250

* Return the names of all option arguments

251

* @return the option names or an empty set if no option arguments were present

252

*/

253

Set<String> getOptionNames();

254

255

/**

256

* Return whether the set of option arguments parsed from the arguments contains

257

* an option with the given name

258

* @param name the name to check

259

* @return true if the arguments contain an option with the given name

260

*/

261

boolean containsOption(String name);

262

263

/**

264

* Return the collection of values associated with the arguments option having

265

* the given name

266

* @param name the name of the option

267

* @return a list of option values for the given name

268

*/

269

List<String> getOptionValues(String name);

270

271

/**

272

* Return the collection of non-option arguments parsed from the command line

273

* @return the non-option arguments or an empty list

274

*/

275

List<String> getNonOptionArgs();

276

}

277

```

278

279

**Usage Examples:**

280

281

```java

282

@Component

283

public class ArgumentProcessor implements ApplicationRunner {

284

285

@Override

286

public void run(ApplicationArguments args) throws Exception {

287

System.out.println("Raw arguments: " + Arrays.toString(args.getSourceArgs()));

288

289

// Check for specific options

290

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

291

System.out.println("Debug mode enabled");

292

}

293

294

// Get option values

295

List<String> profiles = args.getOptionValues("spring.profiles.active");

296

if (profiles != null) {

297

System.out.println("Active profiles: " + profiles);

298

}

299

300

// Process non-option arguments

301

List<String> nonOptionArgs = args.getNonOptionArgs();

302

for (String arg : nonOptionArgs) {

303

System.out.println("Processing file: " + arg);

304

}

305

}

306

}

307

```

308

309

### Application Context Factory

310

311

Strategy interface for creating different types of application contexts.

312

313

```java { .api }

314

/**

315

* Strategy interface for creating ConfigurableApplicationContext instances

316

*/

317

@FunctionalInterface

318

public interface ApplicationContextFactory {

319

320

/**

321

* Creates the ApplicationContext for a SpringApplication with the given application type

322

* @param webApplicationType the web application type

323

* @return a newly created ApplicationContext

324

*/

325

ConfigurableApplicationContext create(WebApplicationType webApplicationType);

326

327

/**

328

* Creates an Environment for a SpringApplication with the given application type

329

* @param webApplicationType the web application type

330

* @return a newly created ConfigurableEnvironment

331

*/

332

default ConfigurableEnvironment createEnvironment(WebApplicationType webApplicationType) {

333

return getEnvironmentType(webApplicationType).getDeclaredConstructor().newInstance();

334

}

335

336

/**

337

* Returns the Environment type for a SpringApplication with the given application type

338

* @param webApplicationType the web application type

339

* @return the Environment type

340

*/

341

default Class<? extends ConfigurableEnvironment> getEnvironmentType(WebApplicationType webApplicationType) {

342

return StandardEnvironment.class;

343

}

344

}

345

```

346

347

### Bootstrap Registry

348

349

Interface for initializing the bootstrap registry during application startup.

350

351

```java { .api }

352

/**

353

* Callback interface that can be used to initialize a BootstrapRegistry before it

354

* is used to create the ApplicationContext

355

*/

356

@FunctionalInterface

357

public interface BootstrapRegistryInitializer {

358

359

/**

360

* Initialize the given BootstrapRegistry with any required registrations

361

* @param registry the registry to initialize

362

*/

363

void initialize(BootstrapRegistry registry);

364

}

365

```

366

367

### Exit Code Handling

368

369

Interfaces for handling application exit codes and exception mapping.

370

371

```java { .api }

372

/**

373

* Interface used to generate an exit code when a SpringApplication encounters an error

374

*/

375

@FunctionalInterface

376

public interface ExitCodeGenerator {

377

378

/**

379

* Returns the exit code that should be returned from the application

380

* @return the exit code (zero indicates normal termination)

381

*/

382

int getExitCode();

383

}

384

385

/**

386

* Maps Exception types to exit codes so that they can be returned from the application

387

*/

388

@FunctionalInterface

389

public interface ExitCodeExceptionMapper {

390

391

/**

392

* Returns the exit code that should be used for the given exception

393

* @param exception the exception causing the application to exit

394

* @return the exit code

395

*/

396

int getExitCode(Throwable exception);

397

}

398

```

399

400

**Usage Examples:**

401

402

```java

403

@Component

404

public class CustomExitCodeGenerator implements ExitCodeGenerator {

405

406

@Autowired

407

private ApplicationHealthIndicator healthIndicator;

408

409

@Override

410

public int getExitCode() {

411

return healthIndicator.isHealthy() ? 0 : 1;

412

}

413

}

414

415

@Component

416

public class DatabaseExceptionMapper implements ExitCodeExceptionMapper {

417

418

@Override

419

public int getExitCode(Throwable exception) {

420

if (exception instanceof DataAccessException) {

421

return 2; // Database error exit code

422

}

423

if (exception instanceof SecurityException) {

424

return 3; // Security error exit code

425

}

426

return 1; // Generic error

427

}

428

}

429

```

430

431

### Spring Application Builder

432

433

Fluent API builder for creating and configuring SpringApplication instances.

434

435

```java { .api }

436

/**

437

* Builder for SpringApplication and ApplicationContext instances with convenient fluent API

438

* and context hierarchy support

439

*/

440

public class SpringApplicationBuilder {

441

442

/**

443

* Create an application builder instance for the specified sources

444

* @param sources the bean sources

445

*/

446

public SpringApplicationBuilder(Class<?>... sources);

447

448

/**

449

* Add sources (configuration classes or components) to this application

450

* @param sources the sources to add

451

* @return the current builder (for chaining)

452

*/

453

public SpringApplicationBuilder sources(Class<?>... sources);

454

455

/**

456

* Set the web application type for the application

457

* @param webApplicationType the web application type

458

* @return the current builder (for chaining)

459

*/

460

public SpringApplicationBuilder web(WebApplicationType webApplicationType);

461

462

/**

463

* Properties to be passed to the application

464

* @param defaultProperties the properties to set

465

* @return the current builder (for chaining)

466

*/

467

public SpringApplicationBuilder properties(String... defaultProperties);

468

469

/**

470

* Profiles to be used by the application

471

* @param profiles the profiles to set

472

* @return the current builder (for chaining)

473

*/

474

public SpringApplicationBuilder profiles(String... profiles);

475

476

/**

477

* Set the parent ApplicationContext for the application

478

* @param parent the parent context

479

* @return the current builder (for chaining)

480

*/

481

public SpringApplicationBuilder parent(ApplicationContext parent);

482

483

/**

484

* Create an application context (and its parent if specified) and run the application

485

* @param args the application arguments

486

* @return the running ApplicationContext

487

*/

488

public ConfigurableApplicationContext run(String... args);

489

490

/**

491

* Create a child ApplicationContext with a parent

492

* @param sources sources for the child context

493

* @return a new builder for the child context

494

*/

495

public SpringApplicationBuilder child(Class<?>... sources);

496

497

/**

498

* Add initializers to be applied to the SpringApplication

499

* @param initializers the initializers to add

500

* @return the current builder (for chaining)

501

*/

502

public SpringApplicationBuilder initializers(ApplicationContextInitializer<?>... initializers);

503

504

/**

505

* Add listeners to be applied to the SpringApplication

506

* @param listeners the listeners to add

507

* @return the current builder (for chaining)

508

*/

509

public SpringApplicationBuilder listeners(ApplicationListener<?>... listeners);

510

511

/**

512

* Set additional configuration for the application

513

* @param configuration the configuration to add

514

* @return the current builder (for chaining)

515

*/

516

public SpringApplicationBuilder bannerMode(Banner.Mode bannerMode);

517

}

518

```

519

520

**Usage Examples:**

521

522

```java

523

// Basic builder usage

524

@SpringBootApplication

525

public class MyApplication {

526

527

public static void main(String[] args) {

528

new SpringApplicationBuilder(MyApplication.class)

529

.web(WebApplicationType.SERVLET)

530

.profiles("production")

531

.properties("server.port=8080")

532

.run(args);

533

}

534

}

535

536

// Hierarchical context setup

537

@Configuration

538

public class ParentConfig {

539

// Shared beans for all contexts

540

}

541

542

@SpringBootApplication

543

public class ChildApplication {

544

545

public static void main(String[] args) {

546

ApplicationContext parent = new SpringApplicationBuilder(ParentConfig.class)

547

.web(WebApplicationType.NONE)

548

.run(args);

549

550

new SpringApplicationBuilder(ChildApplication.class)

551

.parent(parent)

552

.web(WebApplicationType.SERVLET)

553

.run(args);

554

}

555

}

556

557

// Multiple child contexts

558

public class MultiContextApplication {

559

560

public static void main(String[] args) {

561

SpringApplicationBuilder parent = new SpringApplicationBuilder()

562

.sources(SharedConfig.class)

563

.web(WebApplicationType.NONE);

564

565

// Web context

566

parent.child(WebConfig.class)

567

.web(WebApplicationType.SERVLET)

568

.properties("server.port=8080")

569

.run(args);

570

571

// Batch context

572

parent.child(BatchConfig.class)

573

.web(WebApplicationType.NONE)

574

.profiles("batch")

575

.run(args);

576

}

577

}

578

```

579

580

## Core Constants

581

582

```java { .api }

583

public class SpringApplication {

584

/**

585

* The name of the property used to control the application banner location

586

*/

587

public static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";

588

589

/**

590

* The default banner location

591

*/

592

public static final String BANNER_LOCATION_PROPERTY_VALUE = "banner.txt";

593

}

594

```