Class BeanUtils

  • All Implemented Interfaces:
    io.microsphere.util.Utils

    public abstract class BeanUtils
    extends java.lang.Object
    implements io.microsphere.util.Utils
    Bean Utilities Class
    Since:
    1.0.0
    Author:
    Mercy
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.reflect.Constructor<T> findPrimaryConstructor​(java.lang.Class<T> clazz)
      Find the primary constructor of the specified class.
      static <T> T getBeanIfAvailable​(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName, java.lang.Class<T> beanType)
      Retrieve the bean with the specified name and type from the given BeanFactory if it exists.
      static java.lang.String[] getBeanNames​(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
      Get bean names of the specified type from the given ConfigurableListableBeanFactory.
      static java.lang.String[] getBeanNames​(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
      Get bean names of the specified type from the given ConfigurableListableBeanFactory.
      static java.lang.String[] getBeanNames​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
      Get bean names of the specified type from the given ListableBeanFactory.
      static java.lang.String[] getBeanNames​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
      Get Bean Names from ListableBeanFactory by type.
      static <T> T getOptionalBean​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass)
      Retrieve an optional bean of the specified type from the given ListableBeanFactory.
      static <T> T getOptionalBean​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass, boolean includingAncestors)
      Retrieve an optional bean of the specified type from the given ListableBeanFactory.
      static <T> java.util.List<T> getSortedBeans​(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.Class<T> type)
      Retrieve a list of beans of the specified type from the given BeanFactory, sorted based on their order.
      static <T> java.util.List<T> getSortedBeans​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> type)
      Retrieve all beans of the specified type from the given ListableBeanFactory, sorted based on their order.
      static void invokeAwareInterfaces​(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory)
      Invokes the Spring Aware interfaces implemented by the given bean if applicable.
      static void invokeAwareInterfaces​(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)
      Invokes the Spring Aware interfaces implemented by the given bean if applicable.
      static void invokeAwareInterfaces​(java.lang.Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
      Invokes the Spring Aware interfaces implemented by the given bean if applicable.
      static void invokeAwareInterfaces​(java.lang.Object bean, org.springframework.context.ConfigurableApplicationContext context)
      Invoke Aware interfaces if the given bean implements
      static void invokeBeanInterfaces​(java.lang.Object bean, org.springframework.context.ApplicationContext context)
      Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.
      static void invokeBeanInterfaces​(java.lang.Object bean, org.springframework.context.ConfigurableApplicationContext context)
      Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.
      static void invokeBeanNameAware​(java.lang.Object bean, java.lang.String beanName)
      Invokes the BeanNameAware.setBeanName(String) method if the given bean implements the interface.
      static void invokeInitializingBean​(java.lang.Object bean)
      Invoke the InitializingBean.afterPropertiesSet() method if the given bean implements the interface.
      static boolean isBeanPresent​(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName, java.lang.Class<?> beanClass)
      Check if a bean with the specified name and class is present in the given BeanFactory.
      static boolean isBeanPresent​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
      Is Bean Present or not?
      static boolean isBeanPresent​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
      Check if a bean of the specified class is present in the given ListableBeanFactory.
      static boolean isBeanPresent​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName)
      Check if a bean with the specified class name is present in the given ListableBeanFactory.
      static boolean isBeanPresent​(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName, boolean includingAncestors)
      Check if a bean with the specified class name is present in the given ListableBeanFactory.
      static java.lang.Class<?> resolveBeanType​(java.lang.String beanClassName, java.lang.ClassLoader classLoader)
      Resolve the bean type from the given class name using the specified ClassLoader.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isBeanPresent

        public static boolean isBeanPresent​(@Nonnull
                                            org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.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​(@Nonnull
                                            org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.Class<?> beanClass,
                                            boolean includingAncestors)
        Check if a bean of the specified class is present in the given ListableBeanFactory.

        This method checks whether there is at least one bean of the specified type in the bean factory. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

        Example Usage

        
         boolean present = isBeanPresent(beanFactory, MyService.class, true);
         

        Behavior

        • If the bean class is null or not resolvable, an empty array will be returned.
        • If no beans are found and logging is enabled at trace level, a log message will be recorded.
        Parameters:
        beanFactory - the ListableBeanFactory to search for beans
        beanClass - the class of the bean to check presence for
        includingAncestors - whether to include ancestor bean factories in the search
        Returns:
        true if at least one bean of the specified type exists; false otherwise
      • isBeanPresent

        public static boolean isBeanPresent​(@Nonnull
                                            org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.String beanClassName)
        Check if a bean with the specified class name is present in the given ListableBeanFactory.

        This method checks whether there is at least one bean with the specified class name in the bean factory. The check includes only the current bean factory and does not search in ancestor factories.

        Example Usage

        
         boolean present = isBeanPresent(beanFactory, "com.example.MyService");
         

        Behavior

        • The method resolves the class using the bean factory's class loader (if available).
        • If the class cannot be resolved or no bean is found, it returns false.
        Parameters:
        beanFactory - the ListableBeanFactory to search for beans
        beanClassName - the fully qualified name of the class to check presence for
        Returns:
        true if at least one bean of the specified class name exists; false otherwise
      • isBeanPresent

        public static boolean isBeanPresent​(@Nonnull
                                            org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.String beanClassName,
                                            boolean includingAncestors)
        Check if a bean with the specified class name is present in the given ListableBeanFactory.

        This method checks whether there is at least one bean with the specified class name in the bean factory. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

        Example Usage

        
         boolean present = isBeanPresent(beanFactory, "com.example.MyService", true);
         

        Behavior

        • The method resolves the class using the bean factory's class loader (if available).
        • If the class cannot be resolved or no bean is found, it returns false.
        • If logging is enabled at trace level and no bean is found, a log message will be recorded.
        Parameters:
        beanFactory - the ListableBeanFactory to search for beans
        beanClassName - the fully qualified name of the class to check presence for
        includingAncestors - whether to include ancestor bean factories in the search
        Returns:
        true if at least one bean of the specified class name exists; false otherwise
      • isBeanPresent

        public static boolean isBeanPresent​(@Nonnull
                                            org.springframework.beans.factory.BeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.String beanName,
                                            @Nonnull
                                            java.lang.Class<?> beanClass)
                                     throws java.lang.NullPointerException
        Check if a bean with the specified name and class is present in the given BeanFactory.

        This method verifies whether a bean exists in the bean factory by both its name and type. It ensures that the bean is present and matches the expected class type.

        Example Usage

        
         boolean present = isBeanPresent(beanFactory, "myService", MyService.class);
         

        Behavior

        • Returns false if either the bean factory, bean name, or bean class is null.
        • If logging is enabled at trace level, a message will be logged when the bean is not found.
        Parameters:
        beanFactory - the BeanFactory to check for the presence of the bean
        beanName - the name of the bean to check
        beanClass - the class type of the bean to match
        Returns:
        true if the bean is present and matches the specified class; false otherwise
        Throws:
        java.lang.NullPointerException
      • getBeanNames

        @Nonnull
        public static java.lang.String[] getBeanNames​(@Nonnull
                                                      org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                                      @Nonnull
                                                      java.lang.Class<?> beanClass)
        Get bean names of the specified type from the given ListableBeanFactory.

        This method retrieves the names of all beans that match the specified class type. The search is limited to the current bean factory and does not include ancestor factories.

        Example Usage

        
         String[] beanNames = getBeanNames(beanFactory, MyService.class);
         

        Behavior

        • Returns an empty array if no beans of the specified type are found.
        • If logging is enabled at trace level, a message will be logged when no beans are found.
        Parameters:
        beanFactory - the ListableBeanFactory to retrieve bean names from
        beanClass - the class type to match
        Returns:
        an array of bean names matching the specified type; returns an empty array if none are found
      • getBeanNames

        @Nonnull
        public static java.lang.String[] getBeanNames​(@Nonnull
                                                      org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                                      @Nonnull
                                                      java.lang.Class<?> beanClass,
                                                      @Nonnull
                                                      boolean includingAncestors)
        Get Bean Names from ListableBeanFactory by type.

        This method retrieves the names of all beans that match the specified class type. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

        Example Usage

        
         String[] beanNames = getBeanNames(beanFactory, MyService.class, true);
         

        Behavior

        • If no beans are found and logging is enabled at trace level, a log message will be recorded.
        • Returns an empty array if no beans of the specified type are found.
        Parameters:
        beanFactory - the ListableBeanFactory to retrieve bean names from
        beanClass - the class type to match
        includingAncestors - whether to include ancestor bean factories in the search
        Returns:
        an array of bean names matching the specified type; returns an empty array if none are found
      • getBeanNames

        public static java.lang.String[] getBeanNames​(@Nonnull
                                                      org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory,
                                                      @Nonnull
                                                      java.lang.Class<?> beanClass)
        Get bean names of the specified type from the given ConfigurableListableBeanFactory.

        This method retrieves the names of all beans that match the specified class type. The search is limited to the current bean factory and does not include ancestor factories.

        Example Usage

        
         String[] beanNames = getBeanNames(beanFactory, MyService.class);
         

        Behavior

        • Returns an empty array if no beans of the specified type are found.
        • If logging is enabled at trace level, a message will be logged when no beans are found.
        Parameters:
        beanFactory - the ConfigurableListableBeanFactory to retrieve bean names from
        beanClass - the class type to match
        Returns:
        an array of bean names matching the specified type; returns an empty array if none are found
      • getBeanNames

        @Nonnull
        public static java.lang.String[] getBeanNames​(@Nonnull
                                                      org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory,
                                                      @Nonnull
                                                      java.lang.Class<?> beanClass,
                                                      boolean includingAncestors)
        Get bean names of the specified type from the given ConfigurableListableBeanFactory.

        This method retrieves the names of all beans that match the specified class type. If the parameter includingAncestors is set to true, it will also check ancestor bean factories.

        Example Usage

        
         String[] beanNames = getBeanNames(beanFactory, MyService.class, true);
         

        Behavior

        • If no beans are found and logging is enabled at trace level, a log message will be recorded.
        • Returns an empty array if no beans of the specified type are found.
        Parameters:
        beanFactory - the ConfigurableListableBeanFactory to retrieve bean names from
        beanClass - the class type to match
        includingAncestors - whether to include ancestor bean factories in the search
        Returns:
        an array of bean names matching the specified type; returns an empty array if none are found
      • resolveBeanType

        @Nullable
        public static java.lang.Class<?> resolveBeanType​(@Nullable
                                                         java.lang.String beanClassName,
                                                         @Nullable
                                                         java.lang.ClassLoader classLoader)
        Resolve the bean type from the given class name using the specified ClassLoader.

        This method attempts to load the class with the provided name using the given ClassLoader. If the class cannot be found, it returns null. Otherwise, it returns the resolved class. Additionally, if the provided class name is null or empty, the method will return null.

        Example Usage

        
         Class<?> beanType = resolveBeanType("com.example.MyService", classLoader);
         

        Behavior

        • If the class name is null or empty, it returns null.
        • If the class cannot be loaded, it logs a warning and returns null.
        • Otherwise, it returns the resolved class.
        Parameters:
        beanClassName - The fully qualified name of the class to resolve.
        classLoader - The ClassLoader to use for loading the class.
        Returns:
        The resolved class, or null if resolution fails.
      • getOptionalBean

        public static <T> T getOptionalBean​(@Nonnull
                                            org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.Class<T> beanClass,
                                            boolean includingAncestors)
                                     throws org.springframework.beans.BeansException
        Retrieve an optional bean of the specified type from the given ListableBeanFactory.

        This method attempts to find and return a single bean of the specified class. If no beans are found, it returns null. If multiple beans are found, it throws a NoUniqueBeanDefinitionException.

        Example Usage

        
         MyService myService = getOptionalBean(beanFactory, MyService.class);
         

        Behavior

        • If no beans of the specified type are found, returns null.
        • If multiple beans are found, throws a NoUniqueBeanDefinitionException.
        • If logging is enabled at trace level, logs a message when no bean is found.
        • If logging is enabled at error level, logs any exception thrown during bean retrieval.
        Type Parameters:
        T - the type of the bean
        Parameters:
        beanFactory - the ListableBeanFactory to retrieve the bean from
        beanClass - the class of the bean to look up
        includingAncestors - whether to include ancestor bean factories in the search
        Returns:
        the matching bean instance if found; otherwise, returns null
        Throws:
        org.springframework.beans.BeansException - if there is an issue retrieving the bean or multiple beans are found
      • getOptionalBean

        @Nullable
        public static <T> T getOptionalBean​(@Nonnull
                                            org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                            @Nonnull
                                            java.lang.Class<T> beanClass)
                                     throws org.springframework.beans.BeansException
        Retrieve an optional bean of the specified type from the given ListableBeanFactory.

        This method attempts to find and return a single bean of the specified class. If no beans are found, it returns null. If multiple beans are found, it throws a NoUniqueBeanDefinitionException.

        Example Usage

        
         MyService myService = BeanUtils.getOptionalBean(beanFactory, MyService.class);
         

        Behavior

        • If no beans of the specified type are found, returns null.
        • If multiple beans are found, throws a NoUniqueBeanDefinitionException.
        • If logging is enabled at trace level, logs a message when no bean is found.
        • If logging is enabled at error level, logs any exception thrown during bean retrieval.
        Type Parameters:
        T - the type of the bean
        Parameters:
        beanFactory - the ListableBeanFactory to retrieve the bean from
        beanClass - the class of the bean to look up
        Returns:
        the matching bean instance if found; otherwise, returns null
        Throws:
        org.springframework.beans.BeansException - if there is an issue retrieving the bean or multiple beans are found
      • getBeanIfAvailable

        @Nullable
        public static <T> T getBeanIfAvailable​(@Nonnull
                                               org.springframework.beans.factory.BeanFactory beanFactory,
                                               @Nonnull
                                               java.lang.String beanName,
                                               @Nonnull
                                               java.lang.Class<T> beanType)
                                        throws org.springframework.beans.BeansException
        Retrieve the bean with the specified name and type from the given BeanFactory if it exists.

        This method checks whether a bean with the specified name and type exists in the bean factory. If it does, the bean is returned; otherwise, null is returned.

        Example Usage

        
         MyService myService = getBeanIfAvailable(beanFactory, "myService", MyService.class);
         

        Behavior

        • If the bean is not present, returns null.
        • If logging is enabled at trace level, logs a message when the bean is not found.
        Type Parameters:
        T - the type of the bean
        Parameters:
        beanFactory - the BeanFactory to retrieve the bean from
        beanName - the name of the bean to look up
        beanType - the class type of the bean to match
        Returns:
        the matching bean instance if found; otherwise, returns null
        Throws:
        org.springframework.beans.BeansException - if there is an issue retrieving the bean
      • getSortedBeans

        @Nonnull
        public static <T> java.util.List<T> getSortedBeans​(@Nonnull
                                                           org.springframework.beans.factory.BeanFactory beanFactory,
                                                           @Nonnull
                                                           java.lang.Class<T> type)
        Retrieve a list of beans of the specified type from the given BeanFactory, sorted based on their order.

        If the provided BeanFactory is an instance of ListableBeanFactory, it delegates to getSortedBeans(ListableBeanFactory, Class) for retrieving and sorting the beans. Otherwise, it returns an empty list.

        Example Usage

        
         List<MyService> myServices = getSortedBeans(beanFactory, MyService.class);
         

        Behavior

        • Returns an unmodifiable list of beans sorted by their order.
        • If no beans are found, returns an empty list.
        Type Parameters:
        T - the type of the beans
        Parameters:
        beanFactory - the BeanFactory to retrieve the beans from
        type - the class type of the beans to look up
        Returns:
        a sorted list of beans matching the specified type; returns an empty list if none are found
      • getSortedBeans

        @Nonnull
        public static <T> java.util.List<T> getSortedBeans​(@Nonnull
                                                           org.springframework.beans.factory.ListableBeanFactory beanFactory,
                                                           @Nonnull
                                                           java.lang.Class<T> type)
        Retrieve all beans of the specified type from the given ListableBeanFactory, sorted based on their order.

        This method retrieves a map of bean names to bean instances matching the specified type and converts it into a list. The list is then sorted using AnnotationAwareOrderComparator based on the beans' order.

        Example Usage

        
         List<MyService> myServices = getSortedBeans(beanFactory, MyService.class);
         

        Behavior

        • Returns an unmodifiable list of beans sorted by their order.
        • If no beans are found, returns an empty list.
        Type Parameters:
        T - the type of the beans
        Parameters:
        beanFactory - the ListableBeanFactory to retrieve the beans from
        type - the class type of the beans to look up
        Returns:
        a sorted list of beans matching the specified type; returns an empty list if none are found
      • invokeBeanInterfaces

        public static void invokeBeanInterfaces​(@Nullable
                                                java.lang.Object bean,
                                                @Nullable
                                                org.springframework.context.ApplicationContext context)
        Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.

        This method delegates to invokeBeanInterfaces(Object, ConfigurableApplicationContext) after converting the provided ApplicationContext into a ConfigurableApplicationContext, allowing further processing if needed.

        Invoke Spring Bean interfaces in order:

        • BeanNameAware
        • BeanClassLoaderAware
        • BeanFactoryAware
        • EnvironmentAware
        • EmbeddedValueResolverAware
        • ResourceLoaderAware
        • ApplicationEventPublisherAware
        • MessageSourceAware
        • ApplicationStartupAware (Spring Framework 5.3+)
        • ApplicationContextAware
        • InitializingBean

        Example Usage

        
         MyBean myBean = new MyBean();
         ApplicationContext context = ...; // Obtain or create an ApplicationContext
         invokeBeanInterfaces(myBean, context);
         

        Behavior

        • If the provided bean implements any of the supported lifecycle interfaces, their respective methods will be invoked.
        • The order of invocation follows the standard Spring lifecycle contract.
        Parameters:
        bean - The bean instance whose lifecycle interfaces should be invoked.
        context - The application context associated with the bean, may be null.
      • invokeBeanInterfaces

        public static void invokeBeanInterfaces​(@Nullable
                                                java.lang.Object bean,
                                                @Nullable
                                                org.springframework.context.ConfigurableApplicationContext context)
        Invokes the standard Spring bean lifecycle interfaces in a specific order for the given bean and application context.

        This method ensures that all relevant lifecycle callbacks are executed in the correct sequence, starting with the Aware interface methods followed by the InitializingBean.afterPropertiesSet() method.

        Example Usage

        
         MyBean myBean = new MyBean();
         ConfigurableApplicationContext context = ...; // Obtain or create a ConfigurableApplicationContext
         BeanUtils.invokeBeanInterfaces(myBean, context);
         

        Lifecycle Invocation Order

        1. BeanNameAware
        2. BeanClassLoaderAware
        3. BeanFactoryAware
        4. EnvironmentAware
        5. EmbeddedValueResolverAware
        6. ResourceLoaderAware
        7. ApplicationEventPublisherAware
        8. MessageSourceAware
        9. ApplicationStartupAware (Spring Framework 5.3+)
        10. ApplicationContextAware
        11. InitializingBean
        Parameters:
        bean - the bean instance whose lifecycle interfaces should be invoked
        context - the application context associated with the bean, may be null
        See Also:
        invokeBeanInterfaces(Object, ApplicationContext), invokeAwareInterfaces(Object, ApplicationContext), invokeInitializingBean(Object)
      • invokeInitializingBean

        public static void invokeInitializingBean​(@Nullable
                                                  java.lang.Object bean)
                                           throws java.lang.Exception
        Invoke the InitializingBean.afterPropertiesSet() method if the given bean implements the interface.

        This method is typically used to trigger post-processing logic after all bean properties have been set.

        Example Usage

        
         MyInitializingBean myBean = new MyInitializingBean();
         BeanUtils.invokeInitializingBean(myBean);
         

        Behavior

        • If the provided bean does not implement InitializingBean, this method has no effect.
        • If an exception occurs during execution of InitializingBean.afterPropertiesSet(), it will be propagated.
        Parameters:
        bean - the bean object to invoke the initialization method on, may be null
        Throws:
        java.lang.Exception - if an error occurs while invoking the initialization method
      • invokeAwareInterfaces

        public static void invokeAwareInterfaces​(@Nullable
                                                 java.lang.Object bean,
                                                 @Nullable
                                                 org.springframework.beans.factory.BeanFactory beanFactory)
        Invokes the Spring Aware interfaces implemented by the given bean if applicable.

        This method supports a standard lifecycle invocation order for common Spring Aware interfaces:

        • BeanNameAware
        • BeanClassLoaderAware
        • BeanFactoryAware

        If the provided BeanFactory is also an instance of ApplicationContext, additional context-specific Aware interfaces will be invoked in sequence:

        • EnvironmentAware
        • EmbeddedValueResolverAware
        • ResourceLoaderAware
        • ApplicationEventPublisherAware
        • MessageSourceAware
        • ApplicationStartupAware (Spring Framework 5.3+)
        • ApplicationContextAware

        Example Usage

        
         MyBean myBean = new MyBean();
         ConfigurableBeanFactory beanFactory = context.getBeanFactory();
         BeanUtils.invokeAwareInterfaces(myBean, beanFactory);
         

        Behavior

        • No action is taken if the bean is null.
        • Only relevant Aware interfaces are invoked based on the bean's implementation.
        Parameters:
        bean - the bean object that may implement one or more Aware interfaces
        beanFactory - the factory used to configure the bean; may be null
      • invokeAwareInterfaces

        public static void invokeAwareInterfaces​(@Nullable
                                                 java.lang.Object bean,
                                                 @Nullable
                                                 org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
        Invokes the Spring Aware interfaces implemented by the given bean if applicable.

        This method ensures that all relevant lifecycle callbacks are executed in the correct sequence, starting with the basic BeanNameAware, BeanClassLoaderAware, and BeanFactoryAware interfaces. If the provided bean factory is also an application context, additional context-specific aware interfaces will be invoked, including: 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

        Example Usage

        
         MyBean myBean = new MyBean();
         ConfigurableBeanFactory beanFactory = context.getBeanFactory();
         BeanUtils.invokeAwareInterfaces(myBean, beanFactory);
         

        Behavior

        • No action is taken if the bean is null.
        • If the bean implements any of the supported interfaces, their respective methods will be invoked.
        • If the bean factory is a context, more specific aware interfaces will be processed.
        Parameters:
        bean - the bean instance whose lifecycle interfaces should be invoked
        beanFactory - the associated bean factory, may be null
      • invokeAwareInterfaces

        public static void invokeAwareInterfaces​(@Nullable
                                                 java.lang.Object bean,
                                                 @Nullable
                                                 org.springframework.beans.factory.BeanFactory beanFactory,
                                                 @Nullable
                                                 org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)
        Invokes the Spring Aware interfaces implemented by the given bean if applicable.

        This method supports a standard lifecycle invocation order for common Spring Aware interfaces:

        • BeanNameAware
        • BeanClassLoaderAware
        • BeanFactoryAware

        If the provided BeanFactory is also an instance of ApplicationContext, additional context-specific Aware interfaces will be invoked in sequence:

        • EnvironmentAware
        • EmbeddedValueResolverAware
        • ResourceLoaderAware
        • ApplicationEventPublisherAware
        • MessageSourceAware
        • ApplicationStartupAware (Spring Framework 5.3+)
        • ApplicationContextAware

        Example Usage

        
         MyBean myBean = new MyBean();
         ConfigurableBeanFactory beanFactory = context.getBeanFactory();
         BeanUtils.invokeAwareInterfaces(myBean, beanFactory, beanFactory);
         

        Behavior

        • No action is taken if the bean is null.
        • Only relevant Aware interfaces are invoked based on the bean's implementation.
        Parameters:
        bean - the bean object that may implement one or more Aware interfaces
        beanFactory - the factory used to configure the bean; may be null
        configurableBeanFactory - the configurable version of the bean factory; may be null
      • findPrimaryConstructor

        public static <T> java.lang.reflect.Constructor<T> findPrimaryConstructor​(@Nonnull
                                                                                  java.lang.Class<T> clazz)
        Find the primary constructor of the specified class.

        For Kotlin classes, this method returns the Java constructor corresponding to the Kotlin primary constructor, as defined in the Kotlin specification. For non-Kotlin classes, this method typically returns null.

        Example Usage

        
         Constructor<MyClass> constructor = findPrimaryConstructor(MyClass.class);
         if (constructor != null) {
             MyClass instance = constructor.newInstance();
         }
         

        Behavior

        • Returns the primary constructor if available.
        • Returns null if no primary constructor is found or if the class is not a Kotlin class.
        • If an error occurs during invocation, it is logged at warn level and null is returned.
        Type Parameters:
        T - the type of the class
        Parameters:
        clazz - the class to check for a primary constructor, must not be null
        Returns:
        the primary constructor of the class, or null if none is found
        Since:
        Spring Framework 5.0
        See Also:
        BeanUtils.findPrimaryConstructor(Class)
      • invokeBeanNameAware

        public static void invokeBeanNameAware​(@Nullable
                                               java.lang.Object bean,
                                               @Nullable
                                               java.lang.String beanName)
        Invokes the BeanNameAware.setBeanName(String) method if the given bean implements the interface.

        This method is typically used during bean initialization to set the bean's name as defined in the Spring configuration or generated by the container. If the provided bean does not implement BeanNameAware, this method has no effect.

        Example Usage

        
         MyBean myBean = new MyBean();
         BeanUtils.invokeBeanNameAware(myBean, "myBean");
         

        Behavior

        • If the bean implements BeanNameAware, the method sets the bean's name using the provided value.
        • If the bean is null or does not implement the interface, it returns silently without any action.
        Parameters:
        bean - the bean object that may implement BeanNameAware
        beanName - the name to be assigned to the bean, may be null
      • invokeAwareInterfaces

        public static void invokeAwareInterfaces​(@Nullable
                                                 java.lang.Object bean,
                                                 @Nullable
                                                 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

        Example Usage

        
         MyBean myBean = new MyBean();
         ConfigurableApplicationContext context = ...; // Obtain or create a ConfigurableApplicationContext
         BeanUtils.invokeAwareInterfaces(myBean, context);
         

        Behavior

        • No action is taken if the bean is null.
        • If the bean implements any of the supported interfaces, their respective methods will be invoked.
        • If the bean factory is a context, more specific aware interfaces will be processed.
        Parameters:
        bean - the bean instance whose lifecycle interfaces should be invoked
        context - the application context associated with the bean, may be null