Class BeanUtils

java.lang.Object
io.microsphere.util.BaseUtils
io.microsphere.spring.beans.BeanUtils

public abstract class BeanUtils extends io.microsphere.util.BaseUtils
Bean Utilities Class
Since:
2017.01.13
Author:
Mercy
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    getBeanIfAvailable(org.springframework.beans.factory.BeanFactory beanFactory, String beanName, Class<T> beanType)
    Get the bean via the specified bean name and type if available
    static String[]
    getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Class<?> beanClass)
    Get Bean Names from ListableBeanFactory by type.
    static String[]
    getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors)
    Get Bean Names from ListableBeanFactory by type.
    static String[]
    getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass)
    Get Bean Names from ListableBeanFactory by type.
    static String[]
    getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors)
    Get Bean Names from ListableBeanFactory by type.
    static <T> T
    getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> beanClass)
    Get Optional Bean by Class.
    static <T> T
    getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> beanClass, boolean includingAncestors)
    Get Optional Bean by Class including ancestors(BeanFactory).
    static <T> List<T>
    getSortedBeans(org.springframework.beans.factory.BeanFactory beanFactory, Class<T> type)
    Get all sorted Beans of BeanFactory in specified bean type.
    static <T> List<T>
    getSortedBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> type)
    Get all sorted Beans of ListableBeanFactory in specified bean type.
    static void
    invokeAwareInterfaces(Object bean, org.springframework.beans.factory.BeanFactory beanFactory)
    Invoke the Aware interfaces in order : BeanNameAware BeanClassLoaderAware BeanFactoryAware
    static void
    invokeAwareInterfaces(Object bean, org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)
    Invoke the Aware interfaces in order : BeanNameAware BeanClassLoaderAware BeanFactoryAware
    static void
    invokeAwareInterfaces(Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
    Invoke the Aware interfaces in order : BeanNameAware BeanClassLoaderAware BeanFactoryAware
    static void
    invokeAwareInterfaces(Object bean, org.springframework.context.ConfigurableApplicationContext context)
    Invoke Aware interfaces if the given bean implements
    static void
    invokeBeanInterfaces(Object bean, org.springframework.context.ApplicationContext context)
    Invoke Spring Bean interfaces in order: BeanNameAware BeanClassLoaderAware BeanFactoryAware EnvironmentAware EmbeddedValueResolverAware ResourceLoaderAware ApplicationEventPublisherAware MessageSourceAware ApplicationStartupAware (Spring Framework 5.3+) ApplicationContextAware InitializingBean
    static void
    invokeBeanInterfaces(Object bean, org.springframework.context.ConfigurableApplicationContext context)
    Invoke Spring Bean interfaces in order: BeanNameAware BeanClassLoaderAware BeanFactoryAware EnvironmentAware EmbeddedValueResolverAware ResourceLoaderAware ApplicationEventPublisherAware MessageSourceAware ApplicationStartupAware (Spring Framework 5.3+) ApplicationContextAware InitializingBean
    static void
     
    static void
     
    static boolean
    isBeanPresent(org.springframework.beans.factory.BeanFactory beanFactory, String beanName, Class<?> beanClass)
    Is Bean Present or not by the specified name and class
    static boolean
    isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass)
    Is Bean Present or not?
    static boolean
    isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors)
    Is Bean Present or not?
    static boolean
    isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanClassName)
    Is Bean Present or not?
    static boolean
    isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanClassName, boolean includingAncestors)
    Is Bean Present or not?
    static Class<?>
    resolveBeanType(String beanClassName, ClassLoader classLoader)
    Resolve Bean Type

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BeanUtils

      public BeanUtils()
  • Method Details

    • isBeanPresent

      public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass)
      Is Bean Present or not?
      Parameters:
      beanFactory - ListableBeanFactory
      beanClass - The Class of Bean
      Returns:
      If present , return true , or false
    • isBeanPresent

      public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors)
      Is Bean Present or not?
      Parameters:
      beanFactory - ListableBeanFactory
      beanClass - The Class of Bean
      includingAncestors - including ancestors or not
      Returns:
      If present , return true , or false
    • isBeanPresent

      public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanClassName)
      Is Bean Present or not?
      Parameters:
      beanFactory - ListableBeanFactory
      beanClassName - The name of Class of Bean
      Returns:
      If present , return true , or false
    • isBeanPresent

      public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanClassName, boolean includingAncestors)
      Is Bean Present or not?
      Parameters:
      beanFactory - ListableBeanFactory
      beanClassName - The name of Class of Bean
      includingAncestors - including ancestors or not
      Returns:
      If present , return true , or false
    • isBeanPresent

      public static boolean isBeanPresent(org.springframework.beans.factory.BeanFactory beanFactory, String beanName, Class<?> beanClass) throws NullPointerException
      Is Bean Present or not by the specified name and class
      Parameters:
      beanFactory - BeanFactory
      beanName - The bean name
      beanClass - The bean class
      Returns:
      If present , return true , or false
      Throws:
      NullPointerException
    • getBeanNames

      public static String[] getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass)
      Get Bean Names from ListableBeanFactory by type.
      Parameters:
      beanFactory - ListableBeanFactory
      beanClass - The Class of Bean
      Returns:
      If found , return the array of Bean Names , or empty array.
    • getBeanNames

      public static String[] getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors)
      Get Bean Names from ListableBeanFactory by type.
      Parameters:
      beanFactory - ListableBeanFactory
      beanClass - The Class of Bean
      includingAncestors - including ancestors or not
      Returns:
      If found , return the array of Bean Names , or empty array.
    • getBeanNames

      public static String[] getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Class<?> beanClass)
      Get Bean Names from ListableBeanFactory by type.
      Parameters:
      beanFactory - ConfigurableListableBeanFactory
      beanClass - The Class of Bean
      Returns:
      If found , return the array of Bean Names , or empty array.
    • getBeanNames

      public static String[] getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors)
      Get Bean Names from ListableBeanFactory by type.
      Parameters:
      beanFactory - ConfigurableListableBeanFactory
      beanClass - The Class of Bean
      includingAncestors - including ancestors or not
      Returns:
      If found , return the array of Bean Names , or empty array.
    • resolveBeanType

      public static Class<?> resolveBeanType(String beanClassName, ClassLoader classLoader)
      Resolve Bean Type
      Parameters:
      beanClassName - the class name of Bean
      classLoader - ClassLoader
      Returns:
      Bean type if can be resolved , or return null.
    • getOptionalBean

      public static <T> T getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> beanClass, boolean includingAncestors) throws org.springframework.beans.BeansException
      Get Optional Bean by Class including ancestors(BeanFactory).
      Type Parameters:
      T - The Class of Bean
      Parameters:
      beanFactory - ListableBeanFactory
      beanClass - The Class of Bean
      includingAncestors - including ancestors or not
      Returns:
      Bean object if found , or return null.
      Throws:
      org.springframework.beans.factory.NoUniqueBeanDefinitionException - if more than one bean of the given type was found
      org.springframework.beans.BeansException
      See Also:
      • BeanFactoryUtils.beanOfTypeIncludingAncestors(ListableBeanFactory, Class)
    • getOptionalBean

      public static <T> T getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> beanClass) throws org.springframework.beans.BeansException
      Get Optional Bean by Class.
      Type Parameters:
      T - The Class of Bean
      Parameters:
      beanFactory - ListableBeanFactory
      beanClass - The Class of Bean
      Returns:
      Bean object if found , or return null.
      Throws:
      org.springframework.beans.factory.NoUniqueBeanDefinitionException - if more than one bean of the given type was found
      org.springframework.beans.BeansException
    • getBeanIfAvailable

      public static <T> T getBeanIfAvailable(org.springframework.beans.factory.BeanFactory beanFactory, String beanName, Class<T> beanType) throws org.springframework.beans.BeansException
      Get the bean via the specified bean name and type if available
      Type Parameters:
      T - the bean type
      Parameters:
      beanFactory - BeanFactory
      beanName - the bean name
      beanType - the class of bean
      Returns:
      the bean if available, or null
      Throws:
      org.springframework.beans.BeansException - in case of creation errors
      Since:
      1.0.0
    • getSortedBeans

      public static <T> List<T> getSortedBeans(org.springframework.beans.factory.BeanFactory beanFactory, Class<T> type)
      Get all sorted Beans of BeanFactory in specified bean type.
      Type Parameters:
      T - bean type
      Parameters:
      beanFactory - BeanFactory
      type - bean type
      Returns:
      all sorted Beans
    • getSortedBeans

      public static <T> List<T> getSortedBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> type)
      Get all sorted Beans of ListableBeanFactory in specified bean type.
      Type Parameters:
      T - bean type
      Parameters:
      beanFactory - ListableBeanFactory
      type - bean type
      Returns:
      all sorted Beans
    • invokeBeanInterfaces

      public static void invokeBeanInterfaces(Object bean, org.springframework.context.ApplicationContext context)
      Invoke Spring Bean interfaces in order:
      • BeanNameAware
      • BeanClassLoaderAware
      • BeanFactoryAware
      • EnvironmentAware
      • EmbeddedValueResolverAware
      • ResourceLoaderAware
      • ApplicationEventPublisherAware
      • MessageSourceAware
      • ApplicationStartupAware (Spring Framework 5.3+)
      • ApplicationContextAware
      • InitializingBean
      Parameters:
      bean - the bean
      context - ApplicationContext
    • invokeBeanInterfaces

      public static void invokeBeanInterfaces(Object bean, org.springframework.context.ConfigurableApplicationContext context)
      Invoke Spring Bean interfaces in order:
      • BeanNameAware
      • BeanClassLoaderAware
      • BeanFactoryAware
      • EnvironmentAware
      • EmbeddedValueResolverAware
      • ResourceLoaderAware
      • ApplicationEventPublisherAware
      • MessageSourceAware
      • ApplicationStartupAware (Spring Framework 5.3+)
      • ApplicationContextAware
      • InitializingBean
      Parameters:
      bean - the bean
      context - ConfigurableApplicationContext
      See Also:
    • invokeInitializingBean

      public static void invokeInitializingBean(Object bean) throws Exception
      Throws:
      Exception
    • invokeAwareInterfaces

      public static void invokeAwareInterfaces(Object bean, org.springframework.beans.factory.BeanFactory beanFactory)
      Invoke the Aware interfaces in order :
      • BeanNameAware
      • BeanClassLoaderAware
      • BeanFactoryAware

      if the argument beanFactory is an instance of ApplicationContext, the more Aware interfaces will be involved :

      • EnvironmentAware
      • EmbeddedValueResolverAware
      • ResourceLoaderAware
      • ApplicationEventPublisherAware
      • MessageSourceAware
      • ApplicationStartupAware (Spring Framework 5.3+)
      • ApplicationContextAware
      Parameters:
      bean - the bean
      beanFactory - BeanFactory
    • invokeAwareInterfaces

      public static void invokeAwareInterfaces(Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
      Invoke the Aware interfaces in order :
      • BeanNameAware
      • BeanClassLoaderAware
      • BeanFactoryAware

      if the argument beanFactory is an instance of ApplicationContext, the more Aware interfaces will be involved :

      • EnvironmentAware
      • EmbeddedValueResolverAware
      • ResourceLoaderAware
      • ApplicationEventPublisherAware
      • MessageSourceAware
      • ApplicationStartupAware (Spring Framework 5.3+)
      • ApplicationContextAware
      Parameters:
      bean - the bean
      beanFactory - ConfigurableBeanFactory
    • invokeAwareInterfaces

      public static void invokeAwareInterfaces(Object bean, org.springframework.beans.factory.BeanFactory beanFactory, @Nullable org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)
      Invoke the Aware interfaces in order :
      • BeanNameAware
      • BeanClassLoaderAware
      • BeanFactoryAware

      if the argument beanFactory is an instance of ApplicationContext, the more Aware interfaces will be involved :

      • EnvironmentAware
      • EmbeddedValueResolverAware
      • ResourceLoaderAware
      • ApplicationEventPublisherAware
      • MessageSourceAware
      • ApplicationStartupAware (Spring Framework 5.3+)
      • ApplicationContextAware
      Parameters:
      bean -
      beanFactory -
      configurableBeanFactory -
    • invokeBeanNameAware

      public static void invokeBeanNameAware(Object bean, String beanName)
    • invokeAwareInterfaces

      public static void invokeAwareInterfaces(Object bean, org.springframework.context.ConfigurableApplicationContext context)
      Invoke Aware interfaces if the given bean implements

      Current implementation keeps the order of invocation Aware interfaces:

      • BeanNameAware
      • BeanClassLoaderAware
      • BeanFactoryAware
      • EnvironmentAware
      • EmbeddedValueResolverAware
      • ResourceLoaderAware
      • ApplicationEventPublisherAware
      • MessageSourceAware
      • ApplicationStartupAware (Spring Framework 5.3+)
      • ApplicationContextAware
      Parameters:
      bean - the bean
      context - ApplicationContext
      See Also: