0
# Micronaut Framework
1
2
Micronaut is a modern, JVM-based framework designed for building modular, easily testable microservice and serverless applications. It provides dependency injection, AOP, auto-configuration, distributed configuration, service discovery, HTTP routing, and client-side load balancing while achieving fast startup time, reduced memory footprint, minimal reflection usage, and no runtime bytecode generation through compile-time framework infrastructure computation.
3
4
## Package Information
5
6
- **Package Name**: micronaut-core
7
- **Package Type**: maven
8
- **Language**: Java (with Kotlin and Groovy support)
9
- **Installation**: See [Installation Guide](https://micronaut.io/guides/creating-your-first-micronaut-app/)
10
11
## Core Imports
12
13
```java
14
import io.micronaut.context.ApplicationContext;
15
import io.micronaut.runtime.Micronaut;
16
import io.micronaut.http.annotation.*;
17
import io.micronaut.context.annotation.*;
18
import jakarta.inject.*;
19
```
20
21
Gradle dependencies:
22
23
```gradle
24
implementation("io.micronaut:micronaut-http-client")
25
implementation("io.micronaut:micronaut-http-server-netty")
26
implementation("io.micronaut:micronaut-inject")
27
implementation("io.micronaut:micronaut-context")
28
```
29
30
## Basic Usage
31
32
```java
33
import io.micronaut.runtime.Micronaut;
34
import io.micronaut.http.annotation.*;
35
import jakarta.inject.Singleton;
36
37
@Controller("/hello")
38
public class HelloController {
39
40
@Get("/{name}")
41
public String hello(String name) {
42
return "Hello " + name;
43
}
44
}
45
46
@Singleton
47
public class GreetingService {
48
public String createGreeting(String name) {
49
return "Hello " + name + "!";
50
}
51
}
52
53
// Application main class
54
public class Application {
55
public static void main(String[] args) {
56
Micronaut.run(Application.class, args);
57
}
58
}
59
```
60
61
## Architecture
62
63
Micronaut is built around several key architectural principles:
64
65
- **Compile-time DI**: Dependency injection resolution occurs at compile time, eliminating reflection overhead
66
- **Bean Introspection**: Compile-time generation of bean metadata for fast startup
67
- **Reactive Programming**: Built-in support for reactive streams and non-blocking I/O
68
- **Annotation Processing**: Extensive use of annotation processors for framework infrastructure
69
- **Modular Design**: Highly modular architecture allowing selective feature inclusion
70
- **Cloud Native**: Designed for cloud deployment with support for service discovery, distributed configuration, and monitoring
71
72
## Capabilities
73
74
### Dependency Injection
75
76
Compile-time dependency injection container with no reflection, providing fast application startup and minimal memory footprint.
77
78
```java { .api }
79
@Singleton
80
public class ServiceExample {
81
private final Repository repository;
82
83
public ServiceExample(Repository repository) {
84
this.repository = repository;
85
}
86
}
87
88
@Bean
89
public Repository createRepository() {
90
return new DatabaseRepository();
91
}
92
```
93
94
[Dependency Injection](./dependency-injection.md)
95
96
### HTTP Server
97
98
High-performance HTTP server with both blocking and non-blocking APIs, supporting reactive programming patterns.
99
100
```java { .api }
101
@Controller("/api")
102
public class ApiController {
103
104
@Get("/users/{id}")
105
Single<User> getUser(Long id) {
106
return userService.findById(id);
107
}
108
109
@Post("/users")
110
Single<HttpResponse<User>> createUser(@Body User user) {
111
return userService.save(user)
112
.map(HttpResponse::created);
113
}
114
}
115
```
116
117
[HTTP Server](./http-server.md)
118
119
### HTTP Client
120
121
Declarative HTTP client with reactive support, service discovery integration, and comprehensive error handling.
122
123
```java { .api }
124
@Client("/api")
125
public interface UserClient {
126
127
@Get("/users/{id}")
128
Single<User> getUser(Long id);
129
130
@Post("/users")
131
Single<User> createUser(@Body User user);
132
}
133
```
134
135
[HTTP Client](./http-client.md)
136
137
### Configuration
138
139
Type-safe configuration binding with environment-specific property loading and hot-reload support for development.
140
141
```java { .api }
142
@ConfigurationProperties("app.database")
143
public class DatabaseConfiguration {
144
private String host;
145
private int port;
146
private String username;
147
148
// getters and setters
149
}
150
```
151
152
[Configuration](./configuration.md)
153
154
### Aspect-Oriented Programming
155
156
Compile-time AOP with method and constructor interception, providing around advice and introduction support without runtime proxy generation.
157
158
```java { .api }
159
@Around
160
@InterceptorBinding
161
@Retention(RetentionPolicy.RUNTIME)
162
public @interface Timed {
163
}
164
165
@Singleton
166
public class TimedInterceptor implements MethodInterceptor<Object, Object> {
167
@Override
168
public Object intercept(InvocationContext<Object, Object> context) {
169
// timing logic
170
return context.proceed();
171
}
172
}
173
```
174
175
[Aspect-Oriented Programming](./aop.md)
176
177
### Reactive Programming
178
179
Full reactive streams support with backpressure handling, enabling non-blocking I/O operations throughout the framework.
180
181
```java { .api }
182
@Controller("/stream")
183
public class StreamController {
184
185
@Get(value = "/events", produces = MediaType.TEXT_EVENT_STREAM)
186
Publisher<String> events() {
187
return Flowable.interval(1, TimeUnit.SECONDS)
188
.map(i -> "Event " + i);
189
}
190
}
191
```
192
193
[Reactive Programming](./reactive.md)
194
195
### Scheduling and Async
196
197
Task scheduling and asynchronous execution with configurable thread pools and execution contexts.
198
199
```java { .api }
200
@Singleton
201
public class ScheduledService {
202
203
@Scheduled(fixedDelay = "5s")
204
void periodicTask() {
205
// scheduled task logic
206
}
207
208
@Async
209
CompletableFuture<String> asyncOperation() {
210
return CompletableFuture.completedFuture("result");
211
}
212
}
213
```
214
215
[Scheduling and Async](./scheduling.md)
216
217
### WebSocket Support
218
219
Full-duplex WebSocket communication with both client and server implementations.
220
221
```java { .api }
222
@ServerWebSocket("/ws")
223
public class ChatWebSocket {
224
225
@OnOpen
226
public void onOpen(WebSocketSession session) {
227
// connection opened
228
}
229
230
@OnMessage
231
public void onMessage(String message, WebSocketSession session) {
232
session.sendSync(message, MediaType.TEXT_PLAIN_TYPE);
233
}
234
}
235
```
236
237
[WebSocket Support](./websocket.md)
238
239
### Message-Driven Applications
240
241
Support for building message-driven applications with various messaging systems.
242
243
```java { .api }
244
@MessageListener
245
public class MessageHandler {
246
247
@MessageMapping("user.created")
248
void handleUserCreated(@MessageBody User user) {
249
// handle user creation event
250
}
251
}
252
```
253
254
[Messaging](./messaging.md)
255
256
### Application Management
257
258
Built-in management endpoints for health checks, metrics, and application information.
259
260
```java { .api }
261
@Endpoint(id = "custom", defaultEnabled = true)
262
public class CustomEndpoint {
263
264
@Read
265
public Map<String, Object> status() {
266
return Map.of("status", "healthy");
267
}
268
}
269
```
270
271
[Management](./management.md)
272
273
### Retry and Circuit Breaker
274
275
Resilience patterns with configurable retry logic and circuit breaker implementation.
276
277
```java { .api }
278
@Singleton
279
public class ExternalService {
280
281
@Retryable(attempts = "3", delay = "1s")
282
public String callExternal() {
283
// external service call
284
}
285
286
@CircuitBreaker
287
public String reliableCall() {
288
// call with circuit breaker protection
289
}
290
}
291
```
292
293
[Retry and Circuit Breaker](./retry.md)
294
295
### Function as a Service
296
297
Support for serverless functions with integration for AWS Lambda, Google Cloud Functions, and Azure Functions.
298
299
```java { .api }
300
@FunctionBean("hello")
301
public class HelloFunction implements Function<String, String> {
302
303
@Override
304
public String apply(String input) {
305
return "Hello " + input;
306
}
307
}
308
```
309
310
[Function as a Service](./functions.md)
311
312
## Types
313
314
```java { .api }
315
// Core application context
316
public interface ApplicationContext extends BeanContext, LifeCycle<ApplicationContext> {
317
static ApplicationContext build();
318
static ApplicationContext run();
319
static ApplicationContext run(String... args);
320
}
321
322
// Main application runner
323
public final class Micronaut {
324
public static ApplicationContext run();
325
public static ApplicationContext run(String... args);
326
public static ApplicationContext run(Class<?> cls, String... args);
327
}
328
329
// HTTP request/response types
330
public interface HttpRequest<B> extends HttpMessage<B> {
331
HttpMethod getMethod();
332
URI getUri();
333
String getPath();
334
Map<String, Object> getAttributes();
335
}
336
337
public interface HttpResponse<B> extends HttpMessage<B> {
338
HttpStatus getStatus();
339
int code();
340
String reason();
341
}
342
343
// Bean definition and context
344
public interface BeanDefinition<T> extends BeanType<T> {
345
Class<T> getBeanType();
346
Optional<Class<?>> getDeclaringType();
347
boolean isEnabled(BeanContext context);
348
}
349
350
public interface BeanContext extends BeanLocator, LifeCycle<BeanContext> {
351
<T> T getBean(Class<T> beanType);
352
<T> Optional<T> findBean(Class<T> beanType);
353
<T> Collection<T> getBeansOfType(Class<T> beanType);
354
}
355
```