Package io.microsphere.spring.beans
Class BeanUtils
- java.lang.Object
-
- io.microsphere.util.BaseUtils
-
- io.microsphere.spring.beans.BeanUtils
-
public abstract class BeanUtils extends io.microsphere.util.BaseUtils
Bean Utilities Class- Since:
- 2017.01.13
- Author:
- Mercy
-
-
Constructor Summary
Constructors Constructor Description BeanUtils()
-
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)
Return the primary constructor of the provided class.static <T> T
getBeanIfAvailable(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName, java.lang.Class<T> beanType)
Get the bean via the specified bean name and type if availablestatic java.lang.String[]
getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
Get Bean Names fromListableBeanFactory
by type.static java.lang.String[]
getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
Get Bean Names fromListableBeanFactory
by type.static java.lang.String[]
getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
Get Bean Names fromListableBeanFactory
by type.static java.lang.String[]
getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
Get Bean Names fromListableBeanFactory
by type.static <T> T
getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass)
Get Optional Bean byClass
.static <T> T
getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass, boolean includingAncestors)
Get Optional Bean byClass
including ancestors(BeanFactory).static <T> java.util.List<T>
getSortedBeans(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.Class<T> type)
Get all sorted Beans ofBeanFactory
in specified bean type.static <T> java.util.List<T>
getSortedBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> type)
Get all sorted Beans ofListableBeanFactory
in specified bean type.static void
invokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory)
Invoke theAware
interfaces in order :BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
static void
invokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory, org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)
Invoke theAware
interfaces in order :BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
static void
invokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
Invoke theAware
interfaces in order :BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
static void
invokeAwareInterfaces(java.lang.Object bean, org.springframework.context.ConfigurableApplicationContext context)
InvokeAware
interfaces if the given bean implementsstatic void
invokeBeanInterfaces(java.lang.Object bean, org.springframework.context.ApplicationContext context)
Invoke Spring Bean interfaces in order:BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
(Spring Framework 5.3+)ApplicationContextAware
InitializingBean
static void
invokeBeanInterfaces(java.lang.Object bean, org.springframework.context.ConfigurableApplicationContext context)
Invoke Spring Bean interfaces in order:BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
(Spring Framework 5.3+)ApplicationContextAware
InitializingBean
static void
invokeBeanNameAware(java.lang.Object bean, java.lang.String beanName)
static void
invokeInitializingBean(java.lang.Object bean)
static boolean
isBeanPresent(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName, java.lang.Class<?> beanClass)
Is Bean Present or not by the specified name and classstatic 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)
Is Bean Present or not?static boolean
isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName)
Is Bean Present or not?static boolean
isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName, boolean includingAncestors)
Is Bean Present or not?static java.lang.Class<?>
resolveBeanType(java.lang.String beanClassName, java.lang.ClassLoader classLoader)
Resolve Bean Type
-
-
-
Method Detail
-
isBeanPresent
public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.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(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
Is Bean Present or not?- Parameters:
beanFactory
-ListableBeanFactory
beanClass
- TheClass
of BeanincludingAncestors
- including ancestors or not- Returns:
- If present , return
true
, orfalse
-
isBeanPresent
public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName)
Is Bean Present or not?- Parameters:
beanFactory
-ListableBeanFactory
beanClassName
- The name ofClass
of Bean- Returns:
- If present , return
true
, orfalse
-
isBeanPresent
public static boolean isBeanPresent(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.String beanClassName, boolean includingAncestors)
Is Bean Present or not?- Parameters:
beanFactory
-ListableBeanFactory
beanClassName
- The name ofClass
of BeanincludingAncestors
- including ancestors or not- Returns:
- If present , return
true
, orfalse
-
isBeanPresent
public static boolean isBeanPresent(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName, java.lang.Class<?> beanClass) throws java.lang.NullPointerException
Is Bean Present or not by the specified name and class- Parameters:
beanFactory
-BeanFactory
beanName
- The bean namebeanClass
- The bean class- Returns:
- If present , return
true
, orfalse
- Throws:
java.lang.NullPointerException
-
getBeanNames
public static java.lang.String[] getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
Get Bean Names fromListableBeanFactory
by type.- Parameters:
beanFactory
-ListableBeanFactory
beanClass
- TheClass
of Bean- Returns:
- If found , return the array of Bean Names , or empty array.
-
getBeanNames
public static java.lang.String[] getBeanNames(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
Get Bean Names fromListableBeanFactory
by type.- Parameters:
beanFactory
-ListableBeanFactory
beanClass
- TheClass
of BeanincludingAncestors
- including ancestors or not- Returns:
- If found , return the array of Bean Names , or empty array.
-
getBeanNames
public static java.lang.String[] getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, java.lang.Class<?> beanClass)
Get Bean Names fromListableBeanFactory
by type.- Parameters:
beanFactory
-ConfigurableListableBeanFactory
beanClass
- TheClass
of Bean- Returns:
- If found , return the array of Bean Names , or empty array.
-
getBeanNames
public static java.lang.String[] getBeanNames(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory, java.lang.Class<?> beanClass, boolean includingAncestors)
Get Bean Names fromListableBeanFactory
by type.- Parameters:
beanFactory
-ConfigurableListableBeanFactory
beanClass
- TheClass
of BeanincludingAncestors
- including ancestors or not- Returns:
- If found , return the array of Bean Names , or empty array.
-
resolveBeanType
public static java.lang.Class<?> resolveBeanType(java.lang.String beanClassName, java.lang.ClassLoader classLoader)
Resolve Bean Type- Parameters:
beanClassName
- the class name of BeanclassLoader
-ClassLoader
- Returns:
- Bean type if can be resolved , or return
null
.
-
getOptionalBean
public static <T> T getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass, boolean includingAncestors) throws org.springframework.beans.BeansException
Get Optional Bean byClass
including ancestors(BeanFactory).- Type Parameters:
T
- TheClass
of Bean- Parameters:
beanFactory
-ListableBeanFactory
beanClass
- TheClass
of BeanincludingAncestors
- including ancestors or not- Returns:
- Bean object if found , or return
null
. - Throws:
org.springframework.beans.factory.NoUniqueBeanDefinitionException
- if more than one bean of the given type was foundorg.springframework.beans.BeansException
- See Also:
BeanFactoryUtils.beanOfTypeIncludingAncestors(ListableBeanFactory, Class)
-
getOptionalBean
public static <T> T getOptionalBean(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> beanClass) throws org.springframework.beans.BeansException
Get Optional Bean byClass
.- Type Parameters:
T
- TheClass
of Bean- Parameters:
beanFactory
-ListableBeanFactory
beanClass
- TheClass
of Bean- Returns:
- Bean object if found , or return
null
. - Throws:
org.springframework.beans.factory.NoUniqueBeanDefinitionException
- if more than one bean of the given type was foundorg.springframework.beans.BeansException
-
getBeanIfAvailable
public static <T> T getBeanIfAvailable(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.String beanName, java.lang.Class<T> beanType) throws org.springframework.beans.BeansException
Get the bean via the specified bean name and type if available- Type Parameters:
T
- the bean type- Parameters:
beanFactory
-BeanFactory
beanName
- the bean namebeanType
- the class of bean- Returns:
- the bean if available, or
null
- Throws:
org.springframework.beans.BeansException
- in case of creation errors
-
getSortedBeans
public static <T> java.util.List<T> getSortedBeans(org.springframework.beans.factory.BeanFactory beanFactory, java.lang.Class<T> type)
Get all sorted Beans ofBeanFactory
in specified bean type.- Type Parameters:
T
- bean type- Parameters:
beanFactory
-BeanFactory
type
- bean type- Returns:
- all sorted Beans
-
getSortedBeans
public static <T> java.util.List<T> getSortedBeans(org.springframework.beans.factory.ListableBeanFactory beanFactory, java.lang.Class<T> type)
Get all sorted Beans ofListableBeanFactory
in specified bean type.- Type Parameters:
T
- bean type- Parameters:
beanFactory
-ListableBeanFactory
type
- bean type- Returns:
- all sorted Beans
-
invokeBeanInterfaces
public static void invokeBeanInterfaces(java.lang.Object bean, org.springframework.context.ApplicationContext context)
Invoke Spring Bean interfaces in order:BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
(Spring Framework 5.3+)ApplicationContextAware
InitializingBean
- Parameters:
bean
- the beancontext
-ApplicationContext
-
invokeBeanInterfaces
public static void invokeBeanInterfaces(java.lang.Object bean, org.springframework.context.ConfigurableApplicationContext context)
Invoke Spring Bean interfaces in order:BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
EnvironmentAware
EmbeddedValueResolverAware
ResourceLoaderAware
ApplicationEventPublisherAware
MessageSourceAware
ApplicationStartupAware
(Spring Framework 5.3+)ApplicationContextAware
InitializingBean
- Parameters:
bean
- the beancontext
-ConfigurableApplicationContext
- See Also:
invokeBeanInterfaces(Object, ApplicationContext)
,invokeAwareInterfaces(Object, ApplicationContext)
,invokeInitializingBean(Object)
-
invokeInitializingBean
public static void invokeInitializingBean(java.lang.Object bean) throws java.lang.Exception
- Throws:
java.lang.Exception
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory)
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
- Parameters:
bean
- the beanbeanFactory
-BeanFactory
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.config.ConfigurableBeanFactory beanFactory)
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
- Parameters:
bean
- the beanbeanFactory
-ConfigurableBeanFactory
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(java.lang.Object bean, org.springframework.beans.factory.BeanFactory beanFactory, @Nullable org.springframework.beans.factory.config.ConfigurableBeanFactory configurableBeanFactory)
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
- Parameters:
bean
-beanFactory
-configurableBeanFactory
-
-
findPrimaryConstructor
public static <T> java.lang.reflect.Constructor<T> findPrimaryConstructor(java.lang.Class<T> clazz)
Return the primary constructor of the provided class. For Kotlin classes, this returns the Java constructor corresponding to the Kotlin primary constructor (as defined in the Kotlin specification). Otherwise, in particular for non-Kotlin classes, this simply returnsnull
.- Parameters:
clazz
- the class to check- Since:
- Spring Framework 5.0
- See Also:
- Kotlin docs,
BeanUtils.findPrimaryConstructor(Class)
-
invokeBeanNameAware
public static void invokeBeanNameAware(java.lang.Object bean, java.lang.String beanName)
-
invokeAwareInterfaces
public static void invokeAwareInterfaces(java.lang.Object bean, 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
- Parameters:
bean
- the beancontext
-ApplicationContext
- See Also:
invokeAwareInterfaces(Object, BeanFactory)
,ApplicationContextAwareProcessor.invokeAwareInterfaces(Object)
,AbstractAutowireCapableBeanFactory.invokeAwareMethods(String, Object)
-
-