Back to Blog

dependency_injectionannotationmicronaut
Micronaut Core Annotations
Micronaut Core Annotations – All-in-One Cheat Sheet (2025/2026)
S
Sumiran Dahal
•4 min read
Share:

Micronaut Core Annotations – All-in-One Cheat Sheet (2025/2026)
One instance for the entire application (default scope)
@Singleton
public class UserService {
}
Most commonly used
Very lightweight (no reflection)
Preferred over Spring’s @Service
@Prototype
New instance created every time it is injected
Java@Prototype
public class TempProcessor {
}
Best for:
Stateful objects
Short-lived / temporary logic
@Inject
Dependency injection
Field injection (discouraged):
Java@Inject
UserService userService;
Recommended – Constructor injection:
Java@Singleton
public class OrderService {
private final UserService userService;
public OrderService(UserService userService) {
this.userService = userService;
}
}
→ Constructor injection is the best practice 💯
@Named
Select a specific bean when multiple implementations exist
Java@Named("email")
@Inject
NotificationService service;
🌐 Web / HTTP / Controller Layer
@Controller
Defines a controller with base path
Java@Controller("/users")
public class UserController {
}
HTTP Method Annotations
Java@Get("/")
@Post("/")
@Put("/{id}")
@Patch("/{id}")
@Delete("/{id}")
@Head("/")
@Options("/")
Example:
Java@Get("/{id}")
public User getUser(Long id) {
return userService.find(id);
}
@Body
Bind request body
Java@Post("/")
public User create(@Body User user) {
return userService.save(user);
}
@QueryValue
Query parameters
Java@Get("/")
public List<User> list(@QueryValue("page") int page,
@QueryValue(value = "size", defaultValue = "20") int size) {
// ...
}
@PathVariable
URL path variables
Java@Get("/{id}")
public User get(@PathVariable Long id) {
// ...
}
📦 JSON Serialization / Deserialization
@Serdeable
Enables compile-time JSON (de)serialization – very fast & GraalVM native friendly
Java@Serdeable
public class User {
public Long id;
public String name;
public LocalDateTime createdAt;
}
Variants:
Java@Serdeable.Serializable // only serialize (used in responses)
@Serdeable.Deserializable // only deserialize (used in requests)
🔥 Core advantage of Micronaut: compile-time serialization → no reflection, tiny memory footprint, excellent native image support
⚙️ Configuration & Properties
@ConfigurationProperties
Strongly-typed configuration binding
YAMLapp:
name: AwesomeApp
api:
key: xyz123
timeout: 30s
Java@ConfigurationProperties("app")
public class AppConfig {
private String name;
private ApiConfig api;
// getters & setters
public static class ApiConfig {
private String key;
private Duration timeout;
// getters & setters
}
}
@Value
Inject single configuration value
Java@Value("${app.name:Default App}")
private String appName;
@Value("${feature.new-ui:false}")
private boolean newUiEnabled;
@Requires
Conditional bean loading
Java@Requires(env = "dev")
@Singleton
public class DevOnlyLoggingService { ... }
@Requires(property = "feature.s3.enabled", value = "true")
@Singleton
public class S3StorageService { ... }
Great for environments, feature flags, cloud profiles, etc.
🗄️ Persistence – Micronaut Data
@MappedEntity
Marks class as persistent entity
Java@MappedEntity
public class User {
@Id
@GeneratedValue(GeneratedValue.Type.AUTO)
private Long id;
private String email;
private String name;
// ...
}
@Repository
Repository / DAO interface
Java@Repository
public interface UserRepository extends CrudRepository<User, Long> {
// auto-implemented: save, findById, deleteById, existsById, count, findAll...
List<User> findByEmail(String email);
@Query("FROM User u WHERE u.name LIKE :name%")
List<User> searchByNamePrefix(String name);
}
🔐 Security (Basic Annotations)
@Secured
Protect controller / method
Java@Secured("isAuthenticated()")
@Controller("/profile")
public class ProfileController { ... }
@Secured({"ROLE_ADMIN", "ROLE_MANAGER"})
@Post("/admin/users")
public HttpResponse<?> createUser(...) { ... }
🔄 Lifecycle Hooks
Java@PostConstruct
void initialize() { ... } // after bean creation & injection
@PreDestroy
void cleanup() { ... } // before shutdown
🧪 Testing
@MicronautTest
Full context integration test
Java@MicronautTest
class UserServiceTest {
@Inject
UserService userService;
@Test
void shouldCreateUser() {
// ...
}
}
Other useful test annotations
@MicronautTest(startApplication = false) – no server
@MicronautTest(transactional = false) – disable auto tx
@Property(name = "some.key", value = "test-value")
Quick Mental Model Summary
@Singleton → application-wide single instance
@Prototype → new instance every injection
@Controller + @Get/@Post → HTTP endpoints
@Serdeable → fast compile-time JSON
@ConfigurationProperties → clean config binding
@MappedEntity + @Repository → database access
@Secured → simple security
@PostConstruct / @PreDestroy → lifecycle
@MicronautTest → powerful integration tests