0
# Application Runtime
1
2
Core application bootstrapping and lifecycle management functionality. Provides the main entry points for running Micronaut applications with full configuration support and embedded server management.
3
4
## Capabilities
5
6
### Micronaut Bootstrap Class
7
8
Main entry point for running Micronaut applications with fluent configuration API.
9
10
```java { .api }
11
/**
12
* Main entry point for running a Micronaut application
13
* Extends DefaultApplicationContextBuilder for configuration chaining
14
*/
15
public class Micronaut extends DefaultApplicationContextBuilder implements ApplicationContextBuilder {
16
17
/**
18
* Run application with automatic class discovery
19
* @param args Command line arguments
20
* @return Started ApplicationContext
21
*/
22
public static ApplicationContext run(String... args);
23
24
/**
25
* Run application with specific main class
26
* @param cls The application class to run
27
* @param args Command line arguments
28
* @return Started ApplicationContext
29
*/
30
public static ApplicationContext run(Class<?> cls, String... args);
31
32
/**
33
* Run application with multiple classes
34
* @param classes Array of application classes
35
* @param args Command line arguments
36
* @return Started ApplicationContext
37
*/
38
public static ApplicationContext run(Class<?>[] classes, String... args);
39
40
/**
41
* Create Micronaut builder instance with arguments
42
* @param args Command line arguments
43
* @return Micronaut builder for configuration chaining
44
*/
45
public static Micronaut build(String... args);
46
47
/**
48
* Start the application context and embedded applications
49
* @return Started ApplicationContext
50
*/
51
public ApplicationContext start();
52
53
/**
54
* Add classes to be included in application initialization
55
* @param classes Classes to include
56
* @return This Micronaut instance for chaining
57
*/
58
public Micronaut classes(Class<?>... classes);
59
60
/**
61
* Map exception types to exit codes for startup error handling
62
* @param exception Exception class to map
63
* @param mapper Function to convert exception to exit code
64
* @return This Micronaut instance for chaining
65
*/
66
public <T extends Throwable> Micronaut mapError(Class<T> exception, Function<T, Integer> mapper);
67
68
// Configuration methods (inherited from ApplicationContextBuilder)
69
public Micronaut properties(Map<String, Object> properties);
70
public Micronaut environments(String... environments);
71
public Micronaut packages(String... packages);
72
public Micronaut banner(boolean isEnabled);
73
}
74
```
75
76
**Usage Examples:**
77
78
```java
79
// Simple application startup
80
public class Application {
81
public static void main(String[] args) {
82
ApplicationContext context = Micronaut.run(Application.class, args);
83
}
84
}
85
86
// Configured application startup
87
public class ConfiguredApplication {
88
public static void main(String[] args) {
89
ApplicationContext context = Micronaut.build(args)
90
.environments("production")
91
.packages("com.example.services")
92
.properties(Map.of("app.version", "1.0.0"))
93
.start();
94
}
95
}
96
97
// Error handling configuration
98
public class RobustApplication {
99
public static void main(String[] args) {
100
ApplicationContext context = Micronaut.build(args)
101
.mapError(IllegalStateException.class, ex -> 2)
102
.mapError(SecurityException.class, ex -> 3)
103
.start();
104
}
105
}
106
```
107
108
### EmbeddedApplication Interface
109
110
Core interface for runnable applications that manage the ApplicationContext lifecycle.
111
112
```java { .api }
113
/**
114
* Interface for runnable applications that start and manage the ApplicationContext
115
* @param <T> The application type
116
*/
117
public interface EmbeddedApplication<T> extends ApplicationContextLifeCycle<T> {
118
119
/**
120
* Get the application context managed by this application
121
* @return The ApplicationContext instance
122
*/
123
ApplicationContext getApplicationContext();
124
125
/**
126
* Get the application configuration
127
* @return ApplicationConfiguration instance
128
*/
129
ApplicationConfiguration getApplicationConfiguration();
130
131
/**
132
* Get the environment for this application
133
* @return Environment instance
134
*/
135
Environment getEnvironment();
136
137
/**
138
* Check if this is a long-running server application
139
* @return true if this is a server application
140
*/
141
boolean isServer();
142
143
/**
144
* Check if the application should force exit on shutdown
145
* @return true if should force exit
146
*/
147
boolean isForceExit();
148
149
/**
150
* Check if a shutdown hook should be registered
151
* @return true if shutdown hook is needed
152
*/
153
boolean isShutdownHookNeeded();
154
}
155
```
156
157
### Application Configuration
158
159
Configuration properties for common application settings.
160
161
```java { .api }
162
/**
163
* Common application configuration settings
164
* Bound to "micronaut.application" configuration prefix
165
*/
166
@ConfigurationProperties(ApplicationConfiguration.PREFIX)
167
public class ApplicationConfiguration {
168
169
/** Configuration prefix for application settings */
170
public static final String PREFIX = "micronaut.application";
171
172
/** Property key for application name */
173
public static final String APPLICATION_NAME = "micronaut.application.name";
174
175
/**
176
* Get the default character set for the application
177
* @return Default Charset
178
*/
179
public Charset getDefaultCharset();
180
181
/**
182
* Get the application name if configured
183
* @return Optional application name
184
*/
185
public Optional<String> getName();
186
187
/**
188
* Get instance-specific configuration
189
* @return InstanceConfiguration for this application instance
190
*/
191
public InstanceConfiguration getInstance();
192
}
193
```
194
195
**Configuration Examples:**
196
197
```yaml
198
# application.yml
199
micronaut:
200
application:
201
name: my-microservice
202
instance:
203
id: service-instance-1
204
```
205
206
```java
207
// Accessing configuration
208
@Inject
209
ApplicationConfiguration appConfig;
210
211
public void logAppInfo() {
212
String name = appConfig.getName().orElse("unnamed");
213
Charset charset = appConfig.getDefaultCharset();
214
System.out.println("App: " + name + ", Charset: " + charset);
215
}
216
```
217
218
## Exception Handling
219
220
### ApplicationStartupException
221
222
Exception thrown during application startup failures.
223
224
```java { .api }
225
/**
226
* Exception thrown during application startup failures
227
* Extends RuntimeException for unchecked exception handling
228
*/
229
public class ApplicationStartupException extends RuntimeException {
230
public ApplicationStartupException(String message);
231
public ApplicationStartupException(String message, Throwable cause);
232
}
233
```
234
235
**Usage Example:**
236
237
```java
238
try {
239
ApplicationContext context = Micronaut.run(Application.class, args);
240
} catch (ApplicationStartupException e) {
241
logger.error("Failed to start application: " + e.getMessage(), e);
242
System.exit(1);
243
}
244
```