Class CurrentApplicationContext

  • All Implemented Interfaces:
    org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

    public class CurrentApplicationContext
    extends Object
    implements org.springframework.context.ApplicationContextAware
    Based on this Stackoveflow comment. The bean exposes the current Spring application context to static methods. Register it as
     @Bean
     public CurrentApplicationContext currentAppContext() {
            return new CurrentApplicationContext();
     }
     
    Once registered, static methods can obtain bean references:
     static {
            SomeService myService = CurrentApplicationContext.getContext().getBean(...);
     }
     
     public static void foo() {
            SomeBean bean = CurrentApplicationComntext.getContext().getBean(...);
     }
     
    • Constructor Detail

      • CurrentApplicationContext

        public CurrentApplicationContext()
    • Method Detail

      • setApplicationContext

        public void setApplicationContext​(org.springframework.context.ApplicationContext context)
                                   throws org.springframework.beans.BeansException
        Specified by:
        setApplicationContext in interface org.springframework.context.ApplicationContextAware
        Throws:
        org.springframework.beans.BeansException
      • getContext

        public static org.springframework.context.ApplicationContext getContext()
        Note that this is a static method which expose ApplicationContext.
        Returns:
        the instance of the current Spring ApplicationContext