Core dependency injection interfaces and components for the Micronaut Framework
npx @tessl/cli install tessl/maven-io-micronaut--micronaut-inject@4.9.00
# Micronaut Inject
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: io.micronaut:micronaut-inject
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Version**: 4.9.11
10
- **Installation**: Add to your Maven `pom.xml`:
11
12
```xml
13
<dependency>
14
<groupId>io.micronaut</groupId>
15
<artifactId>micronaut-inject</artifactId>
16
<version>4.9.11</version>
17
</dependency>
18
```
19
20
Or Gradle `build.gradle`:
21
22
```gradle
23
implementation 'io.micronaut:micronaut-inject:4.9.11'
24
```
25
26
## Core Imports
27
28
```java
29
import io.micronaut.context.ApplicationContext;
30
import io.micronaut.context.BeanContext;
31
import io.micronaut.inject.BeanDefinition;
32
import jakarta.inject.Inject;
33
import jakarta.inject.Singleton;
34
```
35
36
## Basic Usage
37
38
```java
39
import io.micronaut.context.ApplicationContext;
40
import jakarta.inject.Singleton;
41
import jakarta.inject.Inject;
42
43
// Define a service
44
@Singleton
45
public class UserService {
46
public String getUser() {
47
return "John Doe";
48
}
49
}
50
51
// Define a controller with dependency injection
52
@Singleton
53
public class UserController {
54
private final UserService userService;
55
56
@Inject
57
public UserController(UserService userService) {
58
this.userService = userService;
59
}
60
61
public String handleRequest() {
62
return userService.getUser();
63
}
64
}
65
66
// Bootstrap application context
67
public class Application {
68
public static void main(String[] args) {
69
try (ApplicationContext context = ApplicationContext.run()) {
70
UserController controller = context.getBean(UserController.class);
71
System.out.println(controller.handleRequest());
72
}
73
}
74
}
75
```
76
77
## Architecture
78
79
The Micronaut Inject framework is built around several key components:
80
81
- **ApplicationContext**: The main IoC container that manages bean lifecycle and resolution
82
- **BeanDefinition**: Compile-time metadata about beans including injection points and dependencies
83
- **Qualifiers**: Selection criteria for choosing between multiple bean candidates
84
- **Environment**: Configuration and property resolution system
85
- **Event System**: Application lifecycle and bean event management
86
- **Scoping**: Bean lifecycle management (Singleton, Prototype, custom scopes)
87
88
## Capabilities
89
90
### Application Context Management
91
92
Core container functionality for managing beans, their lifecycle, and dependency resolution.
93
94
```java { .api }
95
public interface ApplicationContext extends BeanContext, LifeCycle<ApplicationContext> {
96
static ApplicationContext run();
97
static ApplicationContext run(Class<?> mainClass, String... args);
98
static ApplicationContextBuilder builder();
99
}
100
```
101
102
[Application Context Management](./application-context.md)
103
104
### Bean Location and Providers
105
106
Programmatic bean discovery, lazy instantiation, and provider pattern for flexible bean access.
107
108
```java { .api }
109
public interface BeanProvider<T> extends jakarta.inject.Provider<T> {
110
boolean isPresent();
111
boolean isUnique();
112
Iterator<T> iterator();
113
Stream<T> stream();
114
}
115
116
public interface BeanLocator {
117
<T> T getBean(Class<T> beanType);
118
<T> Optional<T> findBean(Class<T> beanType);
119
<T> Collection<T> getBeansOfType(Class<T> beanType);
120
}
121
```
122
123
[Bean Location and Providers](./bean-providers.md)
124
125
### Bean Definition and Injection
126
127
Core dependency injection interfaces for defining beans and their injection points.
128
129
```java { .api }
130
public interface BeanDefinition<T> extends BeanType<T> {
131
Class<T> getBeanType();
132
List<Argument<?>> getConstructorArguments();
133
Collection<MethodInjectionPoint<T, ?>> getInjectedMethods();
134
T instantiate(BeanResolutionContext resolutionContext, BeanContext context);
135
}
136
```
137
138
[Bean Definition and Injection](./bean-definition.md)
139
140
### Configuration and Environment
141
142
Environment and property resolution system for accessing configuration values.
143
144
```java { .api }
145
public interface Environment extends PropertyResolver {
146
Collection<String> getActiveNames();
147
<T> Optional<T> getProperty(String name, Class<T> requiredType);
148
boolean containsProperty(String name);
149
}
150
```
151
152
[Configuration and Environment](./environment-config.md)
153
154
### Annotations and Qualifiers
155
156
Annotation system for dependency injection, bean scoping, and conditional loading.
157
158
```java { .api }
159
// Core Jakarta Inject annotations
160
@Inject
161
@Singleton
162
@Named("beanName")
163
164
// Micronaut-specific annotations
165
@Bean
166
@Factory
167
@Primary
168
@Prototype
169
@Value("${property.name}")
170
@ConfigurationProperties("prefix")
171
```
172
173
[Annotations and Qualifiers](./annotations.md)
174
175
### Bean Factory and Advanced Configuration
176
177
Factory pattern for programmatic bean creation and advanced configuration building.
178
179
```java { .api }
180
@Target({TYPE, METHOD})
181
@Retention(RUNTIME)
182
public @interface Factory {
183
}
184
185
@Target({METHOD})
186
@Retention(RUNTIME)
187
public @interface Bean {
188
}
189
190
public interface RuntimeBeanDefinition<T> extends BeanDefinition<T> {
191
static <T> RuntimeBeanDefinition<T> of(Class<T> type);
192
static <T> RuntimeBeanDefinition<T> of(Class<T> type, BeanFactory<T> factory);
193
}
194
```
195
196
[Bean Factory and Advanced Configuration](./bean-factory.md)
197
198
### Event System
199
200
Application lifecycle and bean event management for reactive programming patterns.
201
202
```java { .api }
203
public interface ApplicationEventPublisher<T> {
204
void publishEvent(T event);
205
}
206
207
public interface ApplicationEventListener<E> {
208
void onApplicationEvent(E event);
209
boolean supports(E event);
210
}
211
```
212
213
[Event System](./events.md)
214
215
### Scoping and Lifecycle
216
217
Bean scoping and lifecycle management including custom scope implementations.
218
219
```java { .api }
220
public interface CustomScope<T> {
221
Class<? extends Annotation> annotationType();
222
<T> T get(BeanCreationContext<T> creationContext);
223
}
224
225
public interface LifeCycle<T> {
226
boolean isRunning();
227
T start();
228
T stop();
229
}
230
```
231
232
[Scoping and Lifecycle](./scoping.md)
233
234
### Bean Processing and Extensibility
235
236
Framework extension points for custom bean and method processing.
237
238
```java { .api }
239
public interface BeanDefinitionProcessor<T> {
240
BeanDefinition<T> process(BeanDefinition<T> beanDefinition, BeanContext context);
241
}
242
243
public interface ExecutableMethodProcessor<T> {
244
void process(BeanDefinition<?> beanDefinition, ExecutableMethod<T, ?> method);
245
}
246
247
public interface BeanDefinitionRegistry {
248
<T> boolean containsBean(Class<T> beanType);
249
<T> BeanDefinition<T> findBeanDefinition(Class<T> beanType);
250
<T> Collection<BeanDefinition<T>> getBeanDefinitions(Class<T> beanType);
251
}
252
```
253
254
[Bean Processing and Extensibility](./bean-processing.md)
255
256
### Exception Handling
257
258
Comprehensive exception hierarchy for dependency injection error handling.
259
260
```java { .api }
261
public class NoSuchBeanException extends BeanContextException
262
public class NonUniqueBeanException extends BeanContextException
263
public class BeanInstantiationException extends BeanContextException
264
public class DependencyInjectionException extends BeanContextException
265
```
266
267
[Exception Handling](./exceptions.md)
268
269
## Types
270
271
### Core Context Types
272
273
```java { .api }
274
public interface BeanContext extends BeanLocator {
275
<T> T getBean(Class<T> beanType);
276
<T> Optional<T> findBean(Class<T> beanType);
277
<T> Collection<T> getBeansOfType(Class<T> beanType);
278
<T> BeanDefinition<T> getBeanDefinition(Class<T> beanType);
279
}
280
281
public interface BeanResolutionContext {
282
BeanContext getContext();
283
BeanDefinition<?> getRootDefinition();
284
Path getPath();
285
}
286
```
287
288
### Bean Definition Types
289
290
```java { .api }
291
public interface InjectionPoint<T> {
292
BeanDefinition getDeclaringBean();
293
Argument<T> getArgument();
294
}
295
296
public interface MethodInjectionPoint<B, T> extends InjectionPoint<T>, ExecutableMethod<B, T> {
297
T invoke(B instance, BeanResolutionContext resolutionContext, BeanContext context);
298
}
299
300
public interface FieldInjectionPoint<B, T> extends InjectionPoint<T> {
301
void set(B instance, BeanResolutionContext resolutionContext, BeanContext context);
302
}
303
```
304
305
### Configuration Types
306
307
```java { .api }
308
public interface PropertySource {
309
String getName();
310
Object get(String key);
311
Iterator<String> iterator();
312
}
313
314
public interface PropertyResolver {
315
boolean containsProperty(String name);
316
<T> Optional<T> getProperty(String name, Class<T> requiredType);
317
<T> T getProperty(String name, Class<T> requiredType, T defaultValue);
318
}
319
```