Class BeanUtils

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

    public class BeanUtils
    extends java.lang.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​(java.lang.String contextId, java.lang.String beanId, java.lang.Class<T> clazz)
      Helper method that combines getBeanFactory and getBean.
      static <T> T getBean​(org.springframework.beans.factory.access.BeanFactoryReference beanFactory, java.lang.String beanId, java.lang.Class<T> clazz)
      Get a Spring bean by name.
      static org.springframework.beans.factory.access.BeanFactoryReference getBeanFactory​(java.lang.String contextId)
      Get a Spring BeanFactory by context ID.
      static <T> T getFactory​(java.lang.String contextId, java.lang.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)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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​(java.lang.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,
                                    java.lang.String beanId,
                                    java.lang.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​(java.lang.String contextId,
                                    java.lang.String beanId,
                                    java.lang.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​(java.lang.String contextId,
                                       java.lang.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 />.