Skip to content

Fix Micronaut Dependency Injection Not Working

DodaTech Updated 2026-06-24 1 min read

In this tutorial, you'll learn about Fix Micronaut Dependency Injection Not Working. We cover key concepts, practical examples, and best practices.

The Problem

Micronaut @Inject fails with NoSuchBeanException. Required beans are not found in the application context.

Quick Fix

Add @Singleton annotation

Wrong:

@Inject
private ProductService productService;
// ProductService not annotated

Output:

NoSuchBeanException

Right:

@Singleton
public class ProductService { }

Output:

Bean registered

Use @Factory for beans

Wrong:

// No @Factory
public class AppConfig {
    @Bean
    public DataSource ds() { }
}

Output:

Bean not created

Right:

@Factory
public class AppConfig {
    @Bean
    public DataSource dataSource() { }
}

Output:

Bean created via factory

Scan correct package

Wrong:

@Micronaut
public class Application { }
// Default scan only

Output:

Beans outside root not found

Right:

@Micronaut
@ScanClasspath("com.example.services")
public class Application { }

Output:

Additional packages scanned

Prevention

  • Annotate classes with @Singleton or @Prototype
  • Use @Factory for bean-producing methods
  • Configure package scanning with @ScanClasspath

Common Mistakes with di error

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world MICRONAUT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does @Inject return null?

Micronaut processes annotations at compile time. Ensure the class is annotated and in a scanned package.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro