micronaut-stack-hygiene
Skill1 starsChanged 6 months ago0BSDView on GitHub
Enforce Micronaut-correct annotations, imports, and bean patterns when writing or modifying Java code in a Micronaut project. Use this skill whenever the user is implementing, refactoring, or reviewing Java code in a project that declares Micronaut on the classpath (io.micronaut.* in pom.xml or build.gradle), even when the user does not mention Micronaut explicitly. Especially load when annotations like @Singleton, @Inject, @Factory, @ConfigurationProperties, @Value, @MicronautTest, @Property, @Bean, @Replaces, or @Primary appear in code under discussion, or when the user mentions beans, dependency injection, configuration classes, factories, or test property overrides.
In this repository9 other files it gives its agents.
The file
Show its rules and text
We split this skill into the rules it gives an agent
17 rules. About 0 are common, 4 are distinctive, and 13 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
13
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 43
When in doubt about a package path, write the full FQN as a comment next to the import.
- Distinctive: line 59
If you find any of these in existing code, treat it as drift (see the workflow skill, §12).
- Very specific: line 65
Default scope is singleton via @Singleton.
- Very specific: line 66
Prefer constructor injection over field injection.
- Very specific: line 67
Avoid @Inject on fields unless interoperating with a framework that requires it (rare).
- Very specific: line 71
Use @Factory for a class whose methods produce beans of types you don't own (third-party clients, SDK clients, etc.):
- Very specific: line 79
Do not use @Configuration from Spring — it is silently ignored by Micronaut's compile-time processor.
- Very specific: line 84
Prefer record types for @ConfigurationProperties when the config is immutable (a getter-style record works in Micronaut 4+).
- Very specific: line 85
Add jakarta.validation.constraints.* annotations on fields/parameters.
- Very specific: line 122
Do not confuse Micronaut Test's io.micronaut.test.annotation.MockBean with Spring's similarly named annotation.
- Very specific: line 126
When multiple beans implement an interface, mark the default with @Primary (io.micronaut.context.annotation.Primary).
- Very specific: line 130
Use @Requires(...) (io.micronaut.context.annotation.Requires) for conditional bean wiring (env-specific, property-driven, classpath-based).
- Very specific: line 136
Calling
Micronaut.run(...)in non-main contexts: Only the application entry point should bootstrap the context. - Very specific: line 137
@Valueon a field that needs validation: Use @ConfigurationProperties with jakarta.validation constraints instead. - Distinctive: line 143
When citing any annotation from the table in §1 or referencing a Micronaut feature, write the full FQN at first use within a file.
- Very specific: line 145
If a code section uses an annotation whose package is ambiguous from context (e.g.
- Distinctive: line 161
When this skill conflicts with project-level conventions in CLAUDE.md, CLAUDE.md wins.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Micronaut Stack Hygiene
A skill for keeping Java code in a Micronaut project free of Spring-isms and aligned with idiomatic Micronaut patterns. The cost of getting this wrong is that the code often compiles — Spring annotations on the classpath happily coexist with Micronaut ones — but behaves incorrectly at runtime, in ways that are hard to diagnose.
Minimum protocol
On load. Confirm Micronaut version, annotation processors, project code-style rules, and whether Micronaut for Spring interop is intentionally enabled. Inspect ambiguous imports rather than assuming every Spring annotation is inert. Cite the full FQN at first use of any annotation from the §1 map.
Stop on. Unsupported annotations that alter runtime semantics or conflict with authoritative project style. Do not rewrite intentionally supported interoperability code as drift. Project CLAUDE.md §Code Style that contradicts this skill — CLAUDE.md wins.
Expected output shape. Code with explicit Micronaut FQN imports, no Spring imports, constructor injection preferred over field injection. When ambiguous (@Value, @Primary, @Property), the resolved FQN is named in the surrounding text or a one-line comment.
When this skill applies
Active in any Java project where io.micronaut.* appears as a dependency. The framework is compile-time DI (AOT bean wiring, no runtime reflection), which makes the failure modes different from Spring: Spring annotations on a Micronaut class are silently ignored, not flagged at compile time.
Common triggers: writing a new @Singleton, refactoring a configuration class, adding a test, replacing a bean for testing, working with @ConfigurationProperties, or wiring up a factory method.
1. The Spring-Micronaut annotation map
These annotations share names across frameworks but live in different packages with different semantics. When using any of them, always cite the full import path explicitly.
| Concept | Micronaut (correct) | Spring (do NOT use in Micronaut code) |
| Singleton bean | jakarta.inject.Singleton | org.springframework.stereotype.Component |
| Constructor/field injection | jakarta.inject.Inject | org.springframework.beans.factory.annotation.Autowired |
| Configuration class with factory methods | io.micronaut.context.annotation.Factory | org.springframework.context.annotation.Configuration + @Bean |
Factory method inside @Factory | io.micronaut.context.annotation.Bean | org.springframework.context.annotation.Bean |
| Externalized config binding | io.micronaut.context.annotation.ConfigurationProperties | org.springframework.boot.context.properties.ConfigurationProperties (different semantics) |
| Single property injection | io.micronaut.context.annotation.Value | org.springframework.beans.factory.annotation.Value (different semantics) |
| Test class annotation (JUnit 5) | io.micronaut.test.extensions.junit5.annotation.MicronautTest | org.springframework.boot.test.context.SpringBootTest |
| Test class annotation (Spock) | io.micronaut.test.extensions.spock.annotation.MicronautTest | — |
| Test property override | io.micronaut.context.annotation.Property | org.springframework.test.context.TestPropertySource |
| Bean replacement for tests | io.micronaut.test.annotation.MockBean or io.micronaut.context.annotation.Replaces | Spring's separately packaged mock annotation (verify version) |
| Primary bean among candidates | io.micronaut.context.annotation.Primary | org.springframework.context.annotation.Primary (same name, different runtime) |
| Bean validation constraints | jakarta.validation.constraints.* | jakarta.validation.constraints.* (same package; the surrounding stack differs) |
When in doubt about a package path, write the full FQN as a comment next to the import. Compile-time DI in Micronaut means the wrong import is often a silent no-op, not a build error.
2. Banned Spring patterns
These are disallowed by default for native-only Micronaut projects. With a compatible micronaut-spring compile-time integration, a supported subset of Spring annotations can be translated; check the actual build and project policy before treating them as errors. (Verified as of 2026-11-12 against the Micronaut for Spring guide: it maps e.g. @Component and @Service to Micronaut's @Bean at compile time. The exact mapping of @Autowired was not confirmed — read the guide's supported-annotations table for your version.)
@SpringBootApplication@RestController,@Controllerfrom Spring Web (Micronaut usesio.micronaut.http.annotation.Controller)@Service,@Repository,@Component(use@Singleton)@Autowired(use@Injector, preferably, constructor injection)@TestPropertySource(use@Propertyor@MicronautTest(propertySources = …))- Spring's
@Valueand@ConfigurationProperties(different package, different binding) - Spring's
@MockBeanwhen not intentionally supported; Micronaut Test providesio.micronaut.test.annotation.MockBean @SpringBootTest(use@MicronautTest)- Spring
@Transactionalfromorg.springframework.transaction.annotation(Micronaut provides its own transactional support viaio.micronaut.transaction.annotation.Transactional)
If you find any of these in existing code, treat it as drift (see the workflow skill, §12). Do not silently rewrite; report and ask.
3. Positive Micronaut idioms
Bean declaration
- Default scope is singleton via
@Singleton. Multi-instance scope is explicit:@Prototype,@RequestScope, etc. - Prefer constructor injection over field injection. Combined with
recordorfinalfields, this gives you immutability and testability without extra annotations. - Avoid
@Injecton fields unless interoperating with a framework that requires it (rare).
Factory methods
Use @Factory for a class whose methods produce beans of types you don't own (third-party clients, SDK clients, etc.):
| Element | Annotation | Where |
| Factory class | @Factory (io.micronaut.context.annotation.Factory) | On the class |
| Producing method | @Singleton or @Bean (io.micronaut.context.annotation.Bean) | On the method |
| Conditional bean | @Requires(...) | On the method or class |
Do not use @Configuration from Spring — it is silently ignored by Micronaut's compile-time processor.
Externalized configuration
- Bind YAML / properties / env to a
@ConfigurationPropertiesclass. - Prefer
recordtypes for@ConfigurationPropertieswhen the config is immutable (a getter-style record works in Micronaut 4+). When binding requires setters (rare), use a mutable class with explicit setters. - Add
jakarta.validation.constraints.*annotations on fields/parameters; combined with@Validatedon the config bean (or globally), Micronaut validates at context init. - Config binding failure should fail the context startup. Do not catch
BeanInstantiationExceptionto "recover" from a malformed config.
Test property overrides
For a one-off override on a single test method:
@Test
@Property(name = "app.feature.enabled", value = "true")
void featureWorksWhenEnabled() { ... }For a file-based override across an entire test class:
@MicronautTest(propertySources = "classpath:test-config.yml")
class MyServiceTest { ... }@Property from io.micronaut.context.annotation.Property works on class level and method level. When applied at method level on @ConfigurationProperties-bound beans, it triggers a RefreshEvent that updates the bound configuration; this works in Micronaut 4+ without additional setup (verified against Micronaut 4.x as of 2026-05-29; reconfirm against your project's exact Micronaut minor). Verify behavior if your bean has complex re-binding requirements.
Bean replacement in tests
For replacing a bean, Micronaut Test provides io.micronaut.test.annotation.MockBean(TargetType.class) (verified as of 2026-11-12 against the Micronaut Test 4.x guides; reconfirm for your Micronaut Test version); alternatively a Micronaut @Bean plus @Replaces can work. Verify the project's test libraries before choosing a pattern. Alternative example:
@MicronautTest
class VehicleTest {
@Bean
@Replaces(EngineClient.class)
EngineClient mockEngine() { return Mockito.mock(EngineClient.class); }
@Inject Vehicle vehicle;
}Do not confuse Micronaut Test's io.micronaut.test.annotation.MockBean with Spring's similarly named annotation. Confirm replacement in a real context test.
Bean selection among candidates
When multiple beans implement an interface, mark the default with @Primary (io.micronaut.context.annotation.Primary). For named selection, use @Named("...") (jakarta.inject.Named) on both the producer and the injection point.
Conditional beans
Use @Requires(...) (io.micronaut.context.annotation.Requires) for conditional bean wiring (env-specific, property-driven, classpath-based). Do not invent runtime if-based bean selection in factories; let @Requires handle it at compile time.
4. Anti-patterns to avoid
- Assuming Spring annotations always do nothing. Unsupported ones can be inert, but Micronaut for Spring can map selected annotations at compile time. Confirm actual compatibility and intended conventions.
- Using
@Autowired"because it works". It may or may not work depending on classpath; even when it works, it signals the author was thinking Spring. Use@Injector constructor injection. - Calling
Micronaut.run(...)in non-main contexts. Only the application entry point should bootstrap the context. Inside Lambda handlers, the bootstrap pattern is different (see Lambda companion skills). @Valueon a field that needs validation. Use@ConfigurationPropertieswithjakarta.validationconstraints instead.@Valueis for simple cases.- Reflection-based bean discovery. Micronaut is AOT — if a bean isn't compile-time discoverable, it doesn't exist. Annotations not processed at compile time are invisible at runtime. This is the opposite of Spring's default.
- Assuming open classes / proxies. Micronaut doesn't proxy by default; subclassing for AOP requires explicit
@Around-style advice. Pattern matching from Spring (final classes blocking proxies) doesn't translate.
5. Verification habit
When citing any annotation from the table in §1 or referencing a Micronaut feature, write the full FQN at first use within a file. Example: instead of @Singleton, write jakarta.inject.Singleton in the import block and ensure no org.springframework.* import sneaks in.
If a code section uses an annotation whose package is ambiguous from context (e.g. @Value, @Primary, @Property), grep the imports before committing:
grep -rn "import org.springframework" src/should return zero results only when native-only Micronaut is required; otherwise review every import against approved interoperability.grep -rn "@Autowired\|@Component\|@Service\|@Repository" src/should return zero results.
These checks fit naturally into a pre-commit hook or a CI lint step.
Composability with other skills
This skill defines hygiene and idioms only. It does not cover:
- Build tooling (Maven, Gradle, shade plugin) — see Java/JVM build skills.
- Lambda packaging (fat jar, SnapStart, JVM cold start) — see Lambda companion skills.
- AWS integration patterns (SDK v2 clients, IAM) — see AWS companion skills.
- The implementation workflow itself (BDD, prereq → red → green, classification of failures) — see the
incremental-implementation-workflowskill.
When this skill conflicts with project-level conventions in CLAUDE.md, CLAUDE.md wins. Defer to the project's ADR and Code Style sections; this skill is a fallback for projects whose CLAUDE.md does not fix these choices.
Official references: https://micronaut-projects.github.io/micronaut-test/latest/guide/ and https://micronaut-projects.github.io/micronaut-spring/ .
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

