Spring Beans - IoC Container and Dependency Injection providing comprehensive bean management infrastructure and dependency injection capabilities.
npx @tessl/cli install tessl/maven-org-springframework--spring-beans@6.2.0Spring Beans is a core module of the Spring Framework that provides comprehensive Inversion of Control (IoC) container and dependency injection capabilities. This library forms the foundation for Spring's dependency injection system, enabling developers to build loosely coupled, testable applications by managing object creation, wiring, and lifecycle automatically.
pom.xml:<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.2.8</version>
</dependency>import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;For property binding and type conversion:
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyAccessor;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
// Create a bean factory
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
// Load bean definitions from XML
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));
// Retrieve beans
MyService service = factory.getBean("myService", MyService.class);
// Property binding example
BeanWrapper wrapper = new BeanWrapperImpl(new MyBean());
wrapper.setPropertyValue("name", "Spring");
wrapper.setPropertyValue("active", true);Spring Beans is built around several key architectural patterns:
This design provides a flexible, extensible foundation for dependency injection that supports multiple configuration approaches (XML, annotations, Java configuration) while maintaining high performance and modularity.
Core IoC container functionality for managing bean definitions, lifecycle, and dependency injection. Provides the fundamental infrastructure for creating and managing application objects.
interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
String[] getAliases(String name);
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
}
interface HierarchicalBeanFactory extends BeanFactory {
BeanFactory getParentBeanFactory();
boolean containsLocalBean(String name);
}
interface ListableBeanFactory extends BeanFactory {
boolean containsBeanDefinition(String beanName);
int getBeanDefinitionCount();
String[] getBeanDefinitionNames();
String[] getBeanNamesForType(Class<?> type);
String[] getBeanNamesForType(ResolvableType type);
<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException;
}
interface AutowireCapableBeanFactory extends BeanFactory {
int AUTOWIRE_NO = 0;
int AUTOWIRE_BY_NAME = 1;
int AUTOWIRE_BY_TYPE = 2;
int AUTOWIRE_CONSTRUCTOR = 3;
String ORIGINAL_INSTANCE_SUFFIX = ".ORIGINAL";
<T> T createBean(Class<T> beanClass) throws BeansException;
void autowireBean(Object existingBean) throws BeansException;
Object configureBean(Object existingBean, String beanName) throws BeansException;
Object initializeBean(Object existingBean, String beanName) throws BeansException;
Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException;
Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException;
void destroyBean(Object existingBean);
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException;
Object resolveBeanByName(String name, DependencyDescriptor descriptor) throws BeansException;
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName) throws BeansException;
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException;
}Powerful property binding system with automatic type conversion, nested property access, and comprehensive editor support for converting between string representations and Java objects.
interface PropertyAccessor {
void setPropertyValue(String propertyName, Object value) throws BeansException;
void setPropertyValue(PropertyValue pv) throws BeansException;
Object getPropertyValue(String propertyName) throws BeansException;
PropertyDescriptor[] getPropertyDescriptors();
PropertyDescriptor getPropertyDescriptor(String propertyName) throws InvalidPropertyException;
}
interface BeanWrapper extends PropertyAccessor {
void setWrappedInstance(Object object);
Object getWrappedInstance();
Class<?> getWrappedClass();
}Property Access and Type Conversion
Bean definition infrastructure supporting multiple configuration sources (XML, annotations, programmatic) with comprehensive metadata management and validation.
interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
String getBeanClassName();
void setBeanClassName(String beanClassName);
String getScope();
void setScope(String scope);
boolean isLazyInit();
void setLazyInit(boolean lazyInit);
String[] getDependsOn();
void setDependsOn(String... dependsOn);
}
interface BeanDefinitionRegistry extends AliasRegistry {
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException;
void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
}Bean Definition and Configuration
Comprehensive annotation support for dependency injection, lifecycle callbacks, and bean configuration with automatic detection and processing.
@interface Autowired {
boolean required() default true;
}
@interface Qualifier {
String value() default "";
}
@interface Value {
String value();
}
class AutowiredAnnotationBeanPostProcessor implements BeanPostProcessor {
void setAutowiredAnnotationType(Class<? extends Annotation> autowiredAnnotationType);
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
}Annotation-Based Configuration
Extensible XML parsing framework with namespace support, custom schema handling, and comprehensive bean definition loading from XML configuration files.
class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException;
void setValidationMode(int validationMode);
void setNamespaceAware(boolean namespaceAware);
}
interface NamespaceHandler {
void init();
BeanDefinition parse(Element element, ParserContext parserContext);
BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext);
}Ahead-of-time compilation support for native image generation, providing build-time optimization and reflection-free bean instantiation.
interface BeanRegistrationAotProcessor {
BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean);
}
interface BeanFactoryInitializationAotProcessor {
BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory);
}
class InstanceSupplierCodeGenerator {
CodeBlock generateCode(RegisteredBean registeredBean, Executable constructorOrFactoryMethod);
}Comprehensive bean lifecycle management including initialization, destruction, and post-processing hooks for customizing bean creation and configuration.
interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
interface DisposableBean {
void destroy() throws Exception;
}
interface BeanPostProcessor {
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return null;
}
}Support for custom factory beans that create objects through specialized factory logic rather than direct instantiation.
interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
interface SmartFactoryBean<T> extends FactoryBean<T> {
default boolean isPrototype() {
return false;
}
default boolean isEagerInit() {
return false;
}
}// Exception Hierarchy
class BeansException extends RuntimeException {
public BeansException(String msg);
public BeansException(String msg, Throwable cause);
}
class BeanCreationException extends BeansException {
public BeanCreationException(String beanName, String msg);
public BeanCreationException(String beanName, String msg, Throwable cause);
public String getBeanName();
}
class NoSuchBeanDefinitionException extends BeansException {
public NoSuchBeanDefinitionException(String name);
public NoSuchBeanDefinitionException(Class<?> type);
public NoSuchBeanDefinitionException(ResolvableType type);
public String getBeanName();
public Class<?> getBeanType();
public ResolvableType getResolvableType();
}
class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionException {
public NoUniqueBeanDefinitionException(Class<?> type, int numberOfBeansFound, String message);
public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNamesFound);
public int getNumberOfBeansFound();
public Collection<String> getBeanNamesFound();
}
class BeanDefinitionStoreException extends BeansException {
public BeanDefinitionStoreException(String msg);
public BeanDefinitionStoreException(String resourceDescription, String msg);
public BeanDefinitionStoreException(String resourceDescription, String beanName, String msg);
public String getResourceDescription();
public String getBeanName();
}
// Core Interfaces and Types
interface ObjectProvider<T> extends ObjectFactory<T>, Iterable<T> {
T getObject() throws BeansException;
T getIfAvailable() throws BeansException;
T getIfAvailable(Supplier<T> defaultSupplier) throws BeansException;
void ifAvailable(Consumer<T> dependencyConsumer) throws BeansException;
T getIfUnique() throws BeansException;
T getIfUnique(Supplier<T> defaultSupplier) throws BeansException;
void ifUnique(Consumer<T> dependencyConsumer) throws BeansException;
Stream<T> stream();
Stream<T> orderedStream();
}
interface ObjectFactory<T> {
T getObject() throws BeansException;
}
class NamedBeanHolder<T> implements BeanNameAware {
public NamedBeanHolder(String beanName, T beanInstance);
public String getBeanName();
public T getBeanInstance();
}
class DependencyDescriptor implements Serializable {
public DependencyDescriptor(Field field, boolean required);
public DependencyDescriptor(MethodParameter methodParameter, boolean required);
public boolean isRequired();
public boolean isEager();
public Class<?> getDependencyType();
public String getDependencyName();
public ResolvableType getResolvableType();
}
// Property Value Classes
class PropertyValue {
public PropertyValue(String name, Object value);
public String getName();
public Object getValue();
public boolean isOptional();
public boolean isConverted();
public Object getConvertedValue();
}
class MutablePropertyValues implements PropertyValues {
public MutablePropertyValues();
public MutablePropertyValues(PropertyValues original);
public MutablePropertyValues addPropertyValue(PropertyValue pv);
public MutablePropertyValues addPropertyValue(String propertyName, Object propertyValue);
public void addPropertyValues(PropertyValues other);
public PropertyValue[] getPropertyValues();
public PropertyValue getPropertyValue(String propertyName);
public boolean contains(String propertyName);
public boolean isEmpty();
}
// Lifecycle Interfaces
interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
interface DisposableBean {
void destroy() throws Exception;
}
interface BeanNameAware {
void setBeanName(String name);
}
interface BeanFactoryAware {
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
interface BeanClassLoaderAware {
void setBeanClassLoader(ClassLoader classLoader);
}
// Bean Post Processing
interface BeanPostProcessor {
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return null;
}
}
interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
default boolean requiresDestruction(Object bean) {
return true;
}
}
// Factory Bean Support
interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
interface SmartFactoryBean<T> extends FactoryBean<T> {
default boolean isPrototype() {
return false;
}
default boolean isEagerInit() {
return false;
}
}
// Utility Support
interface Mergeable {
boolean isMergeEnabled();
Object merge(Object parent);
}
interface AttributeAccessor {
void setAttribute(String name, Object value);
Object getAttribute(String name);
Object removeAttribute(String name);
boolean hasAttribute(String name);
String[] attributeNames();
}
interface BeanMetadataElement {
default Object getSource() {
return null;
}
}
interface AliasRegistry {
void registerAlias(String name, String alias);
void removeAlias(String alias);
boolean isAlias(String name);
String[] getAliases(String name);
}// Spring Core Types (from spring-core dependency) class ResolvableType { public static ResolvableType forClass(Class<?> clazz); public static ResolvableType forType(Type type); public static ResolvableType forMethodParameter(MethodParameter methodParameter); public static ResolvableType forField(Field field); public Class<?> resolve(); public Class<?> resolve(Class<?> fallback); public ResolvableType[] getGenerics(); public boolean isAssignableFrom(ResolvableType other); public boolean hasGenerics(); }
class MethodParameter { public MethodParameter(Method method, int parameterIndex); public MethodParameter(Constructor<?> constructor, int parameterIndex); public Method getMethod(); public Constructor<?> getConstructor(); public Class<?> getParameterType(); public Type getGenericParameterType(); public String getParameterName(); public int getParameterIndex(); }
// JavaBeans Types (from java.beans) class PropertyDescriptor { public String getName(); public Method getReadMethod(); public Method getWriteMethod(); public Class<?> getPropertyType(); public boolean isBound(); public boolean isConstrained(); }
// Type Conversion Support interface ConversionService { boolean canConvert(Class<?> sourceType, Class<?> targetType); boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); <T> T convert(Object source, Class<T> targetType); Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); }
class TypeDescriptor { public static TypeDescriptor valueOf(Class<?> type); public static TypeDescriptor forObject(Object object); public Class<?> getType(); public ResolvableType getResolvableType(); public boolean isCollection(); public boolean isArray(); public boolean isMap(); }
// Additional Support Interfaces interface Ordered { int HIGHEST_PRECEDENCE = Integer.MIN_VALUE; int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
int getOrder();}
interface Scope { Object get(String name, ObjectFactory<?> objectFactory); Object remove(String name); void registerDestructionCallback(String name, Runnable callback); Object resolveContextualObject(String key); String getConversationId(); }
interface StringValueResolver { String resolveStringValue(String strVal); }
interface BeanExpressionResolver { Object evaluate(String value, BeanExpressionContext evalContext) throws BeansException; }
class BeanExpressionContext { public BeanExpressionContext(ConfigurableBeanFactory beanFactory, Scope scope); public ConfigurableBeanFactory getBeanFactory(); public Scope getScope(); public boolean containsObject(String key); public Object getObject(String key); }
// Property Editor Support abstract class PropertyEditorSupport implements PropertyEditor { public PropertyEditorSupport(); public PropertyEditorSupport(Object source); public void setValue(Object value); public Object getValue(); public boolean isPaintable(); public void paintValue(Graphics gfx, Rectangle box); public String getJavaInitializationString(); public String getAsText(); public void setAsText(String text) throws IllegalArgumentException; public String[] getTags(); public Component getCustomEditor(); public boolean supportsCustomEditor(); public void addPropertyChangeListener(PropertyChangeListener listener); public void removePropertyChangeListener(PropertyChangeListener listener); public void firePropertyChange(); }
interface PropertyEditorRegistry { void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor); void registerCustomEditor(Class<?> requiredType, String propertyPath, PropertyEditor propertyEditor); PropertyEditor findCustomEditor(Class<?> requiredType, String propertyPath); }
interface PropertyEditorRegistrar { void registerCustomEditors(PropertyEditorRegistry registry); }