Spring Framework Core - IoC Container and Dependency Injection
npx @tessl/cli install tessl/maven-org-springframework--spring-core@6.2.0Package Information
Gradle Dependency
dependencies {
implementation 'org.springframework:spring-core:6.2.8'
}Maven Dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.2.8</version>
</dependency>Common Import Patterns
// Core interfaces and utilities
import org.springframework.core.*;
import org.springframework.core.annotation.*;
import org.springframework.core.io.*;
import org.springframework.core.env.*;
import org.springframework.core.convert.*;
import org.springframework.core.task.*;
import org.springframework.aot.*;
import org.springframework.util.*;
import org.springframework.lang.*;
// Specific commonly used classes
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.ResolvableType;
import org.springframework.core.MethodParameter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.env.Environment;
import org.springframework.core.convert.ConversionService;
import org.springframework.util.StringUtils;
import org.springframework.util.ReflectionUtils;Simple Resource Loading
import org.springframework.core.io.*;
// Create resource loader
ResourceLoader loader = new DefaultResourceLoader();
// Load resources from various sources
Resource classpathResource = loader.getResource("classpath:config.properties");
Resource fileResource = loader.getResource("file:./data.txt");
Resource urlResource = loader.getResource("https://example.com/api");
// Use resources
if (classpathResource.exists()) {
InputStream inputStream = classpathResource.getInputStream();
// Process input stream
}Type Resolution and Conversion
import org.springframework.core.*;
import org.springframework.core.convert.*;
// Resolve generic types
ResolvableType listType = ResolvableType.forClass(List.class);
ResolvableType stringListType = ResolvableType.forClassWithGenerics(List.class, String.class);
Class<?> resolvedClass = stringListType.resolve();
// Convert between types
ConversionService conversionService = new DefaultConversionService();
Integer converted = conversionService.convert("123", Integer.class);
List<String> stringList = conversionService.convert("a,b,c", List.class);Annotation Processing
import org.springframework.core.annotation.*;
// Work with annotations
@Order(1)
public class MyComponent implements Ordered {
@Override
public int getOrder() {
return 1;
}
}
// Find annotations on methods/classes
Method method = MyComponent.class.getMethod("someMethod");
Order orderAnnotation = AnnotationUtils.findAnnotation(method, Order.class);
boolean hasOrder = AnnotatedElementUtils.hasAnnotation(MyComponent.class, Order.class);The Spring Framework Core follows a carefully designed layered architecture that provides the foundational infrastructure for the entire Spring ecosystem:
The core abstractions that define Spring's fundamental contracts:
Ordered and PriorityOrdered interfaces for component precedenceResource interface abstracting files, classpath, URLs, and streamsEnvironment and PropertyResolver for configuration managementAttributeAccessor for metadata attachment and retrievalComprehensive utilities that power Spring's internal operations:
StringUtils for null-safe string manipulation and parsingReflectionUtils for simplified and safe reflection operationsMultiValueMap and CollectionUtils for enhanced data structuresAntPathMatcher for flexible pattern-based resource resolutionAdvanced type handling and conversion capabilities:
ResolvableType for complex generic type introspectionMethodParameter for runtime parameter discoveryConversionService framework for extensible type transformationsModern integration and optimization features:
TaskExecutor implementationsRuntimeHints and code generationModularity: Each layer has clear responsibilities with minimal coupling between components.
Extensibility: Strategy and factory patterns allow customization of core behaviors.
Performance: Optimized implementations with caching, lazy initialization, and efficient algorithms.
Type Safety: Comprehensive generic type support with compile-time and runtime type checking.
Integration: Seamless integration points for other Spring modules (Context, AOP, Web, etc.).
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.
The Spring Framework Core module provides fundamental infrastructure for the entire Spring ecosystem, organized into these key functional areas:
Ordered and PriorityOrdered interfacesResolvableType and MethodParameterAliasRegistryAttributeAccessorNestedRuntimeException and NestedCheckedExceptionResource interface for files, classpath, URLs, and byte arraysResourceLoader and pattern matchingConversionServiceTypeDescriptorEnvironmentStringUtilsMultiValueMapKey Interfaces
// Core ordering and priority
public interface Ordered {
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
int getOrder();
}
public interface PriorityOrdered extends Ordered {
}
// Resource abstraction
public interface Resource extends InputStreamSource {
boolean exists();
boolean isReadable();
boolean isFile();
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
String getDescription();
InputStream getInputStream() throws IOException;
}
public interface ResourceLoader {
String CLASSPATH_URL_PREFIX = "classpath:";
Resource getResource(String location);
ClassLoader getClassLoader();
}
// Environment and properties
public interface Environment extends PropertyResolver {
String[] getActiveProfiles();
String[] getDefaultProfiles();
boolean acceptsProfiles(Profiles... profiles);
}
public interface PropertyResolver {
boolean containsProperty(String key);
String getProperty(String key);
<T> T getProperty(String key, Class<T> targetType);
String getRequiredProperty(String key);
String resolvePlaceholders(String text);
}
// Type conversion
public interface ConversionService {
boolean canConvert(Class<?> sourceType, Class<?> targetType);
<T> T convert(Object source, Class<T> targetType);
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
}
// Task execution
public interface TaskExecutor extends Executor {
void execute(Runnable task);
}
public interface AsyncTaskExecutor extends TaskExecutor {
Future<?> submit(Runnable task);
<T> Future<T> submit(Callable<T> task);
}Key Classes
// Type resolution
public final class ResolvableType {
public static ResolvableType forField(Field field);
public static ResolvableType forMethodParameter(Method method, int parameterIndex);
public static ResolvableType forClass(Class<?> clazz);
public static ResolvableType forClassWithGenerics(Class<?> clazz, Class<?>... generics);
public Class<?> resolve();
public ResolvableType getSuperType();
public ResolvableType[] getInterfaces();
public ResolvableType getGeneric(int... indexes);
public boolean isAssignableFrom(ResolvableType other);
}
// Method parameter handling
public class MethodParameter {
public MethodParameter(Method method, int parameterIndex);
public MethodParameter(Constructor<?> constructor, int parameterIndex);
public Class<?> getParameterType();
public Type getGenericParameterType();
public Annotation[] getParameterAnnotations();
public String getParameterName();
public boolean isOptional();
}
// Annotation utilities
public abstract class AnnotationUtils {
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType);
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType);
public static Map<String, Object> getAnnotationAttributes(Annotation annotation);
public static <A extends Annotation> A synthesizeAnnotation(A annotation);
}
// String utilities
public abstract class StringUtils {
public static boolean hasLength(String str);
public static boolean hasText(String str);
public static boolean isEmpty(Object str);
public static String trimWhitespace(String str);
public static String[] split(String toSplit, String delimiter);
public static String[] tokenizeToStringArray(String str, String delimiters);
public static String collectionToCommaDelimitedString(Collection<?> coll);
public static String capitalize(String str);
public static String uncapitalize(String str);
}
// Reflection utilities
public abstract class ReflectionUtils {
public static Field findField(Class<?> clazz, String name);
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes);
public static Object invokeMethod(Method method, Object target, Object... args);
public static void makeAccessible(Field field);
public static void makeAccessible(Method method);
public static void doWithFields(Class<?> clazz, FieldCallback fc);
public static void doWithMethods(Class<?> clazz, MethodCallback mc);
}Core Types
// Callback interfaces
@FunctionalInterface
public interface FieldCallback {
void doWith(Field field) throws IllegalArgumentException, IllegalAccessException;
}
@FunctionalInterface
public interface MethodCallback {
void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
}
@FunctionalInterface
public interface TaskDecorator {
Runnable decorate(Runnable runnable);
}
// Converter interfaces
@FunctionalInterface
public interface Converter<S, T> {
T convert(S source);
}
public interface ConverterFactory<S, R> {
<T extends R> Converter<S, T> getConverter(Class<T> targetType);
}
public interface GenericConverter {
Set<ConvertiblePair> getConvertibleTypes();
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
final class ConvertiblePair {
public ConvertiblePair(Class<?> sourceType, Class<?> targetType);
public Class<?> getSourceType();
public Class<?> getTargetType();
}
}
// Property source types
public abstract class PropertySource<T> {
public PropertySource(String name, T source);
public String getName();
public T getSource();
public abstract Object getProperty(String name);
}
// Exception types
public abstract class NestedRuntimeException extends RuntimeException {
public NestedRuntimeException(String msg);
public NestedRuntimeException(String msg, Throwable cause);
public Throwable getRootCause();
public Throwable getMostSpecificCause();
}
public abstract class NestedCheckedException extends Exception {
public NestedCheckedException(String msg);
public NestedCheckedException(String msg, Throwable cause);
public Throwable getRootCause();
public Throwable getMostSpecificCause();
}Annotation Types
// Core annotations
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Order {
int value() default Ordered.LOWEST_PRECEDENCE;
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AliasFor {
String value() default "";
String attribute() default "";
Class<? extends Annotation> annotation() default Annotation.class;
}
// Null-safety annotations
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Nullable {
}
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NonNull {
}
@Target(ElementType.PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NonNullApi {
}
@Target(ElementType.PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NonNullFields {
}Utility Constants
// Spring version information
public final class SpringVersion {
public static String getVersion();
}
// Native image detection
public final class NativeDetector {
public static boolean inNativeImage();
}
// Kotlin detection
public final class KotlinDetector {
public static boolean isKotlinPresent();
public static boolean isKotlinType(Class<?> clazz);
}
// AOT detection
public final class AotDetector {
public static final String AOT_ENABLED = "spring.aot.enabled";
public static boolean useGeneratedArtifacts();
}The Spring Framework Core module contains 376+ public API components organized across these packages:
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.