Package org.opennms.core.spring
Class BeanUtils
- java.lang.Object
-
- org.opennms.core.spring.BeanUtils
-
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.context.ApplicationContextAware
public class BeanUtils extends Object implements org.springframework.context.ApplicationContextAware
Helper methods for working with Spring beans.- Version:
- $Id: $
- Author:
- DJ Gregor
-
-
Field Summary
Fields Modifier and Type Field Description static org.slf4j.Logger
LOG
-
Constructor Summary
Constructors Constructor Description BeanUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> void
assertAutowiring(T instance)
Check that all fields that are marked with @Autowired are not null.static <T> T
getBean(String contextId, String beanId, Class<T> clazz)
Helper method that combines getBeanFactory and getBean.static <T> T
getBean(org.springframework.beans.factory.access.BeanFactoryReference beanFactory, String beanId, Class<T> clazz)
Get a Spring bean by name.static org.springframework.beans.factory.access.BeanFactoryReference
getBeanFactory(String contextId)
Get a Spring BeanFactory by context ID.static <T> T
getFactory(String contextId, Class<T> clazz)
Helper method that calls getBeanFactory(contextId).getFactory() and casts the result.void
setApplicationContext(org.springframework.context.ApplicationContext context)
static void
setStaticApplicationContext(org.springframework.context.ApplicationContext context)
-
-
-
Method Detail
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext context)
- Specified by:
setApplicationContext
in interfaceorg.springframework.context.ApplicationContextAware
-
setStaticApplicationContext
public static void setStaticApplicationContext(org.springframework.context.ApplicationContext context)
-
getBeanFactory
public static org.springframework.beans.factory.access.BeanFactoryReference getBeanFactory(String contextId)
Get a Spring BeanFactory by context ID.- Parameters:
contextId
- the context ID of the BeanFactory to fetch- Returns:
- the BeanFactory
-
getBean
public static <T> T getBean(org.springframework.beans.factory.access.BeanFactoryReference beanFactory, String beanId, Class<T> clazz)
Get a Spring bean by name. Uses Java 5 generics to cast the returned class to the appropriate type.- Type Parameters:
T
- class for the returned bean- Parameters:
beanFactory
- bean factory to use to fetch this beanbeanId
- ID of the bean to fetchclazz
- class representing the type for the returned bean- Returns:
- bean for given bean ID casted to the appropriate class
-
getBean
public static <T> T getBean(String contextId, String beanId, Class<T> clazz)
Helper method that combines getBeanFactory and getBean.- Type Parameters:
T
- class for the returned bean- Parameters:
contextId
- the context ID of the BeanFactory from which to fetch this beanbeanId
- ID of the bean to fetchclazz
- class representing the type for the returned bean- Returns:
- bean for given bean ID casted to the appropriate class
-
getFactory
public static <T> T getFactory(String contextId, Class<T> clazz)
Helper method that calls getBeanFactory(contextId).getFactory() and casts the result.- Type Parameters:
T
- class for the returned factory- Parameters:
contextId
- the context ID of the BeanFactory to fetchclazz
- class representing the type for the returned factory- Returns:
- the factory casted to <T>
-
assertAutowiring
public static <T> void assertAutowiring(T instance)
Check that all fields that are marked with @Autowired are not null. This will identify classes that have been loaded by Spring but have not been autowired via<context:annotation-config />
.
-
-