Class SpringApplicationContext

java.lang.Object
org.powertac.common.spring.SpringApplicationContext
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

@Service
public class SpringApplicationContext
extends Object
implements org.springframework.context.ApplicationContextAware
Static methods to access the Spring application context. It is set up as a service so Spring will create and initialize it. NOTE: These methods should NOT be used in constructors. Doing so makes it virtually impossible to test with mocks, and makes transitive dependencies very difficult in a test environment. Better to use them in individual "service getter" methods that do nothing for dependencies that have already been satisfied by test code using ReflectionTestUtils.setField(). ALSO NOTE: Even when used outside a constructor, there are still potential problems in a test environment, because if any test in a suite initializes Spring, then later tests that do not initialize Spring can retrieve instances from earlier tests, which may not be what the test expects. This can lead to baffling problems.
Author:
John Collins
  • Constructor Details

  • Method Details

    • setApplicationContext

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

      public static Object getBean​(String beanName)
      Returns the Spring bean, if any, with the given name.
    • getBeanByType

      public static <T> T getBeanByType​(Class<T> type)
      Returns the first Spring bean found that is an instance of the given class.
    • listBeansOfType

      public static <T> List<T> listBeansOfType​(Class<T> type)
      Returns all the Spring beans that are instances of the given type.
    • mapBeansOfType

      public static <T> Map<String,​T> mapBeansOfType​(Class<T> type)
      Returns a map of all the Spring beans that are instances of the given type.
    • getContext

      public static org.springframework.context.ApplicationContext getContext()