Package com.ultikits.ultitools.context
Class SimpleContainer
java.lang.Object
com.ultikits.ultitools.context.SimpleContainer
Simple dependency injection container to replace Spring ApplicationContext.
简单的依赖注入容器,用于替换Spring ApplicationContext。
简单的依赖注入容器,用于替换Spring ApplicationContext。
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<String, BeanDefinition> private final List<BeanPostProcessor> private final Map<String, SimpleContainer.BeanScope> private ClassLoaderprivate booleanprivate static final Loggerprivate SimpleContainer -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBeanPostProcessor(BeanPostProcessor processor) Add bean post processor.protected voidaddSingleton(String beanName, Object singletonObject) Add a fully initialized singleton to level 1 cache.protected voidaddSingletonFactory(String beanName, Supplier<Object> singletonFactory) Add a singleton factory to level 3 cache for early reference support.voidclose()Close the container.booleancontainsBean(String name) Check if container contains bean.private ObjectcreateBean(String name, BeanDefinition definition) Create bean from definition.private ObjectcreateBeanWithConstructorInjection(Class<?> beanClass) Create bean using constructor auto-wiring.private Constructor<?> findMatchingConstructor(Class<?> beanClass, Object[] args) Find a matching constructor for the given arguments.Get autowire capable bean factory.<T> TGet bean by type.Get bean by name.<T> TGet bean by name and type.String[]Get all bean names.Get bean factory.private <T> StringgetBeanName(Class<T> type) Generate bean name for class.String[]getBeanNamesForType(Class<?> type) Get bean names for type.getBeansOfType(Class<T> type) Get beans of type.Get class loader.<T> TgetHighestPriorityBean(Class<T> type) Get the highest priority bean of the given type.<T> List<T> getOrderedBeansOfType(Class<T> type) Get beans of type ordered by @Service priority.private intgetServicePriority(Class<?> clazz) Get service priority from @Service annotation.protected ObjectgetSingleton(String beanName, boolean allowEarlyReference) Get singleton from three-level cache.Get all singleton bean instances registered in this container (not including parent).Class<?> Get bean type.private voidInvoke methods annotated with @PostConstruct.private voidInvoke methods annotated with @PreDestroy on a bean.private booleanisAssignable(Class<?> paramType, Object value) Check if a value can be assigned to a parameter type.private booleanisPrimitiveWrapperOf(Class<?> primitive, Class<?> wrapper) Check if wrapper is the wrapper class for primitive type.booleanisPrototype(String name) Check if bean is prototype.booleanCheck if container is running.booleanisSingleton(String name) Check if bean is singleton.voidInitialize all singletons.voidprocessConfigurationClass(Class<?> configClass) Process configuration class.voidrefresh()Refresh the container.<T> voidregisterBean(Class<T> type, Object... constructorArgs) Register bean with constructor arguments.voidregisterBeanDefinition(String name, BeanDefinition definition) Register bean definition.voidRegister shutdown hook.voidregisterSingleton(String name, Object instance) Register a singleton instance.voidregisterSupplier(String name, Supplier<Object> supplier) Register a supplier for lazy initialization.voidregisterSupplier(String name, Supplier<Object> supplier, Class<?> type) Register a supplier for lazy initialization with type information.<T> voidregisterType(Class<T> type, T instance) Register a type mapping.<T> voidregisterTypeSupplier(Class<T> type, Supplier<T> supplier) Register a type supplier.voidscanComponents(String... basePackages) Scan components in packages.voidsetClassLoader(ClassLoader classLoader) Set class loader.voidsetDisplayName(String displayName) Set display name.voidSet ID.voidsetParent(SimpleContainer parent) Set parent container.voidstart()Start the container.voidstop()Stop the container.
-
Field Details
-
LOGGER
-
singletonObjects
-
earlySingletonObjects
-
singletonFactories
-
singletons
-
suppliers
-
typeMappings
-
typeSuppliers
-
beanScopes
-
beanTypes
-
beanPostProcessors
-
beanDefinitions
-
currentlyCreating
-
supplierTypes
-
parent
-
classLoader
-
isStarted
private boolean isStarted
-
-
Constructor Details
-
SimpleContainer
public SimpleContainer() -
SimpleContainer
-
-
Method Details
-
getSingleton
Get singleton from three-level cache. This method implements the circular dependency resolution through early singleton exposure.
从三级缓存获取单例。此方法通过提前暴露单例来实现循环依赖解析。- Parameters:
beanName- bean name
Bean名称allowEarlyReference- whether to allow early reference (level 2/3 cache)
是否允许早期引用(二/三级缓存)- Returns:
- singleton instance or null if not found
单例实例,如果未找到则返回null
-
addSingletonFactory
Add a singleton factory to level 3 cache for early reference support.
向三级缓存添加单例工厂以支持早期引用。- Parameters:
beanName- bean name
Bean名称singletonFactory- factory for creating early reference
创建早期引用的工厂
-
addSingleton
Add a fully initialized singleton to level 1 cache. Clears from level 2 and level 3 caches.
向一级缓存添加完全初始化的单例。从二级和三级缓存清除。- Parameters:
beanName- bean name
Bean名称singletonObject- fully initialized singleton
完全初始化的单例
-
registerSingleton
Register a singleton instance.
注册单例实例。- Parameters:
name- instance name
实例名称instance- instance object
实例对象
-
registerSupplier
Register a supplier for lazy initialization.
注册供应商用于延迟初始化。- Parameters:
name- supplier name
供应商名称supplier- supplier function
供应商函数
-
registerSupplier
Register a supplier for lazy initialization with type information.
注册带类型信息的供应商用于延迟初始化。- Parameters:
name- supplier name
供应商名称supplier- supplier function
供应商函数type- the type of bean this supplier produces
供应商生成的Bean类型
-
registerType
Register a type mapping.
注册类型映射。- Parameters:
type- class type
类类型instance- instance object
实例对象
-
registerTypeSupplier
Register a type supplier.
注册类型供应商。- Parameters:
type- class type
类类型supplier- supplier function
供应商函数
-
getBean
Get bean by name.
通过名称获取Bean。- Parameters:
name- bean name
Bean名称- Returns:
- bean instance
Bean实例
-
getBean
Get bean by type.
通过类型获取Bean。- Parameters:
type- bean type
Bean类型- Returns:
- bean instance
Bean实例
-
getBean
Get bean by name and type.
通过名称和类型获取Bean。- Parameters:
name- bean name
Bean名称type- bean type
Bean类型- Returns:
- bean instance
Bean实例
-
getBeanNamesForType
Get bean names for type.
获取指定类型的Bean名称。- Parameters:
type- bean type
Bean类型- Returns:
- bean names
Bean名称数组
-
getAutowireCapableBeanFactory
Get autowire capable bean factory.
获取自动装配Bean工厂。- Returns:
- autowire factory
自动装配工厂
-
close
public void close()Close the container.
关闭容器。 -
containsBean
Check if container contains bean.
检查容器是否包含Bean。- Parameters:
name- bean name
Bean名称- Returns:
- true if contains
如果包含则返回true
-
registerBean
Register bean with constructor arguments.
使用构造器参数注册Bean。- Parameters:
type- bean type
Bean类型constructorArgs- constructor arguments
构造器参数
-
getBeanName
Generate bean name for class.
为类生成Bean名称。 -
refresh
public void refresh()Refresh the container.
刷新容器。 -
getSingletonValues
Get all singleton bean instances registered in this container (not including parent).获取此容器中注册的所有单例 Bean 实例(不包含父容器)。
- Returns:
- unmodifiable collection of singleton beans
-
setDisplayName
Set display name.
设置显示名称。- Parameters:
displayName- display name
显示名称
-
setId
Set ID.
设置ID。- Parameters:
id- container ID
容器ID
-
setClassLoader
Set class loader.
设置类加载器。- Parameters:
classLoader- class loader
类加载器
-
registerShutdownHook
public void registerShutdownHook()Register shutdown hook.
注册关闭钩子。 -
setParent
Set parent container.
设置父容器。- Parameters:
parent- parent container
父容器
-
getBeanFactory
Get bean factory.
获取Bean工厂。- Returns:
- bean factory
Bean工厂
-
getClassLoader
Get class loader.
获取类加载器。- Returns:
- class loader
类加载器
-
registerBeanDefinition
Register bean definition.
注册Bean定义。- Parameters:
name- bean name
Bean名称definition- bean definition
Bean定义
-
createBean
Create bean from definition. Uses three-level cache to support circular dependency resolution for setter injection.
从定义创建Bean。使用三级缓存支持 setter 注入的循环依赖解析。- Parameters:
name- bean name
Bean名称definition- bean definition
Bean定义- Returns:
- created bean
创建的Bean
-
createBeanWithConstructorInjection
Create bean using constructor auto-wiring. Resolves constructor parameters from the container.
使用构造器自动装配创建Bean。从容器中解析构造器参数。- Parameters:
beanClass- the class to instantiate
要实例化的类- Returns:
- new bean instance
新的Bean实例
-
findMatchingConstructor
private Constructor<?> findMatchingConstructor(Class<?> beanClass, Object[] args) throws NoSuchMethodException Find a matching constructor for the given arguments. Supports interface/superclass parameter types and primitive type boxing.
为给定参数查找匹配的构造器。支持接口/父类参数类型和基本类型装箱。- Parameters:
beanClass- the class to find constructor for
要查找构造器的类args- constructor arguments
构造器参数- Returns:
- matching constructor
匹配的构造器 - Throws:
NoSuchMethodException
-
isAssignable
Check if a value can be assigned to a parameter type. Handles primitive types, interfaces, and inheritance.
检查值是否可以分配给参数类型。处理基本类型、接口和继承。- Parameters:
paramType- the parameter type
参数类型value- the value to check
要检查的值- Returns:
- true if assignable
如果可分配则返回true
-
isPrimitiveWrapperOf
Check if wrapper is the wrapper class for primitive type.
检查wrapper是否是primitive类型的包装类。 -
invokePostConstructMethods
Invoke methods annotated with @PostConstruct.
调用带有@PostConstruct注解的方法。- Parameters:
bean- the bean instance
Bean实例
-
invokePreDestroyMethods
Invoke methods annotated with @PreDestroy on a bean.
调用Bean上带有@PreDestroy注解的方法。- Parameters:
bean- the bean instance
Bean实例
-
addBeanPostProcessor
Add bean post processor.
添加Bean后处理器。- Parameters:
processor- bean post processor
Bean后处理器
-
getBeanDefinitionNames
Get all bean names.
获取所有Bean名称。- Returns:
- bean names
Bean名称数组
-
getBeansOfType
Get beans of type.
获取指定类型的Bean。- Parameters:
type- bean type
Bean类型- Returns:
- beans map
Bean映射
-
getOrderedBeansOfType
Get beans of type ordered by @Service priority. Higher priority beans come first.
按 @Service 优先级排序获取指定类型的Bean。 优先级高的Bean排在前面。- Parameters:
type- bean type
Bean类型- Returns:
- ordered list of beans
排序后的Bean列表
-
getHighestPriorityBean
Get the highest priority bean of the given type.
获取指定类型中优先级最高的Bean。- Parameters:
type- bean type
Bean类型- Returns:
- highest priority bean or null
优先级最高的Bean或null
-
getServicePriority
Get service priority from @Service annotation.
从 @Service 注解获取服务优先级。- Parameters:
clazz- the class to check
要检查的类- Returns:
- priority value (default 0)
优先级值(默认0)
-
isSingleton
Check if bean is singleton.
检查Bean是否是单例。- Parameters:
name- bean name
Bean名称- Returns:
- true if singleton
如果是单例则返回true
-
isPrototype
Check if bean is prototype.
检查Bean是否是原型。- Parameters:
name- bean name
Bean名称- Returns:
- true if prototype
如果是原型则返回true
-
getType
Get bean type.
获取Bean类型。- Parameters:
name- bean name
Bean名称- Returns:
- bean type
Bean类型
-
preInstantiateSingletons
public void preInstantiateSingletons()Initialize all singletons.
初始化所有单例。 -
start
public void start()Start the container.
启动容器。 -
stop
public void stop()Stop the container.
停止容器。 -
isRunning
public boolean isRunning()Check if container is running.
检查容器是否正在运行。- Returns:
- true if running
如果正在运行则返回true
-
scanComponents
Scan components in packages.
扫描包中的组件。- Parameters:
basePackages- packages to scan
要扫描的包
-
processConfigurationClass
Process configuration class.
处理配置类。- Parameters:
configClass- configuration class
配置类
-