or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

annotations.mdapplication-context.mdbean-definition.mdbean-factory.mdbean-processing.mdbean-providers.mdenvironment-config.mdevents.mdexceptions.mdindex.mdscoping.md
tile.json

tessl/maven-io-micronaut--micronaut-inject

Core dependency injection interfaces and components for the Micronaut Framework

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.micronaut/micronaut-inject@4.9.x

To install, run

npx @tessl/cli install tessl/maven-io-micronaut--micronaut-inject@4.9.0

index.mddocs/

Micronaut Inject

Micronaut Inject provides core dependency injection (DI) and inversion of control (IoC) capabilities for the Micronaut Framework. It implements Jakarta Inject API standards and offers compile-time dependency injection that eliminates runtime reflection, proxy generation, and bytecode manipulation, enabling fast application startup times, reduced memory footprint, and easy unit testing.

Package Information

  • Package Name: io.micronaut:micronaut-inject
  • Package Type: Maven
  • Language: Java
  • Version: 4.9.11
  • Installation: Add to your Maven pom.xml:
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-inject</artifactId>
    <version>4.9.11</version>
</dependency>

Or Gradle build.gradle:

implementation 'io.micronaut:micronaut-inject:4.9.11'

Core Imports

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.BeanContext;
import io.micronaut.inject.BeanDefinition;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

Basic Usage

import io.micronaut.context.ApplicationContext;
import jakarta.inject.Singleton;
import jakarta.inject.Inject;

// Define a service
@Singleton
public class UserService {
    public String getUser() {
        return "John Doe";
    }
}

// Define a controller with dependency injection
@Singleton  
public class UserController {
    private final UserService userService;
    
    @Inject
    public UserController(UserService userService) {
        this.userService = userService;
    }
    
    public String handleRequest() {
        return userService.getUser();
    }
}

// Bootstrap application context
public class Application {
    public static void main(String[] args) {
        try (ApplicationContext context = ApplicationContext.run()) {
            UserController controller = context.getBean(UserController.class);
            System.out.println(controller.handleRequest());
        }
    }
}

Architecture

The Micronaut Inject framework is built around several key components:

  • ApplicationContext: The main IoC container that manages bean lifecycle and resolution
  • BeanDefinition: Compile-time metadata about beans including injection points and dependencies
  • Qualifiers: Selection criteria for choosing between multiple bean candidates
  • Environment: Configuration and property resolution system
  • Event System: Application lifecycle and bean event management
  • Scoping: Bean lifecycle management (Singleton, Prototype, custom scopes)

Capabilities

Application Context Management

Core container functionality for managing beans, their lifecycle, and dependency resolution.

public interface ApplicationContext extends BeanContext, LifeCycle<ApplicationContext> {
    static ApplicationContext run();
    static ApplicationContext run(Class<?> mainClass, String... args);
    static ApplicationContextBuilder builder();
}

Application Context Management

Bean Location and Providers

Programmatic bean discovery, lazy instantiation, and provider pattern for flexible bean access.

public interface BeanProvider<T> extends jakarta.inject.Provider<T> {
    boolean isPresent();
    boolean isUnique();
    Iterator<T> iterator();
    Stream<T> stream();
}

public interface BeanLocator {  
    <T> T getBean(Class<T> beanType);
    <T> Optional<T> findBean(Class<T> beanType);
    <T> Collection<T> getBeansOfType(Class<T> beanType);
}

Bean Location and Providers

Bean Definition and Injection

Core dependency injection interfaces for defining beans and their injection points.

public interface BeanDefinition<T> extends BeanType<T> {
    Class<T> getBeanType();
    List<Argument<?>> getConstructorArguments();
    Collection<MethodInjectionPoint<T, ?>> getInjectedMethods();
    T instantiate(BeanResolutionContext resolutionContext, BeanContext context);
}

Bean Definition and Injection

Configuration and Environment

Environment and property resolution system for accessing configuration values.

public interface Environment extends PropertyResolver {
    Collection<String> getActiveNames();
    <T> Optional<T> getProperty(String name, Class<T> requiredType);
    boolean containsProperty(String name);
}

