Class ContextHolder

java.lang.Object
com.ultikits.ultitools.context.ContextHolder

public class ContextHolder extends Object
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 Details

  • Constructor Details

    • ContextHolder

      private ContextHolder()
  • Method Details

    • setContext

      public static void setContext(SimpleContainer container)
      Sets the global container context.

      This should be called once during application startup.

      Parameters:
      container - the container to set as global context
    • getContext

      public static SimpleContainer getContext()
      Gets the global container context.
      Returns:
      the current container, or null if not set
    • getBean

      public static <T> T getBean(String name)
      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

      public static <T> T getBean(Class<T> type)
      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.