Class 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 Detail

      • LOG

        public static final org.slf4j.Logger LOG
    • Constructor Detail

      • BeanUtils

        public BeanUtils()
    • Method Detail

      • setApplicationContext

        public void setApplicationContext​(org.springframework.context.ApplicationContext context)
        Specified by:
        setApplicationContext in interface org.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 bean
        beanId - ID of the bean to fetch
        clazz - 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 bean
        beanId - ID of the bean to fetch
        clazz - 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 fetch
        clazz - 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 />.