Package com.ultikits.ultitools.context
Class ContextHolder
java.lang.Object
com.ultikits.ultitools.context.ContextHolder
Static holder for accessing the current IoC container context.
This provides a convenient way to access the container from places where dependency injection is not available, such as in AOP interceptors.
Note: Prefer constructor/field injection when possible. This class is mainly for framework-internal use.
- Since:
- 6.2.0
- Author:
- wisdomme
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()Clears the global context.static <T> TGets a bean from the global context by type.static <T> TGets a bean from the global context.static SimpleContainerGets the global container context.static voidsetContext(SimpleContainer container) Sets the global container context.
-
Field Details
-
context
-
-
Constructor Details
-
ContextHolder
private ContextHolder()
-
-
Method Details
-
setContext
Sets the global container context.This should be called once during application startup.
- Parameters:
container- the container to set as global context
-
getContext
Gets the global container context.- Returns:
- the current container, or null if not set
-
getBean
Gets a bean from the global context.Convenience method equivalent to
getContext().getBean(name).- Type Parameters:
T- the expected type- Parameters:
name- the bean name- Returns:
- the bean instance
- Throws:
IllegalStateException- if context is not set
-
getBean
Gets a bean from the global context by type.- Type Parameters:
T- the expected type- Parameters:
type- the bean type- Returns:
- the bean instance
- Throws:
IllegalStateException- if context is not set
-
clear
public static void clear()Clears the global context.Should be called during application shutdown.
-