Configuration and Environment

Annotations and Qualifiers

Annotation system for dependency injection, bean scoping, and conditional loading.

// Core Jakarta Inject annotations
@Inject
@Singleton  
@Named("beanName")

// Micronaut-specific annotations
@Bean
@Factory
@Primary
@Prototype
@Value("${property.name}")
@ConfigurationProperties("prefix")

Annotations and Qualifiers

Bean Factory and Advanced Configuration

Factory pattern for programmatic bean creation and advanced configuration building.

@Target({TYPE, METHOD})
@Retention(RUNTIME)
public @interface Factory {
}

@Target({METHOD})  
@Retention(RUNTIME)
public @interface Bean {
}

public interface RuntimeBeanDefinition<T> extends BeanDefinition<T> {
    static <T> RuntimeBeanDefinition<T> of(Class<T> type);
    static <T> RuntimeBeanDefinition<T> of(Class<T> type, BeanFactory<T> factory);
}

Bean Factory and Advanced Configuration

Event System

Application lifecycle and bean event management for reactive programming patterns.

public interface ApplicationEventPublisher<T> {
    void publishEvent(T event);
}

public interface ApplicationEventListener<E> {
    void onApplicationEvent(E event);
    boolean supports(E event);
}

Event System

Scoping and Lifecycle

Bean scoping and lifecycle management including custom scope implementations.

public interface CustomScope<T> {
    Class<? extends Annotation> annotationType();
    <T> T get(BeanCreationContext<T> creationContext);
}

public interface LifeCycle<T> {
    boolean isRunning();
    T start();
    T stop();
}

Scoping and Lifecycle

Bean Processing and Extensibility

Framework extension points for custom bean and method processing.

public interface BeanDefinitionProcessor<T> {
    BeanDefinition<T> process(BeanDefinition<T> beanDefinition, BeanContext context);
}

public interface ExecutableMethodProcessor<T> {
    void process(BeanDefinition<?> beanDefinition, ExecutableMethod<T, ?> method);
}

public interface BeanDefinitionRegistry {
    <T> boolean containsBean(Class<T> beanType);
    <T> BeanDefinition<T> findBeanDefinition(Class<T> beanType);
    <T> Collection<BeanDefinition<T>> getBeanDefinitions(Class<T> beanType);
}

Bean Processing and Extensibility

Exception Handling

Comprehensive exception hierarchy for dependency injection error handling.

public class NoSuchBeanException extends BeanContextException
public class NonUniqueBeanException extends BeanContextException  
public class BeanInstantiationException extends BeanContextException
public class DependencyInjectionException extends BeanContextException

Exception Handling

Types

Core Context Types

public interface BeanContext extends BeanLocator {
    <T> T getBean(Class<T> beanType);
    <T> Optional<T> findBean(Class<T> beanType);
    <T> Collection<T> getBeansOfType(Class<T> beanType);
    <T> BeanDefinition<T> getBeanDefinition(Class<T> beanType);
}

public interface BeanResolutionContext {
    BeanContext getContext();
    BeanDefinition<?> getRootDefinition();
    Path getPath();
}

Bean Definition Types

public interface InjectionPoint<T> {
    BeanDefinition getDeclaringBean();
    Argument<T> getArgument();
}

public interface MethodInjectionPoint<B, T> extends InjectionPoint<T>, ExecutableMethod<B, T> {
    T invoke(B instance, BeanResolutionContext resolutionContext, BeanContext context);
}

public interface FieldInjectionPoint<B, T> extends InjectionPoint<T> {
    void set(B instance, BeanResolutionContext resolutionContext, BeanContext context);
}

Configuration Types

public interface PropertySource {
    String getName();
    Object get(String key);
    Iterator<String> iterator();
}

public interface PropertyResolver {
    boolean containsProperty(String name);
    <T> Optional<T> getProperty(String name, Class<T> requiredType);
    <T> T getProperty(String name, Class<T> requiredType, T defaultValue);
}