Core dependency injection interfaces and components for the Micronaut Framework
npx @tessl/cli install tessl/maven-io-micronaut--micronaut-inject@4.9.0Micronaut 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.
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'import io.micronaut.context.ApplicationContext;
import io.micronaut.context.BeanContext;
import io.micronaut.inject.BeanDefinition;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;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());
}
}
}The Micronaut Inject framework is built around several key components:
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
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);
}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);
}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);
}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")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
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);
}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();
}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
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 BeanContextExceptionpublic 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();
}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);
}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);
}