Modern, JVM-based framework for building modular, easily testable microservice and serverless applications with compile-time DI and fast startup.
npx @tessl/cli install tessl/maven-micronaut@4.9.0Micronaut 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.
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.Micronaut;
import io.micronaut.http.annotation.*;
import io.micronaut.context.annotation.*;
import jakarta.inject.*;Gradle dependencies:
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-http-server-netty")
implementation("io.micronaut:micronaut-inject")
implementation("io.micronaut:micronaut-context")import io.micronaut.runtime.Micronaut;
import io.micronaut.http.annotation.*;
import jakarta.inject.Singleton;
@Controller("/hello")
public class HelloController {
@Get("/{name}")
public String hello(String name) {
return "Hello " + name;
}
}
@Singleton
public class GreetingService {
public String createGreeting(String name) {
return "Hello " + name + "!";
}
}
// Application main class
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}Micronaut is built around several key architectural principles:
Compile-time dependency injection container with no reflection, providing fast application startup and minimal memory footprint.
@Singleton
public class ServiceExample {
private final Repository repository;
public ServiceExample(Repository repository) {
this.repository = repository;
}
}
@Bean
public Repository createRepository() {
return new DatabaseRepository();
}High-performance HTTP server with both blocking and non-blocking APIs, supporting reactive programming patterns.
@Controller("/api")
public class ApiController {
@Get("/users/{id}")
Single<User> getUser(Long id) {
return userService.findById(id);
}
@Post("/users")
Single<HttpResponse<User>> createUser(@Body User user) {
return userService.save(user)
.map(HttpResponse::created);
}
}Declarative HTTP client with reactive support, service discovery integration, and comprehensive error handling.
@Client("/api")
public interface UserClient {
@Get("/users/{id}")
Single<User> getUser(Long id);
@Post("/users")
Single<User> createUser(@Body User user);
}Type-safe configuration binding with environment-specific property loading and hot-reload support for development.
@ConfigurationProperties("app.database")
public class DatabaseConfiguration {
private String host;
private int port;
private String username;
// getters and setters
}Compile-time AOP with method and constructor interception, providing around advice and introduction support without runtime proxy generation.
@Around
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface Timed {
}
@Singleton
public class TimedInterceptor implements MethodInterceptor<Object, Object> {
@Override
public Object intercept(InvocationContext<Object, Object> context) {
// timing logic
return context.proceed();
}
}Full reactive streams support with backpressure handling, enabling non-blocking I/O operations throughout the framework.
@Controller("/stream")
public class StreamController {
@Get(value = "/events", produces = MediaType.TEXT_EVENT_STREAM)
Publisher<String> events() {
return Flowable.interval(1, TimeUnit.SECONDS)
.map(i -> "Event " + i);
}
}Task scheduling and asynchronous execution with configurable thread pools and execution contexts.
@Singleton
public class ScheduledService {
@Scheduled(fixedDelay = "5s")
void periodicTask() {
// scheduled task logic
}
@Async
CompletableFuture<String> asyncOperation() {
return CompletableFuture.completedFuture("result");
}
}Full-duplex WebSocket communication with both client and server implementations.
@ServerWebSocket("/ws")
public class ChatWebSocket {
@OnOpen
public void onOpen(WebSocketSession session) {
// connection opened
}
@OnMessage
public void onMessage(String message, WebSocketSession session) {
session.sendSync(message, MediaType.TEXT_PLAIN_TYPE);
}
}Support for building message-driven applications with various messaging systems.
@MessageListener
public class MessageHandler {
@MessageMapping("user.created")
void handleUserCreated(@MessageBody User user) {
// handle user creation event
}
}Built-in management endpoints for health checks, metrics, and application information.
@Endpoint(id = "custom", defaultEnabled = true)
public class CustomEndpoint {
@Read
public Map<String, Object> status() {
return Map.of("status", "healthy");
}
}Resilience patterns with configurable retry logic and circuit breaker implementation.
@Singleton
public class ExternalService {
@Retryable(attempts = "3", delay = "1s")
public String callExternal() {
// external service call
}
@CircuitBreaker
public String reliableCall() {
// call with circuit breaker protection
}
}Support for serverless functions with integration for AWS Lambda, Google Cloud Functions, and Azure Functions.
@FunctionBean("hello")
public class HelloFunction implements Function<String, String> {
@Override
public String apply(String input) {
return "Hello " + input;
}
}// Core application context
public interface ApplicationContext extends BeanContext, LifeCycle<ApplicationContext> {
static ApplicationContext build();
static ApplicationContext run();
static ApplicationContext run(String... args);
}
// Main application runner
public final class Micronaut {
public static ApplicationContext run();
public static ApplicationContext run(String... args);
public static ApplicationContext run(Class<?> cls, String... args);
}
// HTTP request/response types
public interface HttpRequest<B> extends HttpMessage<B> {
HttpMethod getMethod();
URI getUri();
String getPath();
Map<String, Object> getAttributes();
}
public interface HttpResponse<B> extends HttpMessage<B> {
HttpStatus getStatus();
int code();
String reason();
}
// Bean definition and context
public interface BeanDefinition<T> extends BeanType<T> {
Class<T> getBeanType();
Optional<Class<?>> getDeclaringType();
boolean isEnabled(BeanContext context);
}
public interface BeanContext extends BeanLocator, LifeCycle<BeanContext> {
<T> T getBean(Class<T> beanType);
<T> Optional<T> findBean(Class<T> beanType);
<T> Collection<T> getBeansOfType(Class<T> beanType);
}