Class BeanUtils
- java.lang.Object
-
- io.microsphere.spring.beans.BeanUtils
-
- All Implemented Interfaces:
io.microsphere.util.Utils
public abstract class BeanUtils extends java.lang.Object implements io.microsphere.util.UtilsBean 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> TgetBeanIfAvailable(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 givenBeanFactoryif 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 givenConfigurableListableBeanFactory.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 givenConfigurableListableBeanFactory.static java.lang.String[]getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass)Get bean names of the specified type from the givenListableBeanFactory.static java.lang.String[]getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)Get Bean Names fromListableBeanFactoryby type.static <T> TgetOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass)Retrieve an optional bean of the specified type from the givenListableBeanFactory.static <T> TgetOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass, boolean includingAncestors)Retrieve an optional bean of the specified type from the givenListableBeanFactory.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 givenBeanFactory, 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 givenListableBeanFactory, sorted based on their order.static voidinvokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory)Invokes the SpringAwareinterfaces implemented by the given bean if applicable.static voidinvokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)Invokes the SpringAwareinterfaces implemented by the given bean if applicable.static voidinvokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)Invokes the SpringAwareinterfaces implemented by the given bean if applicable.static voidinvokeAwareInterfaces(java.lang.Object bean, org.springframework.context.ConfigurableApplicationContext context)InvokeAwareinterfaces if the given bean implementsstatic voidinvokeBeanInterfaces(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 voidinvokeBeanInterfaces(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 voidinvokeBeanNameAware(java.lang.Object bean, java.lang.String beanName)Invokes theBeanNameAware.setBeanName(String)method if the given bean implements the interface.static voidinvokeInitializingBean(java.lang.Object bean)Invoke theInitializingBean.afterPropertiesSet()method if the given bean implements the interface.static booleanisBeanPresent(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 givenBeanFactory.static booleanisBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass)Is Bean Present or not?static booleanisBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)Check if a bean of the specified class is present in the givenListableBeanFactory.static booleanisBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName)Check if a bean with the specified class name is present in the givenListableBeanFactory.static booleanisBeanPresent(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 givenListableBeanFactory.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.
-
-
-
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-ListableBeanFactorybeanClass- TheClassof Bean- Returns:
- If present , return
true, orfalse
-
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 givenListableBeanFactory.This method checks whether there is at least one bean of the specified type in the bean factory. If the parameter
includingAncestorsis set totrue, 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- theListableBeanFactoryto search for beansbeanClass- the class of the bean to check presence forincludingAncestors- 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 givenListableBeanFactory.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- theListableBeanFactoryto search for beansbeanClassName- 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 givenListableBeanFactory.This method checks whether there is at least one bean with the specified class name in the bean factory. If the parameter
includingAncestorsis set totrue, 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- theListableBeanFactoryto search for beansbeanClassName- the fully qualified name of the class to check presence forincludingAncestors- 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.NullPointerExceptionCheck if a bean with the specified name and class is present in the givenBeanFactory.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- theBeanFactoryto check for the presence of the beanbeanName- the name of the bean to checkbeanClass- 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 givenListableBeanFactory.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- theListableBeanFactoryto retrieve bean names frombeanClass- 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 fromListableBeanFactoryby type.This method retrieves the names of all beans that match the specified class type. If the parameter
includingAncestorsis set totrue, 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- theListableBeanFactoryto retrieve bean names frombeanClass- the class type to matchincludingAncestors- 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 givenConfigurableListableBeanFactory.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- theConfigurableListableBeanFactoryto retrieve bean names frombeanClass- 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 givenConfigurableListableBeanFactory.This method retrieves the names of all beans that match the specified class type. If the parameter
includingAncestorsis set totrue, 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- theConfigurableListableBeanFactoryto retrieve bean names frombeanClass- the class type to matchincludingAncestors- 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.BeansExceptionRetrieve an optional bean of the specified type from the givenListableBeanFactory.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 aNoUniqueBeanDefinitionException.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- theListableBeanFactoryto retrieve the bean frombeanClass- the class of the bean to look upincludingAncestors- 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
- If no beans of the specified type are found, returns
-
getOptionalBean
@Nullable public static <T> T getOptionalBean(@Nonnull org.springframework.beans.factory.ListableBeanFactory beanFactory, @Nonnull java.lang.Class<T> beanClass) throws org.springframework.beans.BeansExceptionRetrieve an optional bean of the specified type from the givenListableBeanFactory.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 aNoUniqueBeanDefinitionException.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- theListableBeanFactoryto retrieve the bean frombeanClass- 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
- If no beans of the specified type are found, returns
-
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.BeansExceptionRetrieve the bean with the specified name and type from the givenBeanFactoryif 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,
nullis 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- theBeanFactoryto retrieve the bean frombeanName- the name of the bean to look upbeanType- 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
- If the bean is not present, returns
-
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 givenBeanFactory, sorted based on their order.If the provided
BeanFactoryis an instance ofListableBeanFactory, it delegates togetSortedBeans(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- theBeanFactoryto retrieve the beans fromtype- 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 givenListableBeanFactory, 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
AnnotationAwareOrderComparatorbased 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- theListableBeanFactoryto retrieve the beans fromtype- 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 providedApplicationContextinto aConfigurableApplicationContext, allowing further processing if needed.Invoke Spring Bean interfaces in order:
BeanNameAwareBeanClassLoaderAwareBeanFactoryAwareEnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationStartupAware(Spring Framework 5.3+)ApplicationContextAwareInitializingBean
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
Awareinterface methods followed by theInitializingBean.afterPropertiesSet()method.Example Usage
MyBean myBean = new MyBean(); ConfigurableApplicationContext context = ...; // Obtain or create a ConfigurableApplicationContext BeanUtils.invokeBeanInterfaces(myBean, context);Lifecycle Invocation Order
BeanNameAwareBeanClassLoaderAwareBeanFactoryAwareEnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationStartupAware(Spring Framework 5.3+)ApplicationContextAwareInitializingBean
- Parameters:
bean- the bean instance whose lifecycle interfaces should be invokedcontext- 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.ExceptionInvoke theInitializingBean.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
- If the provided bean does not implement
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(@Nullable java.lang.Object bean, @Nullable org.springframework.beans.factory.BeanFactory beanFactory)Invokes the SpringAwareinterfaces implemented by the given bean if applicable.This method supports a standard lifecycle invocation order for common Spring Aware interfaces:
BeanNameAwareBeanClassLoaderAwareBeanFactoryAware
If the provided
BeanFactoryis also an instance ofApplicationContext, additional context-specific Aware interfaces will be invoked in sequence:EnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationStartupAware(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 moreAwareinterfacesbeanFactory- 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 SpringAwareinterfaces 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, andBeanFactoryAwareinterfaces. If the provided bean factory is also an application context, additional context-specific aware interfaces will be invoked, including: Invoke theAwareinterfaces in order :BeanNameAwareBeanClassLoaderAwareBeanFactoryAware
if the argument
beanFactory is an instance ofApplicationContext, the moreAwareinterfaces will be involved :EnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationStartupAware(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 invokedbeanFactory- 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 SpringAwareinterfaces implemented by the given bean if applicable.This method supports a standard lifecycle invocation order for common Spring Aware interfaces:
BeanNameAwareBeanClassLoaderAwareBeanFactoryAware
If the provided
BeanFactoryis also an instance ofApplicationContext, additional context-specific Aware interfaces will be invoked in sequence:EnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationStartupAware(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 moreAwareinterfacesbeanFactory- the factory used to configure the bean; may be nullconfigurableBeanFactory- 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
nullif 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 benull- Returns:
- the primary constructor of the class, or
nullif 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 theBeanNameAware.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 implementBeanNameAwarebeanName- the name to be assigned to the bean, may be null
- If the bean implements
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(@Nullable java.lang.Object bean, @Nullable org.springframework.context.ConfigurableApplicationContext context)InvokeAwareinterfaces if the given bean implementsCurrent implementation keeps the order of invocation
Aware interfaces:BeanNameAwareBeanClassLoaderAwareBeanFactoryAwareEnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationStartupAware(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 invokedcontext- the application context associated with the bean, may be null
-
-