相关源码注释
Spring 5 SimpleAliasRegistry 源码注释 Spring 5 DefaultSingletonBeanRegistry 源码注释 Spring 5 FactoryBeanRegistrySupport 源码注释 Spring 5 AbstractBeanFactory 源码注释 Spring 5 AbstractAutowireCapableBeanFactory 源码注释 Spring 5 DefaultLisbaleBeanFactory 源码注释
UML类图
源码
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
implements AutowireCapableBeanFactory {
private InstantiationStrategy instantiationStrategy
= new CglibSubclassingInstantiationStrategy();
@Nullable
private ParameterNameDiscoverer parameterNameDiscoverer
= new DefaultParameterNameDiscoverer();
private boolean allowCircularReferences
= true;
private boolean allowRawInjectionDespiteWrapping
= false;
private final Set
<Class
<?>> ignoredDependencyTypes
= new HashSet<>();
private final Set
<Class
<?>> ignoredDependencyInterfaces
= new HashSet<>();
private final NamedThreadLocal
<String> currentlyCreatedBean
= new NamedThreadLocal<>("Currently created bean");
private final ConcurrentMap
<String, BeanWrapper> factoryBeanInstanceCache
= new ConcurrentHashMap<>();
private final ConcurrentMap
<Class
<?>, Method
[]> factoryMethodCandidateCache
= new ConcurrentHashMap<>();
private final ConcurrentMap
<Class
<?>, PropertyDescriptor
[]> filteredPropertyDescriptorsCache
=
new ConcurrentHashMap<>();
public AbstractAutowireCapableBeanFactory() {
super();
ignoreDependencyInterface(BeanNameAware
.class);
ignoreDependencyInterface(BeanFactoryAware
.class);
ignoreDependencyInterface(BeanClassLoaderAware
.class);
}
public AbstractAutowireCapableBeanFactory(@Nullable BeanFactory parentBeanFactory
) {
this();
setParentBeanFactory(parentBeanFactory
);
}
public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy
) {
this.instantiationStrategy
= instantiationStrategy
;
}
protected InstantiationStrategy
getInstantiationStrategy() {
return this.instantiationStrategy
;
}
public void setParameterNameDiscoverer(@Nullable ParameterNameDiscoverer parameterNameDiscoverer
) {
this.parameterNameDiscoverer
= parameterNameDiscoverer
;
}
@Nullable
protected ParameterNameDiscoverer
getParameterNameDiscoverer() {
return this.parameterNameDiscoverer
;
}
public void setAllowCircularReferences(boolean allowCircularReferences
) {
this.allowCircularReferences
= allowCircularReferences
;
}
public void setAllowRawInjectionDespiteWrapping(boolean allowRawInjectionDespiteWrapping
) {
this.allowRawInjectionDespiteWrapping
= allowRawInjectionDespiteWrapping
;
}
public void ignoreDependencyType(Class
<?> type
) {
this.ignoredDependencyTypes
.add(type
);
}
public void ignoreDependencyInterface(Class
<?> ifc
) {
this.ignoredDependencyInterfaces
.add(ifc
);
}
@Override
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory
) {
super.copyConfigurationFrom(otherFactory
);
if (otherFactory
instanceof AbstractAutowireCapableBeanFactory) {
AbstractAutowireCapableBeanFactory otherAutowireFactory
=
(AbstractAutowireCapableBeanFactory
) otherFactory
;
this.instantiationStrategy
= otherAutowireFactory
.instantiationStrategy
;
this.allowCircularReferences
= otherAutowireFactory
.allowCircularReferences
;
this.ignoredDependencyTypes
.addAll(otherAutowireFactory
.ignoredDependencyTypes
);
this.ignoredDependencyInterfaces
.addAll(otherAutowireFactory
.ignoredDependencyInterfaces
);
}
}
@Override
@SuppressWarnings("unchecked")
public <T> T
createBean(Class
<T> beanClass
) throws BeansException
{
RootBeanDefinition bd
= new RootBeanDefinition(beanClass
);
bd
.setScope(SCOPE_PROTOTYPE
);
bd
.allowCaching
= ClassUtils
.isCacheSafe(beanClass
, getBeanClassLoader());
return (T
) createBean(beanClass
.getName(), bd
, null
);
}
@Override
public void autowireBean(Object existingBean
) {
RootBeanDefinition bd
= new RootBeanDefinition(ClassUtils
.getUserClass(existingBean
));
bd
.setScope(BeanDefinition
.SCOPE_PROTOTYPE
);
bd
.allowCaching
= ClassUtils
.isCacheSafe(bd
.getBeanClass(), getBeanClassLoader());
BeanWrapper bw
= new BeanWrapperImpl(existingBean
);
initBeanWrapper(bw
);
populateBean(bd
.getBeanClass().getName(), bd
, bw
);
}
@Override
public Object
configureBean(Object existingBean
, String beanName
) throws BeansException
{
markBeanAsCreated(beanName
);
BeanDefinition mbd
= getMergedBeanDefinition(beanName
);
RootBeanDefinition bd
= null
;
if (mbd
instanceof RootBeanDefinition) {
RootBeanDefinition rbd
= (RootBeanDefinition
) mbd
;
bd
= (rbd
.isPrototype() ? rbd
: rbd
.cloneBeanDefinition());
}
if (bd
== null
) {
bd
= new RootBeanDefinition(mbd
);
}
if (!bd
.isPrototype()) {
bd
.setScope(BeanDefinition
.SCOPE_PROTOTYPE
);
bd
.allowCaching
= ClassUtils
.isCacheSafe(ClassUtils
.getUserClass(existingBean
), getBeanClassLoader());
}
BeanWrapper bw
= new BeanWrapperImpl(existingBean
);
initBeanWrapper(bw
);
populateBean(beanName
, bd
, bw
);
return initializeBean(beanName
, existingBean
, bd
);
}
@Override
public Object
createBean(Class
<?> beanClass
, int autowireMode
, boolean dependencyCheck
) throws BeansException
{
RootBeanDefinition bd
= new RootBeanDefinition(beanClass
, autowireMode
, dependencyCheck
);
bd
.setScope(BeanDefinition
.SCOPE_PROTOTYPE
);
return createBean(beanClass
.getName(), bd
, null
);
}
@Override
public Object
autowire(Class
<?> beanClass
, int autowireMode
, boolean dependencyCheck
) throws BeansException
{
final RootBeanDefinition bd
= new RootBeanDefinition(beanClass
, autowireMode
, dependencyCheck
);
bd
.setScope(BeanDefinition
.SCOPE_PROTOTYPE
);
if (bd
.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR
) {
return autowireConstructor(beanClass
.getName(), bd
, null
, null
).getWrappedInstance();
}
else {
Object bean
;
final BeanFactory parent
= this;
if (System
.getSecurityManager() != null
) {
bean
= AccessController
.doPrivileged((PrivilegedAction
<Object>) () ->
getInstantiationStrategy().instantiate(bd
, null
, parent
),
getAccessControlContext());
}
else {
bean
= getInstantiationStrategy().instantiate(bd
, null
, parent
);
}
populateBean(beanClass
.getName(), bd
, new BeanWrapperImpl(bean
));
return bean
;
}
}
@Override
public void autowireBeanProperties(Object existingBean
, int autowireMode
, boolean dependencyCheck
)
throws BeansException
{
if (autowireMode
== AUTOWIRE_CONSTRUCTOR
) {
throw new IllegalArgumentException("AUTOWIRE_CONSTRUCTOR not supported for existing bean instance");
}
RootBeanDefinition bd
=
new RootBeanDefinition(ClassUtils
.getUserClass(existingBean
), autowireMode
, dependencyCheck
);
bd
.setScope(BeanDefinition
.SCOPE_PROTOTYPE
);
BeanWrapper bw
= new BeanWrapperImpl(existingBean
);
initBeanWrapper(bw
);
populateBean(bd
.getBeanClass().getName(), bd
, bw
);
}
@Override
public void applyBeanPropertyValues(Object existingBean
, String beanName
) throws BeansException
{
markBeanAsCreated(beanName
);
BeanDefinition bd
= getMergedBeanDefinition(beanName
);
BeanWrapper bw
= new BeanWrapperImpl(existingBean
);
initBeanWrapper(bw
);
applyPropertyValues(beanName
, bd
, bw
, bd
.getPropertyValues());
}
@Override
public Object
initializeBean(Object existingBean
, String beanName
) {
return initializeBean(beanName
, existingBean
, null
);
}
@Override
public Object
applyBeanPostProcessorsBeforeInitialization(Object existingBean
, String beanName
)
throws BeansException
{
Object result
= existingBean
;
for (BeanPostProcessor processor
: getBeanPostProcessors()) {
Object current
= processor
.postProcessBeforeInitialization(result
, beanName
);
if (current
== null
) {
return result
;
}
result
= current
;
}
return result
;
}
@Override
public Object
applyBeanPostProcessorsAfterInitialization(Object existingBean
, String beanName
)
throws BeansException
{
Object result
= existingBean
;
for (BeanPostProcessor processor
: getBeanPostProcessors()) {
Object current
= processor
.postProcessAfterInitialization(result
, beanName
);
if (current
== null
) {
return result
;
}
result
= current
;
}
return result
;
}
@Override
public void destroyBean(Object existingBean
) {
new DisposableBeanAdapter(existingBean
, getBeanPostProcessors(), getAccessControlContext()).destroy();
}
@Override
public Object
resolveBeanByName(String name
, DependencyDescriptor descriptor
) {
InjectionPoint previousInjectionPoint
= ConstructorResolver
.setCurrentInjectionPoint(descriptor
);
try {
return getBean(name
, descriptor
.getDependencyType());
}
finally {
ConstructorResolver
.setCurrentInjectionPoint(previousInjectionPoint
);
}
}
@Override
@Nullable
public Object
resolveDependency(DependencyDescriptor descriptor
, @Nullable String requestingBeanName
) throws BeansException
{
return resolveDependency(descriptor
, requestingBeanName
, null
, null
);
}
@Override
protected Object
createBean(String beanName
, RootBeanDefinition mbd
, @Nullable Object
[] args
)
throws BeanCreationException
{
if (logger
.isTraceEnabled()) {
logger
.trace("Creating instance of bean '" + beanName
+ "'");
}
RootBeanDefinition mbdToUse
= mbd
;
Class
<?> resolvedClass
= resolveBeanClass(mbd
, beanName
);
if (resolvedClass
!= null
&& !mbd
.hasBeanClass() && mbd
.getBeanClassName() != null
) {
mbdToUse
= new RootBeanDefinition(mbd
);
mbdToUse
.setBeanClass(resolvedClass
);
}
try {
mbdToUse
.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex
) {
throw new BeanDefinitionStoreException(mbdToUse
.getResourceDescription(),
beanName
, "Validation of method overrides failed", ex
);
}
try {
Object bean
= resolveBeforeInstantiation(beanName
, mbdToUse
);
if (bean
!= null
) {
return bean
;
}
}
catch (Throwable ex
) {
throw new BeanCreationException(mbdToUse
.getResourceDescription(), beanName
,
"BeanPostProcessor before instantiation of bean failed", ex
);
}
try {
Object beanInstance
= doCreateBean(beanName
, mbdToUse
, args
);
if (logger
.isTraceEnabled()) {
logger
.trace("Finished creating instance of bean '" + beanName
+ "'");
}
return beanInstance
;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex
) {
throw ex
;
}
catch (Throwable ex
) {
throw new BeanCreationException(
mbdToUse
.getResourceDescription(), beanName
, "Unexpected exception during bean creation", ex
);
}
}
protected Object
doCreateBean(final String beanName
, final RootBeanDefinition mbd
, final @Nullable Object
[] args
)
throws BeanCreationException
{
BeanWrapper instanceWrapper
= null
;
if (mbd
.isSingleton()) {
instanceWrapper
= this.factoryBeanInstanceCache
.remove(beanName
);
}
if (instanceWrapper
== null
) {
instanceWrapper
= createBeanInstance(beanName
, mbd
, args
);
}
final Object bean
= instanceWrapper
.getWrappedInstance();
Class
<?> beanType
= instanceWrapper
.getWrappedClass();
if (beanType
!= NullBean
.class) {
mbd
.resolvedTargetType
= beanType
;
}
synchronized (mbd
.postProcessingLock
) {
if (!mbd
.postProcessed
) {
try {
applyMergedBeanDefinitionPostProcessors(mbd
, beanType
, beanName
);
}
catch (Throwable ex
) {
throw new BeanCreationException(mbd
.getResourceDescription(), beanName
,
"Post-processing of merged bean definition failed", ex
);
}
mbd
.postProcessed
= true;
}
}
boolean earlySingletonExposure
= (mbd
.isSingleton() && this.allowCircularReferences
&&
isSingletonCurrentlyInCreation(beanName
));
if (earlySingletonExposure
) {
if (logger
.isTraceEnabled()) {
logger
.trace("Eagerly caching bean '" + beanName
+
"' to allow for resolving potential circular references");
}
addSingletonFactory(beanName
, () -> getEarlyBeanReference(beanName
, mbd
, bean
));
}
Object exposedObject
= bean
;
try {
populateBean(beanName
, mbd
, instanceWrapper
);
exposedObject
= initializeBean(beanName
, exposedObject
, mbd
);
}
catch (Throwable ex
) {
if (ex
instanceof BeanCreationException && beanName
.equals(((BeanCreationException
) ex
).getBeanName())) {
throw (BeanCreationException
) ex
;
}
else {
throw new BeanCreationException(
mbd
.getResourceDescription(), beanName
, "Initialization of bean failed", ex
);
}
}
if (earlySingletonExposure
) {
Object earlySingletonReference
= getSingleton(beanName
, false);
if (earlySingletonReference
!= null
) {
if (exposedObject
== bean
) {
exposedObject
= earlySingletonReference
;
}
else if (!this.allowRawInjectionDespiteWrapping
&& hasDependentBean(beanName
)) {
String
[] dependentBeans
= getDependentBeans(beanName
);
Set
<String> actualDependentBeans
= new LinkedHashSet<>(dependentBeans
.length
);
for (String dependentBean
: dependentBeans
) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean
)) {
actualDependentBeans
.add(dependentBean
);
}
}
if (!actualDependentBeans
.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName
,
"Bean with name '" + beanName
+ "' has been injected into other beans [" +
StringUtils
.collectionToCommaDelimitedString(actualDependentBeans
) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
}
try {
registerDisposableBeanIfNecessary(beanName
, bean
, mbd
);
}
catch (BeanDefinitionValidationException ex
) {
throw new BeanCreationException(
mbd
.getResourceDescription(), beanName
, "Invalid destruction signature", ex
);
}
return exposedObject
;
}
@Override
@Nullable
protected Class
<?> predictBeanType(String beanName
, RootBeanDefinition mbd
, Class
<?>... typesToMatch
) {
Class
<?> targetType
= determineTargetType(beanName
, mbd
, typesToMatch
);
if (targetType
!= null
&& !mbd
.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
boolean matchingOnlyFactoryBean
= typesToMatch
.length
== 1 && typesToMatch
[0] == FactoryBean
.class;
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof SmartInstantiationAwareBeanPostProcessor) {
SmartInstantiationAwareBeanPostProcessor ibp
= (SmartInstantiationAwareBeanPostProcessor
) bp
;
Class
<?> predicted
= ibp
.predictBeanType(targetType
, beanName
);
if (predicted
!= null
&&
(!matchingOnlyFactoryBean
|| FactoryBean
.class.isAssignableFrom(predicted
))) {
return predicted
;
}
}
}
}
return targetType
;
}
@Nullable
protected Class
<?> determineTargetType(String beanName
, RootBeanDefinition mbd
, Class
<?>... typesToMatch
) {
Class
<?> targetType
= mbd
.getTargetType();
if (targetType
== null
) {
targetType
= (mbd
.getFactoryMethodName() != null
?
getTypeForFactoryMethod(beanName
, mbd
, typesToMatch
) :
resolveBeanClass(mbd
, beanName
, typesToMatch
));
if (ObjectUtils
.isEmpty(typesToMatch
) || getTempClassLoader() == null
) {
mbd
.resolvedTargetType
= targetType
;
}
}
return targetType
;
}
@Nullable
protected Class
<?> getTypeForFactoryMethod(String beanName
, RootBeanDefinition mbd
, Class
<?>... typesToMatch
) {
ResolvableType cachedReturnType
= mbd
.factoryMethodReturnType
;
if (cachedReturnType
!= null
) {
return cachedReturnType
.resolve();
}
Class
<?> commonType
= null
;
Method uniqueCandidate
= mbd
.factoryMethodToIntrospect
;
if (uniqueCandidate
== null
) {
Class
<?> factoryClass
;
boolean isStatic
= true;
String factoryBeanName
= mbd
.getFactoryBeanName();
if (factoryBeanName
!= null
) {
if (factoryBeanName
.equals(beanName
)) {
throw new BeanDefinitionStoreException(mbd
.getResourceDescription(), beanName
,
"factory-bean reference points back to the same bean definition");
}
factoryClass
= getType(factoryBeanName
);
isStatic
= false;
}
else {
factoryClass
= resolveBeanClass(mbd
, beanName
, typesToMatch
);
}
if (factoryClass
== null
) {
return null
;
}
factoryClass
= ClassUtils
.getUserClass(factoryClass
);
int minNrOfArgs
=
(mbd
.hasConstructorArgumentValues() ? mbd
.getConstructorArgumentValues().getArgumentCount() : 0);
Method
[] candidates
= this.factoryMethodCandidateCache
.computeIfAbsent(factoryClass
,
clazz
-> ReflectionUtils
.getUniqueDeclaredMethods(clazz
, ReflectionUtils
.USER_DECLARED_METHODS
));
for (Method candidate
: candidates
) {
if (Modifier
.isStatic(candidate
.getModifiers()) == isStatic
&& mbd
.isFactoryMethod(candidate
) &&
candidate
.getParameterCount() >= minNrOfArgs
) {
if (candidate
.getTypeParameters().length
> 0) {
try {
Class
<?>[] paramTypes
= candidate
.getParameterTypes();
String
[] paramNames
= null
;
ParameterNameDiscoverer pnd
= getParameterNameDiscoverer();
if (pnd
!= null
) {
paramNames
= pnd
.getParameterNames(candidate
);
}
ConstructorArgumentValues cav
= mbd
.getConstructorArgumentValues();
Set
<ConstructorArgumentValues.ValueHolder> usedValueHolders
= new HashSet<>(paramTypes
.length
);
Object
[] args
= new Object[paramTypes
.length
];
for (int i
= 0; i
< args
.length
; i
++) {
ConstructorArgumentValues
.ValueHolder valueHolder
= cav
.getArgumentValue(
i
, paramTypes
[i
], (paramNames
!= null
? paramNames
[i
] : null
), usedValueHolders
);
if (valueHolder
== null
) {
valueHolder
= cav
.getGenericArgumentValue(null
, null
, usedValueHolders
);
}
if (valueHolder
!= null
) {
args
[i
] = valueHolder
.getValue();
usedValueHolders
.add(valueHolder
);
}
}
Class
<?> returnType
= AutowireUtils
.resolveReturnTypeForFactoryMethod(
candidate
, args
, getBeanClassLoader());
uniqueCandidate
= (commonType
== null
&& returnType
== candidate
.getReturnType() ?
candidate
: null
);
commonType
= ClassUtils
.determineCommonAncestor(returnType
, commonType
);
if (commonType
== null
) {
return null
;
}
}
catch (Throwable ex
) {
if (logger
.isDebugEnabled()) {
logger
.debug("Failed to resolve generic return type for factory method: " + ex
);
}
}
}
else {
uniqueCandidate
= (commonType
== null
? candidate
: null
);
commonType
= ClassUtils
.determineCommonAncestor(candidate
.getReturnType(), commonType
);
if (commonType
== null
) {
return null
;
}
}
}
}
mbd
.factoryMethodToIntrospect
= uniqueCandidate
;
if (commonType
== null
) {
return null
;
}
}
cachedReturnType
= (uniqueCandidate
!= null
?
ResolvableType
.forMethodReturnType(uniqueCandidate
) : ResolvableType
.forClass(commonType
));
mbd
.factoryMethodReturnType
= cachedReturnType
;
return cachedReturnType
.resolve();
}
@Override
protected ResolvableType
getTypeForFactoryBean(String beanName
, RootBeanDefinition mbd
, boolean allowInit
) {
ResolvableType result
= getTypeForFactoryBeanFromAttributes(mbd
);
if (result
!= ResolvableType
.NONE
) {
return result
;
}
ResolvableType beanType
=
(mbd
.hasBeanClass() ? ResolvableType
.forClass(mbd
.getBeanClass()) : ResolvableType
.NONE
);
if (mbd
.getInstanceSupplier() != null
) {
result
= getFactoryBeanGeneric(mbd
.targetType
);
if (result
.resolve() != null
) {
return result
;
}
result
= getFactoryBeanGeneric(beanType
);
if (result
.resolve() != null
) {
return result
;
}
}
String factoryBeanName
= mbd
.getFactoryBeanName();
String factoryMethodName
= mbd
.getFactoryMethodName();
if (factoryBeanName
!= null
) {
if (factoryMethodName
!= null
) {
BeanDefinition factoryBeanDefinition
= getBeanDefinition(factoryBeanName
);
Class
<?> factoryBeanClass
;
if (factoryBeanDefinition
instanceof AbstractBeanDefinition &&
((AbstractBeanDefinition
) factoryBeanDefinition
).hasBeanClass()) {
factoryBeanClass
= ((AbstractBeanDefinition
) factoryBeanDefinition
).getBeanClass();
}
else {
RootBeanDefinition fbmbd
= getMergedBeanDefinition(factoryBeanName
, factoryBeanDefinition
);
factoryBeanClass
= determineTargetType(factoryBeanName
, fbmbd
);
}
if (factoryBeanClass
!= null
) {
result
= getTypeForFactoryBeanFromMethod(factoryBeanClass
, factoryMethodName
);
if (result
.resolve() != null
) {
return result
;
}
}
}
if (!isBeanEligibleForMetadataCaching(factoryBeanName
)) {
return ResolvableType
.NONE
;
}
}
if (allowInit
) {
FactoryBean
<?> factoryBean
= (mbd
.isSingleton() ?
getSingletonFactoryBeanForTypeCheck(beanName
, mbd
) :
getNonSingletonFactoryBeanForTypeCheck(beanName
, mbd
));
if (factoryBean
!= null
) {
Class
<?> type
= getTypeForFactoryBean(factoryBean
);
if (type
!= null
) {
return ResolvableType
.forClass(type
);
}
return super.getTypeForFactoryBean(beanName
, mbd
, true);
}
}
if (factoryBeanName
== null
&& mbd
.hasBeanClass() && factoryMethodName
!= null
) {
return getTypeForFactoryBeanFromMethod(mbd
.getBeanClass(), factoryMethodName
);
}
result
= getFactoryBeanGeneric(beanType
);
if (result
.resolve() != null
) {
return result
;
}
return ResolvableType
.NONE
;
}
private ResolvableType
getFactoryBeanGeneric(@Nullable ResolvableType type
) {
if (type
== null
) {
return ResolvableType
.NONE
;
}
return type
.as(FactoryBean
.class).getGeneric();
}
private ResolvableType
getTypeForFactoryBeanFromMethod(Class
<?> beanClass
, String factoryMethodName
) {
Class
<?> factoryBeanClass
= ClassUtils
.getUserClass(beanClass
);
FactoryBeanMethodTypeFinder finder
= new FactoryBeanMethodTypeFinder(factoryMethodName
);
ReflectionUtils
.doWithMethods(factoryBeanClass
, finder
, ReflectionUtils
.USER_DECLARED_METHODS
);
return finder
.getResult();
}
@Override
@Deprecated
@Nullable
protected Class
<?> getTypeForFactoryBean(String beanName
, RootBeanDefinition mbd
) {
return getTypeForFactoryBean(beanName
, mbd
, true).resolve();
}
protected Object
getEarlyBeanReference(String beanName
, RootBeanDefinition mbd
, Object bean
) {
Object exposedObject
= bean
;
if (!mbd
.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof SmartInstantiationAwareBeanPostProcessor) {
SmartInstantiationAwareBeanPostProcessor ibp
= (SmartInstantiationAwareBeanPostProcessor
) bp
;
exposedObject
= ibp
.getEarlyBeanReference(exposedObject
, beanName
);
}
}
}
return exposedObject
;
}
@Nullable
private FactoryBean
<?> getSingletonFactoryBeanForTypeCheck(String beanName
, RootBeanDefinition mbd
) {
synchronized (getSingletonMutex()) {
BeanWrapper bw
= this.factoryBeanInstanceCache
.get(beanName
);
if (bw
!= null
) {
return (FactoryBean
<?>) bw
.getWrappedInstance();
}
Object beanInstance
= getSingleton(beanName
, false);
if (beanInstance
instanceof FactoryBean) {
return (FactoryBean
<?>) beanInstance
;
}
if (isSingletonCurrentlyInCreation(beanName
) ||
(mbd
.getFactoryBeanName() != null
&& isSingletonCurrentlyInCreation(mbd
.getFactoryBeanName()))) {
return null
;
}
Object instance
;
try {
beforeSingletonCreation(beanName
);
instance
= resolveBeforeInstantiation(beanName
, mbd
);
if (instance
== null
) {
bw
= createBeanInstance(beanName
, mbd
, null
);
instance
= bw
.getWrappedInstance();
}
}
catch (UnsatisfiedDependencyException ex
) {
throw ex
;
}
catch (BeanCreationException ex
) {
if (logger
.isDebugEnabled()) {
logger
.debug("Bean creation exception on singleton FactoryBean type check: " + ex
);
}
onSuppressedException(ex
);
return null
;
}
finally {
afterSingletonCreation(beanName
);
}
FactoryBean
<?> fb
= getFactoryBean(beanName
, instance
);
if (bw
!= null
) {
this.factoryBeanInstanceCache
.put(beanName
, bw
);
}
return fb
;
}
}
@Nullable
private FactoryBean
<?> getNonSingletonFactoryBeanForTypeCheck(String beanName
, RootBeanDefinition mbd
) {
if (isPrototypeCurrentlyInCreation(beanName
)) {
return null
;
}
Object instance
;
try {
beforePrototypeCreation(beanName
);
instance
= resolveBeforeInstantiation(beanName
, mbd
);
if (instance
== null
) {
BeanWrapper bw
= createBeanInstance(beanName
, mbd
, null
);
instance
= bw
.getWrappedInstance();
}
}
catch (UnsatisfiedDependencyException ex
) {
throw ex
;
}
catch (BeanCreationException ex
) {
if (logger
.isDebugEnabled()) {
logger
.debug("Bean creation exception on non-singleton FactoryBean type check: " + ex
);
}
onSuppressedException(ex
);
return null
;
}
finally {
afterPrototypeCreation(beanName
);
}
return getFactoryBean(beanName
, instance
);
}
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd
, Class
<?> beanType
, String beanName
) {
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp
= (MergedBeanDefinitionPostProcessor
) bp
;
bdp
.postProcessMergedBeanDefinition(mbd
, beanType
, beanName
);
}
}
}
@Nullable
protected Object
resolveBeforeInstantiation(String beanName
, RootBeanDefinition mbd
) {
Object bean
= null
;
if (!Boolean
.FALSE
.equals(mbd
.beforeInstantiationResolved
)) {
if (!mbd
.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
Class
<?> targetType
= determineTargetType(beanName
, mbd
);
if (targetType
!= null
) {
bean
= applyBeanPostProcessorsBeforeInstantiation(targetType
, beanName
);
if (bean
!= null
) {
bean
= applyBeanPostProcessorsAfterInitialization(bean
, beanName
);
}
}
}
mbd
.beforeInstantiationResolved
= (bean
!= null
);
}
return bean
;
}
@Nullable
protected Object
applyBeanPostProcessorsBeforeInstantiation(Class
<?> beanClass
, String beanName
) {
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp
= (InstantiationAwareBeanPostProcessor
) bp
;
Object result
= ibp
.postProcessBeforeInstantiation(beanClass
, beanName
);
if (result
!= null
) {
return result
;
}
}
}
return null
;
}
protected BeanWrapper
createBeanInstance(String beanName
, RootBeanDefinition mbd
, @Nullable Object
[] args
) {
Class
<?> beanClass
= resolveBeanClass(mbd
, beanName
);
if (beanClass
!= null
&& !Modifier
.isPublic(beanClass
.getModifiers()) && !mbd
.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd
.getResourceDescription(), beanName
,
"Bean class isn't public, and non-public access not allowed: " + beanClass
.getName());
}
Supplier
<?> instanceSupplier
= mbd
.getInstanceSupplier();
if (instanceSupplier
!= null
) {
return obtainFromSupplier(instanceSupplier
, beanName
);
}
if (mbd
.getFactoryMethodName() != null
) {
return instantiateUsingFactoryMethod(beanName
, mbd
, args
);
}
boolean resolved
= false;
boolean autowireNecessary
= false;
if (args
== null
) {
synchronized (mbd
.constructorArgumentLock
) {
if (mbd
.resolvedConstructorOrFactoryMethod
!= null
) {
resolved
= true;
autowireNecessary
= mbd
.constructorArgumentsResolved
;
}
}
}
if (resolved
) {
if (autowireNecessary
) {
return autowireConstructor(beanName
, mbd
, null
, null
);
}
else {
return instantiateBean(beanName
, mbd
);
}
}
Constructor
<?>[] ctors
= determineConstructorsFromBeanPostProcessors(beanClass
, beanName
);
if (ctors
!= null
|| mbd
.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR
||
mbd
.hasConstructorArgumentValues() || !ObjectUtils
.isEmpty(args
)) {
return autowireConstructor(beanName
, mbd
, ctors
, args
);
}
ctors
= mbd
.getPreferredConstructors();
if (ctors
!= null
) {
return autowireConstructor(beanName
, mbd
, ctors
, null
);
}
return instantiateBean(beanName
, mbd
);
}
protected BeanWrapper
obtainFromSupplier(Supplier
<?> instanceSupplier
, String beanName
) {
Object instance
;
String outerBean
= this.currentlyCreatedBean
.get();
this.currentlyCreatedBean
.set(beanName
);
try {
instance
= instanceSupplier
.get();
}
finally {
if (outerBean
!= null
) {
this.currentlyCreatedBean
.set(outerBean
);
}
else {
this.currentlyCreatedBean
.remove();
}
}
if (instance
== null
) {
instance
= new NullBean();
}
BeanWrapper bw
= new BeanWrapperImpl(instance
);
initBeanWrapper(bw
);
return bw
;
}
@Override
protected Object
getObjectForBeanInstance(
Object beanInstance
, String name
, String beanName
, @Nullable RootBeanDefinition mbd
) {
String currentlyCreatedBean
= this.currentlyCreatedBean
.get();
if (currentlyCreatedBean
!= null
) {
registerDependentBean(beanName
, currentlyCreatedBean
);
}
return super.getObjectForBeanInstance(beanInstance
, name
, beanName
, mbd
);
}
@Nullable
protected Constructor
<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class
<?> beanClass
, String beanName
)
throws BeansException
{
if (beanClass
!= null
&& hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof SmartInstantiationAwareBeanPostProcessor) {
SmartInstantiationAwareBeanPostProcessor ibp
= (SmartInstantiationAwareBeanPostProcessor
) bp
;
Constructor
<?>[] ctors
= ibp
.determineCandidateConstructors(beanClass
, beanName
);
if (ctors
!= null
) {
return ctors
;
}
}
}
}
return null
;
}
protected BeanWrapper
instantiateBean(final String beanName
, final RootBeanDefinition mbd
) {
try {
Object beanInstance
;
final BeanFactory parent
= this;
if (System
.getSecurityManager() != null
) {
beanInstance
= AccessController
.doPrivileged((PrivilegedAction
<Object>) () ->
getInstantiationStrategy().instantiate(mbd
, beanName
, parent
),
getAccessControlContext());
}
else {
beanInstance
= getInstantiationStrategy().instantiate(mbd
, beanName
, parent
);
}
BeanWrapper bw
= new BeanWrapperImpl(beanInstance
);
initBeanWrapper(bw
);
return bw
;
}
catch (Throwable ex
) {
throw new BeanCreationException(
mbd
.getResourceDescription(), beanName
, "Instantiation of bean failed", ex
);
}
}
protected BeanWrapper
instantiateUsingFactoryMethod(
String beanName
, RootBeanDefinition mbd
, @Nullable Object
[] explicitArgs
) {
return new ConstructorResolver(this).instantiateUsingFactoryMethod(beanName
, mbd
, explicitArgs
);
}
protected BeanWrapper
autowireConstructor(
String beanName
, RootBeanDefinition mbd
, @Nullable Constructor
<?>[] ctors
, @Nullable Object
[] explicitArgs
) {
return new ConstructorResolver(this).autowireConstructor(beanName
, mbd
, ctors
, explicitArgs
);
}
@SuppressWarnings("deprecation")
protected void populateBean(String beanName
, RootBeanDefinition mbd
, @Nullable BeanWrapper bw
) {
if (bw
== null
) {
if (mbd
.hasPropertyValues()) {
throw new BeanCreationException(
mbd
.getResourceDescription(), beanName
, "Cannot apply property values to null instance");
}
else {
return;
}
}
boolean continueWithPropertyPopulation
= true;
if (!mbd
.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp
= (InstantiationAwareBeanPostProcessor
) bp
;
if (!ibp
.postProcessAfterInstantiation(bw
.getWrappedInstance(), beanName
)) {
continueWithPropertyPopulation
= false;
break;
}
}
}
}
if (!continueWithPropertyPopulation
) {
return;
}
PropertyValues pvs
= (mbd
.hasPropertyValues() ? mbd
.getPropertyValues() : null
);
int resolvedAutowireMode
= mbd
.getResolvedAutowireMode();
if (resolvedAutowireMode
== AUTOWIRE_BY_NAME
|| resolvedAutowireMode
== AUTOWIRE_BY_TYPE
) {
MutablePropertyValues newPvs
= new MutablePropertyValues(pvs
);
if (resolvedAutowireMode
== AUTOWIRE_BY_NAME
) {
autowireByName(beanName
, mbd
, bw
, newPvs
);
}
if (resolvedAutowireMode
== AUTOWIRE_BY_TYPE
) {
autowireByType(beanName
, mbd
, bw
, newPvs
);
}
pvs
= newPvs
;
}
boolean hasInstAwareBpps
= hasInstantiationAwareBeanPostProcessors();
boolean needsDepCheck
= (mbd
.getDependencyCheck() != AbstractBeanDefinition
.DEPENDENCY_CHECK_NONE
);
PropertyDescriptor
[] filteredPds
= null
;
if (hasInstAwareBpps
) {
if (pvs
== null
) {
pvs
= mbd
.getPropertyValues();
}
for (BeanPostProcessor bp
: getBeanPostProcessors()) {
if (bp
instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp
= (InstantiationAwareBeanPostProcessor
) bp
;
PropertyValues pvsToUse
= ibp
.postProcessProperties(pvs
, bw
.getWrappedInstance(), beanName
);
if (pvsToUse
== null
) {
if (filteredPds
== null
) {
filteredPds
= filterPropertyDescriptorsForDependencyCheck(bw
, mbd
.allowCaching
);
}
pvsToUse
= ibp
.postProcessPropertyValues(pvs
, filteredPds
, bw
.getWrappedInstance(), beanName
);
if (pvsToUse
== null
) {
return;
}
}
pvs
= pvsToUse
;
}
}
}
if (needsDepCheck
) {
if (filteredPds
== null
) {
filteredPds
= filterPropertyDescriptorsForDependencyCheck(bw
, mbd
.allowCaching
);
}
checkDependencies(beanName
, mbd
, filteredPds
, pvs
);
}
if (pvs
!= null
) {
applyPropertyValues(beanName
, mbd
, bw
, pvs
);
}
}
protected void autowireByName(
String beanName
, AbstractBeanDefinition mbd
, BeanWrapper bw
, MutablePropertyValues pvs
) {
String
[] propertyNames
= unsatisfiedNonSimpleProperties(mbd
, bw
);
for (String propertyName
: propertyNames
) {
if (containsBean(propertyName
)) {
Object bean
= getBean(propertyName
);
pvs
.add(propertyName
, bean
);
registerDependentBean(propertyName
, beanName
);
if (logger
.isTraceEnabled()) {
logger
.trace("Added autowiring by name from bean name '" + beanName
+
"' via property '" + propertyName
+ "' to bean named '" + propertyName
+ "'");
}
}
else {
if (logger
.isTraceEnabled()) {
logger
.trace("Not autowiring property '" + propertyName
+ "' of bean '" + beanName
+
"' by name: no matching bean found");
}
}
}
}
protected void autowireByType(
String beanName
, AbstractBeanDefinition mbd
, BeanWrapper bw
, MutablePropertyValues pvs
) {
TypeConverter converter
= getCustomTypeConverter();
if (converter
== null
) {
converter
= bw
;
}
Set
<String> autowiredBeanNames
= new LinkedHashSet<>(4);
String
[] propertyNames
= unsatisfiedNonSimpleProperties(mbd
, bw
);
for (String propertyName
: propertyNames
) {
try {
PropertyDescriptor pd
= bw
.getPropertyDescriptor(propertyName
);
if (Object
.class != pd
.getPropertyType()) {
MethodParameter methodParam
= BeanUtils
.getWriteMethodParameter(pd
);
boolean eager
= !PriorityOrdered
.class.isInstance(bw
.getWrappedInstance());
DependencyDescriptor desc
= new AutowireByTypeDependencyDescriptor(methodParam
, eager
);
Object autowiredArgument
= resolveDependency(desc
, beanName
, autowiredBeanNames
, converter
);
if (autowiredArgument
!= null
) {
pvs
.add(propertyName
, autowiredArgument
);
}
for (String autowiredBeanName
: autowiredBeanNames
) {
registerDependentBean(autowiredBeanName
, beanName
);
if (logger
.isTraceEnabled()) {
logger
.trace("Autowiring by type from bean name '" + beanName
+ "' via property '" +
propertyName
+ "' to bean named '" + autowiredBeanName
+ "'");
}
}
autowiredBeanNames
.clear();
}
}
catch (BeansException ex
) {
throw new UnsatisfiedDependencyException(mbd
.getResourceDescription(), beanName
, propertyName
, ex
);
}
}
}
protected String
[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd
, BeanWrapper bw
) {
Set
<String> result
= new TreeSet<>();
PropertyValues pvs
= mbd
.getPropertyValues();
PropertyDescriptor
[] pds
= bw
.getPropertyDescriptors();
for (PropertyDescriptor pd
: pds
) {
if (pd
.getWriteMethod() != null
&& !isExcludedFromDependencyCheck(pd
) && !pvs
.contains(pd
.getName()) &&
!BeanUtils
.isSimpleProperty(pd
.getPropertyType())) {
result
.add(pd
.getName());
}
}
return StringUtils
.toStringArray(result
);
}
protected PropertyDescriptor
[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw
, boolean cache
) {
PropertyDescriptor
[] filtered
= this.filteredPropertyDescriptorsCache
.get(bw
.getWrappedClass());
if (filtered
== null
) {
filtered
= filterPropertyDescriptorsForDependencyCheck(bw
);
if (cache
) {
PropertyDescriptor
[] existing
=
this.filteredPropertyDescriptorsCache
.putIfAbsent(bw
.getWrappedClass(), filtered
);
if (existing
!= null
) {
filtered
= existing
;
}
}
}
return filtered
;
}
protected PropertyDescriptor
[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw
) {
List
<PropertyDescriptor> pds
= new ArrayList<>(Arrays
.asList(bw
.getPropertyDescriptors()));
pds
.removeIf(this::isExcludedFromDependencyCheck
);
return pds
.toArray(new PropertyDescriptor[0]);
}
protected boolean isExcludedFromDependencyCheck(PropertyDescriptor pd
) {
return (AutowireUtils
.isExcludedFromDependencyCheck(pd
) ||
this.ignoredDependencyTypes
.contains(pd
.getPropertyType()) ||
AutowireUtils
.isSetterDefinedInInterface(pd
, this.ignoredDependencyInterfaces
));
}
protected void checkDependencies(
String beanName
, AbstractBeanDefinition mbd
, PropertyDescriptor
[] pds
, @Nullable PropertyValues pvs
)
throws UnsatisfiedDependencyException
{
int dependencyCheck
= mbd
.getDependencyCheck();
for (PropertyDescriptor pd
: pds
) {
if (pd
.getWriteMethod() != null
&& (pvs
== null
|| !pvs
.contains(pd
.getName()))) {
boolean isSimple
= BeanUtils
.isSimpleProperty(pd
.getPropertyType());
boolean unsatisfied
= (dependencyCheck
== AbstractBeanDefinition
.DEPENDENCY_CHECK_ALL
) ||
(isSimple
&& dependencyCheck
== AbstractBeanDefinition
.DEPENDENCY_CHECK_SIMPLE
) ||
(!isSimple
&& dependencyCheck
== AbstractBeanDefinition
.DEPENDENCY_CHECK_OBJECTS
);
if (unsatisfied
) {
throw new UnsatisfiedDependencyException(mbd
.getResourceDescription(), beanName
, pd
.getName(),
"Set this property value or disable dependency checking for this bean.");
}
}
}
}
protected void applyPropertyValues(String beanName
, BeanDefinition mbd
, BeanWrapper bw
, PropertyValues pvs
) {
if (pvs
.isEmpty()) {
return;
}
if (System
.getSecurityManager() != null
&& bw
instanceof BeanWrapperImpl) {
((BeanWrapperImpl
) bw
).setSecurityContext(getAccessControlContext());
}
MutablePropertyValues mpvs
= null
;
List
<PropertyValue> original
;
if (pvs
instanceof MutablePropertyValues) {
mpvs
= (MutablePropertyValues
) pvs
;
if (mpvs
.isConverted()) {
try {
bw
.setPropertyValues(mpvs
);
return;
}
catch (BeansException ex
) {
throw new BeanCreationException(
mbd
.getResourceDescription(), beanName
, "Error setting property values", ex
);
}
}
original
= mpvs
.getPropertyValueList();
}
else {
original
= Arrays
.asList(pvs
.getPropertyValues());
}
TypeConverter converter
= getCustomTypeConverter();
if (converter
== null
) {
converter
= bw
;
}
BeanDefinitionValueResolver valueResolver
= new BeanDefinitionValueResolver(this, beanName
, mbd
, converter
);
List
<PropertyValue> deepCopy
= new ArrayList<>(original
.size());
boolean resolveNecessary
= false;
for (PropertyValue pv
: original
) {
if (pv
.isConverted()) {
deepCopy
.add(pv
);
}
else {
String propertyName
= pv
.getName();
Object originalValue
= pv
.getValue();
if (originalValue
== AutowiredPropertyMarker
.INSTANCE
) {
Method writeMethod
= bw
.getPropertyDescriptor(propertyName
).getWriteMethod();
if (writeMethod
== null
) {
throw new IllegalArgumentException("Autowire marker for property without write method: " + pv
);
}
originalValue
= new DependencyDescriptor(new MethodParameter(writeMethod
, 0), true);
}
Object resolvedValue
= valueResolver
.resolveValueIfNecessary(pv
, originalValue
);
Object convertedValue
= resolvedValue
;
boolean convertible
= bw
.isWritableProperty(propertyName
) &&
!PropertyAccessorUtils
.isNestedOrIndexedProperty(propertyName
);
if (convertible
) {
convertedValue
= convertForProperty(resolvedValue
, propertyName
, bw
, converter
);
}
if (resolvedValue
== originalValue
) {
if (convertible
) {
pv
.setConvertedValue(convertedValue
);
}
deepCopy
.add(pv
);
}
else if (convertible
&& originalValue
instanceof TypedStringValue &&
!((TypedStringValue
) originalValue
).isDynamic() &&
!(convertedValue
instanceof Collection || ObjectUtils
.isArray(convertedValue
))) {
pv
.setConvertedValue(convertedValue
);
deepCopy
.add(pv
);
}
else {
resolveNecessary
= true;
deepCopy
.add(new PropertyValue(pv
, convertedValue
));
}
}
}
if (mpvs
!= null
&& !resolveNecessary
) {
mpvs
.setConverted();
}
try {
bw
.setPropertyValues(new MutablePropertyValues(deepCopy
));
}
catch (BeansException ex
) {
throw new BeanCreationException(
mbd
.getResourceDescription(), beanName
, "Error setting property values", ex
);
}
}
@Nullable
private Object
convertForProperty(
@Nullable Object value
, String propertyName
, BeanWrapper bw
, TypeConverter converter
) {
if (converter
instanceof BeanWrapperImpl) {
return ((BeanWrapperImpl
) converter
).convertForProperty(value
, propertyName
);
}
else {
PropertyDescriptor pd
= bw
.getPropertyDescriptor(propertyName
);
MethodParameter methodParam
= BeanUtils
.getWriteMethodParameter(pd
);
return converter
.convertIfNecessary(value
, pd
.getPropertyType(), methodParam
);
}
}
protected Object
initializeBean(final String beanName
, final Object bean
, @Nullable RootBeanDefinition mbd
) {
if (System
.getSecurityManager() != null
) {
AccessController
.doPrivileged((PrivilegedAction
<Object>) () -> {
invokeAwareMethods(beanName
, bean
);
return null
;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName
, bean
);
}
Object wrappedBean
= bean
;
if (mbd
== null
|| !mbd
.isSynthetic()) {
wrappedBean
= applyBeanPostProcessorsBeforeInitialization(wrappedBean
, beanName
);
}
try {
invokeInitMethods(beanName
, wrappedBean
, mbd
);
}
catch (Throwable ex
) {
throw new BeanCreationException(
(mbd
!= null
? mbd
.getResourceDescription() : null
),
beanName
, "Invocation of init method failed", ex
);
}
if (mbd
== null
|| !mbd
.isSynthetic()) {
wrappedBean
= applyBeanPostProcessorsAfterInitialization(wrappedBean
, beanName
);
}
return wrappedBean
;
}
private void invokeAwareMethods(final String beanName
, final Object bean
) {
if (bean
instanceof Aware) {
if (bean
instanceof BeanNameAware) {
((BeanNameAware
) bean
).setBeanName(beanName
);
}
if (bean
instanceof BeanClassLoaderAware) {
ClassLoader bcl
= getBeanClassLoader();
if (bcl
!= null
) {
((BeanClassLoaderAware
) bean
).setBeanClassLoader(bcl
);
}
}
if (bean
instanceof BeanFactoryAware) {
((BeanFactoryAware
) bean
).setBeanFactory(AbstractAutowireCapableBeanFactory
.this);
}
}
}
protected void invokeInitMethods(String beanName
, final Object bean
, @Nullable RootBeanDefinition mbd
)
throws Throwable
{
boolean isInitializingBean
= (bean
instanceof InitializingBean);
if (isInitializingBean
&& (mbd
== null
|| !mbd
.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger
.isTraceEnabled()) {
logger
.trace("Invoking afterPropertiesSet() on bean with name '" + beanName
+ "'");
}
if (System
.getSecurityManager() != null
) {
try {
AccessController
.doPrivileged((PrivilegedExceptionAction
<Object>) () -> {
((InitializingBean
) bean
).afterPropertiesSet();
return null
;
}, getAccessControlContext());
}
catch (PrivilegedActionException pae
) {
throw pae
.getException();
}
}
else {
((InitializingBean
) bean
).afterPropertiesSet();
}
}
if (mbd
!= null
&& bean
.getClass() != NullBean
.class) {
String initMethodName
= mbd
.getInitMethodName();
if (StringUtils
.hasLength(initMethodName
) &&
!(isInitializingBean
&& "afterPropertiesSet".equals(initMethodName
)) &&
!mbd
.isExternallyManagedInitMethod(initMethodName
)) {
invokeCustomInitMethod(beanName
, bean
, mbd
);
}
}
}
protected void invokeCustomInitMethod(String beanName
, final Object bean
, RootBeanDefinition mbd
)
throws Throwable
{
String initMethodName
= mbd
.getInitMethodName();
Assert
.state(initMethodName
!= null
, "No init method set");
Method initMethod
= (mbd
.isNonPublicAccessAllowed() ?
BeanUtils
.findMethod(bean
.getClass(), initMethodName
) :
ClassUtils
.getMethodIfAvailable(bean
.getClass(), initMethodName
));
if (initMethod
== null
) {
if (mbd
.isEnforceInitMethod()) {
throw new BeanDefinitionValidationException("Could not find an init method named '" +
initMethodName
+ "' on bean with name '" + beanName
+ "'");
}
else {
if (logger
.isTraceEnabled()) {
logger
.trace("No default init method named '" + initMethodName
+
"' found on bean with name '" + beanName
+ "'");
}
return;
}
}
if (logger
.isTraceEnabled()) {
logger
.trace("Invoking init method '" + initMethodName
+ "' on bean with name '" + beanName
+ "'");
}
Method methodToInvoke
= ClassUtils
.getInterfaceMethodIfPossible(initMethod
);
if (System
.getSecurityManager() != null
) {
AccessController
.doPrivileged((PrivilegedAction
<Object>) () -> {
ReflectionUtils
.makeAccessible(methodToInvoke
);
return null
;
});
try {
AccessController
.doPrivileged((PrivilegedExceptionAction
<Object>) () ->
methodToInvoke
.invoke(bean
), getAccessControlContext());
}
catch (PrivilegedActionException pae
) {
InvocationTargetException ex
= (InvocationTargetException
) pae
.getException();
throw ex
.getTargetException();
}
}
else {
try {
ReflectionUtils
.makeAccessible(methodToInvoke
);
methodToInvoke
.invoke(bean
);
}
catch (InvocationTargetException ex
) {
throw ex
.getTargetException();
}
}
}
@Override
protected Object
postProcessObjectFromFactoryBean(Object object
, String beanName
) {
return applyBeanPostProcessorsAfterInitialization(object
, beanName
);
}
@Override
protected void removeSingleton(String beanName
) {
synchronized (getSingletonMutex()) {
super.removeSingleton(beanName
);
this.factoryBeanInstanceCache
.remove(beanName
);
}
}
@Override
protected void clearSingletonCache() {
synchronized (getSingletonMutex()) {
super.clearSingletonCache();
this.factoryBeanInstanceCache
.clear();
}
}
Log
getLogger() {
return logger
;
}
@SuppressWarnings("serial")
private static class AutowireByTypeDependencyDescriptor extends DependencyDescriptor {
public AutowireByTypeDependencyDescriptor(MethodParameter methodParameter
, boolean eager
) {
super(methodParameter
, false, eager
);
}
@Override
public String
getDependencyName() {
return null
;
}
}
private static class FactoryBeanMethodTypeFinder implements MethodCallback {
private final String factoryMethodName
;
private ResolvableType result
= ResolvableType
.NONE
;
FactoryBeanMethodTypeFinder(String factoryMethodName
) {
this.factoryMethodName
= factoryMethodName
;
}
@Override
public void doWith(Method method
) throws IllegalArgumentException
, IllegalAccessException
{
if (isFactoryBeanMethod(method
)) {
ResolvableType returnType
= ResolvableType
.forMethodReturnType(method
);
ResolvableType candidate
= returnType
.as(FactoryBean
.class).getGeneric();
if (this.result
== ResolvableType
.NONE
) {
this.result
= candidate
;
}
else {
Class
<?> resolvedResult
= this.result
.resolve();
Class
<?> commonAncestor
= ClassUtils
.determineCommonAncestor(candidate
.resolve(), resolvedResult
);
if (!ObjectUtils
.nullSafeEquals(resolvedResult
, commonAncestor
)) {
this.result
= ResolvableType
.forClass(commonAncestor
);
}
}
}
}
private boolean isFactoryBeanMethod(Method method
) {
return (method
.getName().equals(this.factoryMethodName
) &&
FactoryBean
.class.isAssignableFrom(method
.getReturnType()));
}
ResolvableType
getResult() {
Class
<?> resolved
= this.result
.resolve();
boolean foundResult
= resolved
!= null
&& resolved
!= Object
.class;
return (foundResult
? this.result
: ResolvableType
.NONE
);
}
}
}