Package org.powertac.common.spring
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 Summary
Constructors Constructor Description SpringApplicationContext() -
Method Summary
Modifier and Type Method Description static ObjectgetBean(String beanName)Returns the Spring bean, if any, with the given name.static <T> TgetBeanByType(Class<T> type)Returns the first Spring bean found that is an instance of the given class.static org.springframework.context.ApplicationContextgetContext()static <T> List<T>listBeansOfType(Class<T> type)Returns all the Spring beans that are instances of the given type.static <T> Map<String,T>mapBeansOfType(Class<T> type)Returns a map of all the Spring beans that are instances of the given type.voidsetApplicationContext(org.springframework.context.ApplicationContext appContext)
-
Constructor Details
-
SpringApplicationContext
public SpringApplicationContext()
-
-
Method Details
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext appContext) throws org.springframework.beans.BeansException- Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Throws:
org.springframework.beans.BeansException
-
getBean
Returns the Spring bean, if any, with the given name. -
getBeanByType
Returns the first Spring bean found that is an instance of the given class. -
listBeansOfType
Returns all the Spring beans that are instances of the given type. -
mapBeansOfType
Returns a map of all the Spring beans that are instances of the given type. -
getContext
public static org.springframework.context.ApplicationContext getContext()
-