Spring Framework Core - IoC Container and Dependency Injection
npx @tessl/cli install tessl/maven-org-springframework--spring-core@6.2.00
# Spring Framework Core Module
1
2
**Package Information**
3
- **Name**: Spring Framework Core (spring-core)
4
- **Type**: Java Framework Library
5
- **Language**: Java (with Kotlin support)
6
- **Version**: 6.2.8
7
- **Repository**: https://github.com/spring-projects/spring-framework
8
9
## Core Imports
10
11
**Gradle Dependency**
12
```gradle
13
dependencies {
14
implementation 'org.springframework:spring-core:6.2.8'
15
}
16
```
17
18
**Maven Dependency**
19
```xml
20
<dependency>
21
<groupId>org.springframework</groupId>
22
<artifactId>spring-core</artifactId>
23
<version>6.2.8</version>
24
</dependency>
25
```
26
27
**Common Import Patterns**
28
```java
29
// Core interfaces and utilities
30
import org.springframework.core.*;
31
import org.springframework.core.annotation.*;
32
import org.springframework.core.io.*;
33
import org.springframework.core.env.*;
34
import org.springframework.core.convert.*;
35
import org.springframework.core.task.*;
36
import org.springframework.aot.*;
37
import org.springframework.util.*;
38
import org.springframework.lang.*;
39
40
// Specific commonly used classes
41
import org.springframework.core.Ordered;
42
import org.springframework.core.PriorityOrdered;
43
import org.springframework.core.ResolvableType;
44
import org.springframework.core.MethodParameter;
45
import org.springframework.core.io.Resource;
46
import org.springframework.core.io.ResourceLoader;
47
import org.springframework.core.env.Environment;
48
import org.springframework.core.convert.ConversionService;
49
import org.springframework.util.StringUtils;
50
import org.springframework.util.ReflectionUtils;
51
```
52
53
## Basic Usage
54
55
**Simple Resource Loading**
56
```java
57
import org.springframework.core.io.*;
58
59
// Create resource loader
60
ResourceLoader loader = new DefaultResourceLoader();
61
62
// Load resources from various sources
63
Resource classpathResource = loader.getResource("classpath:config.properties");
64
Resource fileResource = loader.getResource("file:./data.txt");
65
Resource urlResource = loader.getResource("https://example.com/api");
66
67
// Use resources
68
if (classpathResource.exists()) {
69
InputStream inputStream = classpathResource.getInputStream();
70
// Process input stream
71
}
72
```
73
74
**Type Resolution and Conversion**
75
```java
76
import org.springframework.core.*;
77
import org.springframework.core.convert.*;
78
79
// Resolve generic types
80
ResolvableType listType = ResolvableType.forClass(List.class);
81
ResolvableType stringListType = ResolvableType.forClassWithGenerics(List.class, String.class);
82
Class<?> resolvedClass = stringListType.resolve();
83
84
// Convert between types
85
ConversionService conversionService = new DefaultConversionService();
86
Integer converted = conversionService.convert("123", Integer.class);
87
List<String> stringList = conversionService.convert("a,b,c", List.class);
88
```
89
90
**Annotation Processing**
91
```java
92
import org.springframework.core.annotation.*;
93
94
// Work with annotations
95
@Order(1)
96
public class MyComponent implements Ordered {
97
@Override
98
public int getOrder() {
99
return 1;
100
}
101
}
102
103
// Find annotations on methods/classes
104
Method method = MyComponent.class.getMethod("someMethod");
105
Order orderAnnotation = AnnotationUtils.findAnnotation(method, Order.class);
106
boolean hasOrder = AnnotatedElementUtils.hasAnnotation(MyComponent.class, Order.class);
107
```
108
109
## Architecture
110
111
The Spring Framework Core follows a carefully designed layered architecture that provides the foundational infrastructure for the entire Spring ecosystem:
112
113
### **Foundation Layer**
114
The core abstractions that define Spring's fundamental contracts:
115
- **Ordering System**: `Ordered` and `PriorityOrdered` interfaces for component precedence
116
- **Resource Abstraction**: Unified `Resource` interface abstracting files, classpath, URLs, and streams
117
- **Environment Model**: `Environment` and `PropertyResolver` for configuration management
118
- **Attribute System**: `AttributeAccessor` for metadata attachment and retrieval
119
120
### **Utility Layer**
121
Comprehensive utilities that power Spring's internal operations:
122
- **String Processing**: `StringUtils` for null-safe string manipulation and parsing
123
- **Reflection Utilities**: `ReflectionUtils` for simplified and safe reflection operations
124
- **Collection Support**: `MultiValueMap` and `CollectionUtils` for enhanced data structures
125
- **Path Matching**: `AntPathMatcher` for flexible pattern-based resource resolution
126
127
### **Type System**
128
Advanced type handling and conversion capabilities:
129
- **Generic Type Resolution**: `ResolvableType` for complex generic type introspection
130
- **Method Parameter Analysis**: `MethodParameter` for runtime parameter discovery
131
- **Type Conversion**: `ConversionService` framework for extensible type transformations
132
- **Type Descriptors**: Rich metadata system for conversion context
133
134
### **Integration Layer**
135
Modern integration and optimization features:
136
- **Task Execution**: Flexible asynchronous processing with `TaskExecutor` implementations
137
- **AOT Compilation**: Native image support through `RuntimeHints` and code generation
138
- **Annotation Processing**: Meta-annotation and attribute merging capabilities
139
- **Exception Handling**: Nested exception support for detailed error context
140
141
### **Design Principles**
142
143
**Modularity**: Each layer has clear responsibilities with minimal coupling between components.
144
145
**Extensibility**: Strategy and factory patterns allow customization of core behaviors.
146
147
**Performance**: Optimized implementations with caching, lazy initialization, and efficient algorithms.
148
149
**Type Safety**: Comprehensive generic type support with compile-time and runtime type checking.
150
151
**Integration**: Seamless integration points for other Spring modules (Context, AOP, Web, etc.).
152
153
This architecture enables Spring Core to serve as the stable foundation upon which the entire Spring Framework is built, from dependency injection containers to web applications and enterprise integration solutions.
154
155
## Capabilities
156
157
The Spring Framework Core module provides fundamental infrastructure for the entire Spring ecosystem, organized into these key functional areas:
158
159
### [Core Infrastructure](./core-infrastructure.md)
160
- **Ordered Interface System**: Priority-based ordering for components with `Ordered` and `PriorityOrdered` interfaces
161
- **Type Resolution**: Advanced generic type handling with `ResolvableType` and `MethodParameter`
162
- **Alias Management**: Component name aliasing through `AliasRegistry`
163
- **Attribute Access**: Metadata attachment via `AttributeAccessor`
164
- **Exception Handling**: Nested exception support with `NestedRuntimeException` and `NestedCheckedException`
165
- **Parameter Discovery**: Runtime parameter name resolution
166
167
### [Resource Management](./resource-management.md)
168
- **Resource Abstraction**: Unified `Resource` interface for files, classpath, URLs, and byte arrays
169
- **Resource Loading**: Flexible loading strategies with `ResourceLoader` and pattern matching
170
- **I/O Operations**: Comprehensive support for input/output operations across resource types
171
- **Path Pattern Matching**: Ant-style pattern matching for resource resolution
172
173
### [Type Conversion and Validation](./type-conversion.md)
174
- **Conversion Service**: Extensible type conversion system with `ConversionService`
175
- **Type Descriptors**: Rich type metadata with `TypeDescriptor`
176
- **Custom Converters**: Pluggable converter implementations
177
- **Generic Type Support**: Full generic type conversion capabilities
178
179
### [Annotation Processing](./annotation-processing.md)
180
- **Merged Annotations**: Attribute override and composition support
181
- **Annotation Utilities**: Comprehensive annotation introspection
182
- **Meta-Annotations**: Support for annotation composition and inheritance
183
- **Order Comparators**: Annotation-aware sorting and ordering
184
185
### [Environment and Configuration](./environment-config.md)
186
- **Environment Abstraction**: Profile-based configuration with `Environment`
187
- **Property Sources**: Hierarchical property resolution
188
- **Profile Management**: Runtime profile activation and management
189
- **Property Placeholders**: Expression and placeholder resolution
190
191
### [Task Execution](./task-execution.md)
192
- **Task Executors**: Asynchronous task execution with various strategies
193
- **Virtual Thread Support**: Java Project Loom integration
194
- **Task Decoration**: Customizable task wrapping and enhancement
195
- **Synchronous/Asynchronous**: Both sync and async execution models
196
197
### [AOT Optimization](./aot-optimization.md)
198
- **Runtime Hints**: GraalVM native image optimization hints
199
- **Reflection Registration**: AOT-safe reflection usage
200
- **Resource Registration**: Static resource bundling for native images
201
- **Code Generation**: Ahead-of-time code generation support
202
203
### [Utilities and Bundled Libraries](./utilities.md)
204
- **String Utilities**: Comprehensive string manipulation with `StringUtils`
205
- **Reflection Utilities**: Simplified reflection operations
206
- **Collection Utilities**: Enhanced collection support including `MultiValueMap`
207
- **MIME Type Support**: Content type handling and negotiation
208
- **Path Matching**: Ant-style path pattern matching
209
- **Bundled Libraries**: ASM, CGLib, JavaPoet, and Objenesis integration
210
211
## Core API Overview
212
213
**Key Interfaces**
214
215
```java { .api }
216
// Core ordering and priority
217
public interface Ordered {
218
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
219
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
220
int getOrder();
221
}
222
223
public interface PriorityOrdered extends Ordered {
224
}
225
226
// Resource abstraction
227
public interface Resource extends InputStreamSource {
228
boolean exists();
229
boolean isReadable();
230
boolean isFile();
231
URL getURL() throws IOException;
232
URI getURI() throws IOException;
233
File getFile() throws IOException;
234
String getDescription();
235
InputStream getInputStream() throws IOException;
236
}
237
238
public interface ResourceLoader {
239
String CLASSPATH_URL_PREFIX = "classpath:";
240
Resource getResource(String location);
241
ClassLoader getClassLoader();
242
}
243
244
// Environment and properties
245
public interface Environment extends PropertyResolver {
246
String[] getActiveProfiles();
247
String[] getDefaultProfiles();
248
boolean acceptsProfiles(Profiles... profiles);
249
}
250
251
public interface PropertyResolver {
252
boolean containsProperty(String key);
253
String getProperty(String key);
254
<T> T getProperty(String key, Class<T> targetType);
255
String getRequiredProperty(String key);
256
String resolvePlaceholders(String text);
257
}
258
259
// Type conversion
260
public interface ConversionService {
261
boolean canConvert(Class<?> sourceType, Class<?> targetType);
262
<T> T convert(Object source, Class<T> targetType);
263
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
264
}
265
266
// Task execution
267
public interface TaskExecutor extends Executor {
268
void execute(Runnable task);
269
}
270
271
public interface AsyncTaskExecutor extends TaskExecutor {
272
Future<?> submit(Runnable task);
273
<T> Future<T> submit(Callable<T> task);
274
}
275
```
276
277
**Key Classes**
278
279
```java { .api }
280
// Type resolution
281
public final class ResolvableType {
282
public static ResolvableType forField(Field field);
283
public static ResolvableType forMethodParameter(Method method, int parameterIndex);
284
public static ResolvableType forClass(Class<?> clazz);
285
public static ResolvableType forClassWithGenerics(Class<?> clazz, Class<?>... generics);
286
287
public Class<?> resolve();
288
public ResolvableType getSuperType();
289
public ResolvableType[] getInterfaces();
290
public ResolvableType getGeneric(int... indexes);
291
public boolean isAssignableFrom(ResolvableType other);
292
}
293
294
// Method parameter handling
295
public class MethodParameter {
296
public MethodParameter(Method method, int parameterIndex);
297
public MethodParameter(Constructor<?> constructor, int parameterIndex);
298
299
public Class<?> getParameterType();
300
public Type getGenericParameterType();
301
public Annotation[] getParameterAnnotations();
302
public String getParameterName();
303
public boolean isOptional();
304
}
305
306
// Annotation utilities
307
public abstract class AnnotationUtils {
308
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType);
309
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType);
310
public static Map<String, Object> getAnnotationAttributes(Annotation annotation);
311
public static <A extends Annotation> A synthesizeAnnotation(A annotation);
312
}
313
314
// String utilities
315
public abstract class StringUtils {
316
public static boolean hasLength(String str);
317
public static boolean hasText(String str);
318
public static boolean isEmpty(Object str);
319
public static String trimWhitespace(String str);
320
public static String[] split(String toSplit, String delimiter);
321
public static String[] tokenizeToStringArray(String str, String delimiters);
322
public static String collectionToCommaDelimitedString(Collection<?> coll);
323
public static String capitalize(String str);
324
public static String uncapitalize(String str);
325
}
326
327
// Reflection utilities
328
public abstract class ReflectionUtils {
329
public static Field findField(Class<?> clazz, String name);
330
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes);
331
public static Object invokeMethod(Method method, Object target, Object... args);
332
public static void makeAccessible(Field field);
333
public static void makeAccessible(Method method);
334
public static void doWithFields(Class<?> clazz, FieldCallback fc);
335
public static void doWithMethods(Class<?> clazz, MethodCallback mc);
336
}
337
```
338
339
## Type Definitions
340
341
**Core Types**
342
```java { .api }
343
// Callback interfaces
344
@FunctionalInterface
345
public interface FieldCallback {
346
void doWith(Field field) throws IllegalArgumentException, IllegalAccessException;
347
}
348
349
@FunctionalInterface
350
public interface MethodCallback {
351
void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
352
}
353
354
@FunctionalInterface
355
public interface TaskDecorator {
356
Runnable decorate(Runnable runnable);
357
}
358
359
// Converter interfaces
360
@FunctionalInterface
361
public interface Converter<S, T> {
362
T convert(S source);
363
}
364
365
public interface ConverterFactory<S, R> {
366
<T extends R> Converter<S, T> getConverter(Class<T> targetType);
367
}
368
369
public interface GenericConverter {
370
Set<ConvertiblePair> getConvertibleTypes();
371
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
372
373
final class ConvertiblePair {
374
public ConvertiblePair(Class<?> sourceType, Class<?> targetType);
375
public Class<?> getSourceType();
376
public Class<?> getTargetType();
377
}
378
}
379
380
// Property source types
381
public abstract class PropertySource<T> {
382
public PropertySource(String name, T source);
383
public String getName();
384
public T getSource();
385
public abstract Object getProperty(String name);
386
}
387
388
// Exception types
389
public abstract class NestedRuntimeException extends RuntimeException {
390
public NestedRuntimeException(String msg);
391
public NestedRuntimeException(String msg, Throwable cause);
392
public Throwable getRootCause();
393
public Throwable getMostSpecificCause();
394
}
395
396
public abstract class NestedCheckedException extends Exception {
397
public NestedCheckedException(String msg);
398
public NestedCheckedException(String msg, Throwable cause);
399
public Throwable getRootCause();
400
public Throwable getMostSpecificCause();
401
}
402
```
403
404
**Annotation Types**
405
```java { .api }
406
// Core annotations
407
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
408
@Retention(RetentionPolicy.RUNTIME)
409
@Documented
410
public @interface Order {
411
int value() default Ordered.LOWEST_PRECEDENCE;
412
}
413
414
@Target(ElementType.METHOD)
415
@Retention(RetentionPolicy.RUNTIME)
416
@Documented
417
public @interface AliasFor {
418
String value() default "";
419
String attribute() default "";
420
Class<? extends Annotation> annotation() default Annotation.class;
421
}
422
423
// Null-safety annotations
424
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
425
@Retention(RetentionPolicy.RUNTIME)
426
@Documented
427
public @interface Nullable {
428
}
429
430
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
431
@Retention(RetentionPolicy.RUNTIME)
432
@Documented
433
public @interface NonNull {
434
}
435
436
@Target(ElementType.PACKAGE)
437
@Retention(RetentionPolicy.RUNTIME)
438
@Documented
439
public @interface NonNullApi {
440
}
441
442
@Target(ElementType.PACKAGE)
443
@Retention(RetentionPolicy.RUNTIME)
444
@Documented
445
public @interface NonNullFields {
446
}
447
```
448
449
**Utility Constants**
450
```java { .api }
451
// Spring version information
452
public final class SpringVersion {
453
public static String getVersion();
454
}
455
456
// Native image detection
457
public final class NativeDetector {
458
public static boolean inNativeImage();
459
}
460
461
// Kotlin detection
462
public final class KotlinDetector {
463
public static boolean isKotlinPresent();
464
public static boolean isKotlinType(Class<?> clazz);
465
}
466
467
// AOT detection
468
public final class AotDetector {
469
public static final String AOT_ENABLED = "spring.aot.enabled";
470
public static boolean useGeneratedArtifacts();
471
}
472
```
473
474
---
475
476
## Package Structure
477
478
The Spring Framework Core module contains **376+ public API components** organized across these packages:
479
480
- **org.springframework.core** (45+ classes) - Core functionality and utilities
481
- **org.springframework.core.annotation** (25+ classes) - Annotation processing and meta-annotation support
482
- **org.springframework.core.io** (35+ classes) - Resource abstraction and I/O operations
483
- **org.springframework.core.env** (20+ classes) - Environment and property management
484
- **org.springframework.core.convert** (25+ classes) - Type conversion system
485
- **org.springframework.core.task** (15+ classes) - Task execution framework
486
- **org.springframework.aot** (40+ classes) - AOT compilation and native image support
487
- **org.springframework.util** (85+ classes) - Comprehensive utility library
488
- **org.springframework.lang** (8+ classes) - Language annotations and utilities
489
- **org.springframework.asm** (50+ classes) - Bundled ASM bytecode library
490
- **org.springframework.cglib** (40+ classes) - Bundled CGLib proxy library
491
- **org.springframework.javapoet** (25+ classes) - Bundled JavaPoet code generation
492
- **org.springframework.objenesis** (8+ classes) - Bundled Objenesis instantiation library
493
494
This comprehensive API surface provides the foundational infrastructure that powers the entire Spring Framework ecosystem, from dependency injection and aspect-oriented programming to web applications and enterprise integration.