Class BeanUtils
- All Implemented Interfaces:
io.microsphere.util.Utils
- Since:
- 1.0.0
- Author:
- Mercy
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
getBeanIfAvailable
(org.springframework.beans.factory.BeanFactory beanFactory, String beanName, Class<T> beanType) Retrieve the bean with the specified name and type from the givenBeanFactory
if it exists.static String[]
getBeanNames
(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Class<?> beanClass) Get bean names of the specified type from the givenConfigurableListableBeanFactory
.static String[]
getBeanNames
(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors) Get bean names of the specified type from the givenConfigurableListableBeanFactory
.static String[]
getBeanNames
(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass) Get bean names of the specified type from the givenListableBeanFactory
.static String[]
getBeanNames
(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<?> beanClass, boolean includingAncestors) Get Bean Names fromListableBeanFactory
by type.static <T> T
getOptionalBean
(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> beanClass) Retrieve an optional bean of the specified type from the givenListableBeanFactory
.static <T> T
getOptionalBean
(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> beanClass, boolean includingAncestors) Retrieve an optional bean of the specified type from the givenListableBeanFactory
.static <T> List<T>
getSortedBeans
(org.springframework.beans.factory.BeanFactory beanFactory, Class<T> type) Retrieve a list of beans of the specified type from the givenBeanFactory
, sorted based on their order.static <T> List<T>
getSortedBeans
(org.springframework.beans.factory.ListableBeanFactory beanFactory, Class<T> type) Retrieve all beans of the specified type from the givenListableBeanFactory
, sorted based on their order.static void
invokeAwareInterfaces
(Object bean, org.springframework.beans.factory.BeanFactory beanFactory) Invokes the SpringAware
interfaces implemented by the given bean if applicable.static void
invokeAwareInterfaces
(Object bean, org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory) Invokes the SpringAware
interfaces implemented by the given bean if applicable.static void
invokeAwareInterfaces
(Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory) Invokes the SpringAware
interfaces implemented by the given bean if applicable.static void
invokeAwareInterfaces
(Object bean, org.springframework.context.ConfigurableApplicationContext context) InvokeAware
interfaces if the given bean implementsstatic void
invokeBeanInterfaces
(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
(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
(Object bean, String beanName) Invokes theBeanNameAware.setBeanName(String)
method if the given bean implements the interface.static void
invokeInitializingBean
(Object bean) Invoke theInitializingBean.afterPropertiesSet()
method if the given bean implements the interface.static boolean
isBeanPresent
(org.springframework.beans.factory.BeanFactory beanFactory, String beanName, Class<?> beanClass) Check if a bean with the specified name and class is present in the givenBeanFactory
.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) Check if a bean of the specified class is present in the givenListableBeanFactory
.static boolean
isBeanPresent
(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanClassName) Check if a bean with the specified class name is present in the givenListableBeanFactory
.static boolean
isBeanPresent
(org.springframework.beans.factory.ListableBeanFactory beanFactory, String beanClassName, boolean includingAncestors) Check if a bean with the specified class name is present in the givenListableBeanFactory
.static Class<?>
resolveBeanType
(String beanClassName, ClassLoader classLoader) Resolve the bean type from the given class name using the specified ClassLoader.
-
Method Details
-
isBeanPresent
public static boolean isBeanPresent(@Nonnull org.springframework.beans.factory.ListableBeanFactory beanFactory, @Nonnull Class<?> beanClass) Is Bean Present or not?- Parameters:
beanFactory
-ListableBeanFactory
beanClass
- TheClass
of Bean- Returns:
- If present , return
true
, orfalse
-
isBeanPresent
public static boolean isBeanPresent(@Nonnull org.springframework.beans.factory.ListableBeanFactory beanFactory, @Nonnull 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
includingAncestors
is 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
- theListableBeanFactory
to 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 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
- theListableBeanFactory
to 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 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
includingAncestors
is 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
- theListableBeanFactory
to 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 String beanName, @Nonnull Class<?> beanClass) throws NullPointerException Check 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
- theBeanFactory
to 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:
NullPointerException
-
getBeanNames
@Nonnull public static String[] getBeanNames(@Nonnull org.springframework.beans.factory.ListableBeanFactory beanFactory, @Nonnull 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
- theListableBeanFactory
to 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 String[] getBeanNames(@Nonnull org.springframework.beans.factory.ListableBeanFactory beanFactory, @Nonnull Class<?> beanClass, @Nonnull boolean includingAncestors) Get Bean Names fromListableBeanFactory
by type.This method retrieves the names of all beans that match the specified class type. If the parameter
includingAncestors
is 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
- theListableBeanFactory
to 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 String[] getBeanNames(@Nonnull org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, @Nonnull 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
- theConfigurableListableBeanFactory
to 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 String[] getBeanNames(@Nonnull org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, @Nonnull 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
includingAncestors
is 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
- theConfigurableListableBeanFactory
to 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 Class<?> resolveBeanType(@Nullable String beanClassName, @Nullable 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 Class<T> beanClass, boolean includingAncestors) throws org.springframework.beans.BeansException Retrieve 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
- theListableBeanFactory
to 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 Class<T> beanClass) throws org.springframework.beans.BeansException Retrieve 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
- theListableBeanFactory
to 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 String beanName, @Nonnull Class<T> beanType) throws org.springframework.beans.BeansException Retrieve the bean with the specified name and type from the givenBeanFactory
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
- theBeanFactory
to 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> List<T> getSortedBeans(@Nonnull org.springframework.beans.factory.BeanFactory beanFactory, @Nonnull Class<T> type) Retrieve a list of beans of the specified type from the givenBeanFactory
, sorted based on their order.If the provided
BeanFactory
is 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
- theBeanFactory
to 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> List<T> getSortedBeans(@Nonnull org.springframework.beans.factory.ListableBeanFactory beanFactory, @Nonnull 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
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
- theListableBeanFactory
to 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 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 providedApplicationContext
into aConfigurableApplicationContext
, 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 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 theInitializingBean.afterPropertiesSet()
method.Example Usage
MyBean myBean = new MyBean(); ConfigurableApplicationContext context = ...; // Obtain or create a ConfigurableApplicationContext BeanUtils.invokeBeanInterfaces(myBean, context);
Lifecycle Invocation Order
BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
(Spring Framework 5.3+)ApplicationContextAware
InitializingBean
- 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
Invoke 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:
Exception
- if an error occurs while invoking the initialization method
- If the provided bean does not implement
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(@Nullable Object bean, @Nullable org.springframework.beans.factory.BeanFactory beanFactory) Invokes the SpringAware
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 ofApplicationContext
, 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 moreAware
interfacesbeanFactory
- the factory used to configure the bean; may be null
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(@Nullable Object bean, @Nullable org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory) Invokes the SpringAware
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
, andBeanFactoryAware
interfaces. If the provided bean factory is also an application context, additional context-specific aware interfaces will be invoked, including: Invoke theAware
interfaces in order :BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
if the argument
beanFactory is an instance ofApplicationContext
, the moreAware
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 invokedbeanFactory
- the associated bean factory, may be null
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(@Nullable Object bean, @Nullable org.springframework.beans.factory.BeanFactory beanFactory, @Nullable org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory) Invokes the SpringAware
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 ofApplicationContext
, 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 moreAware
interfacesbeanFactory
- the factory used to configure the bean; may be nullconfigurableBeanFactory
- the configurable version of the bean factory; may be null
-
invokeBeanNameAware
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 implementBeanNameAware
beanName
- the name to be assigned to the bean, may be null
- If the bean implements
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(@Nullable Object bean, @Nullable org.springframework.context.ConfigurableApplicationContext context) InvokeAware
interfaces if the given bean implementsCurrent 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 invokedcontext
- the application context associated with the bean, may be null
-