相关源码注释
Spring 5 SimpleAliasRegistry 源码注释 Spring 5 DefaultSingletonBeanRegistry 源码注释 Spring 5 FactoryBeanRegistrySupport 源码注释 Spring 5 AbstractBeanFactory 源码注释 Spring 5 AbstractAutowireCapableBeanFactory 源码注释 Spring 5 DefaultLisbaleBeanFactory 源码注释
UML 类图
源码
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
@Nullable
private BeanFactory parentBeanFactory
;
@Nullable
private ClassLoader beanClassLoader
= ClassUtils
.getDefaultClassLoader();
@Nullable
private ClassLoader tempClassLoader
;
private boolean cacheBeanMetadata
= true;
@Nullable
private BeanExpressionResolver beanExpressionResolver
;
@Nullable
private ConversionService conversionService
;
private final Set
<PropertyEditorRegistrar> propertyEditorRegistrars
= new LinkedHashSet<>(4);
private final Map
<Class
<?>, Class
<? extends PropertyEditor>> customEditors
= new HashMap<>(4);
@Nullable
private TypeConverter typeConverter
;
private final List
<StringValueResolver> embeddedValueResolvers
= new CopyOnWriteArrayList<>();
private final List
<BeanPostProcessor> beanPostProcessors
= new CopyOnWriteArrayList<>();
private volatile boolean hasInstantiationAwareBeanPostProcessors
;
private volatile boolean hasDestructionAwareBeanPostProcessors
;
private final Map
<String, Scope> scopes
= new LinkedHashMap<>(8);
@Nullable
private SecurityContextProvider securityContextProvider
;
private final Map
<String, RootBeanDefinition> mergedBeanDefinitions
= new ConcurrentHashMap<>(256);
private final Set
<String> alreadyCreated
= Collections
.newSetFromMap(new ConcurrentHashMap<>(256));
private final ThreadLocal
<Object> prototypesCurrentlyInCreation
=
new NamedThreadLocal<>("Prototype beans currently in creation");
public AbstractBeanFactory() {
}
public AbstractBeanFactory(@Nullable BeanFactory parentBeanFactory
) {
this.parentBeanFactory
= parentBeanFactory
;
}
@Override
public Object
getBean(String name
) throws BeansException
{
return doGetBean(name
, null
, null
, false);
}
@Override
public <T> T
getBean(String name
, Class
<T> requiredType
) throws BeansException
{
return doGetBean(name
, requiredType
, null
, false);
}
@Override
public Object
getBean(String name
, Object
... args
) throws BeansException
{
return doGetBean(name
, null
, args
, false);
}
public <T> T
getBean(String name
, @Nullable Class
<T> requiredType
, @Nullable Object
... args
)
throws BeansException
{
return doGetBean(name
, requiredType
, args
, false);
}
@SuppressWarnings("unchecked")
protected <T> T
doGetBean(final String name
, @Nullable final Class
<T> requiredType
,
@Nullable final Object
[] args
, boolean typeCheckOnly
) throws BeansException
{
final String beanName
= transformedBeanName(name
);
Object bean
;
Object sharedInstance
= getSingleton(beanName
);
if (sharedInstance
!= null
&& args
== null
) {
if (logger
.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName
)) {
logger
.trace("Returning eagerly cached instance of singleton bean '" + beanName
+
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger
.trace("Returning cached instance of singleton bean '" + beanName
+ "'");
}
}
bean
= getObjectForBeanInstance(sharedInstance
, name
, beanName
, null
);
}
else {
if (isPrototypeCurrentlyInCreation(beanName
)) {
throw new BeanCurrentlyInCreationException(beanName
);
}
BeanFactory parentBeanFactory
= getParentBeanFactory();
if (parentBeanFactory
!= null
&& !containsBeanDefinition(beanName
)) {
String nameToLookup
= originalBeanName(name
);
if (parentBeanFactory
instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory
) parentBeanFactory
).doGetBean(
nameToLookup
, requiredType
, args
, typeCheckOnly
);
}
else if (args
!= null
) {
return (T
) parentBeanFactory
.getBean(nameToLookup
, args
);
}
else if (requiredType
!= null
) {
return parentBeanFactory
.getBean(nameToLookup
, requiredType
);
}
else {
return (T
) parentBeanFactory
.getBean(nameToLookup
);
}
}
if (!typeCheckOnly
) {
markBeanAsCreated(beanName
);
}
try {
final RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
checkMergedBeanDefinition(mbd
, beanName
, args
);
String
[] dependsOn
= mbd
.getDependsOn();
if (dependsOn
!= null
) {
for (String dep
: dependsOn
) {
if (isDependent(beanName
, dep
)) {
throw new BeanCreationException(mbd
.getResourceDescription(), beanName
,
"Circular depends-on relationship between '" + beanName
+ "' and '" + dep
+ "'");
}
registerDependentBean(dep
, beanName
);
try {
getBean(dep
);
}
catch (NoSuchBeanDefinitionException ex
) {
throw new BeanCreationException(mbd
.getResourceDescription(), beanName
,
"'" + beanName
+ "' depends on missing bean '" + dep
+ "'", ex
);
}
}
}
if (mbd
.isSingleton()) {
sharedInstance
= getSingleton(beanName
, () -> {
try {
return createBean(beanName
, mbd
, args
);
}
catch (BeansException ex
) {
destroySingleton(beanName
);
throw ex
;
}
});
bean
= getObjectForBeanInstance(sharedInstance
, name
, beanName
, mbd
);
}
else if (mbd
.isPrototype()) {
Object prototypeInstance
= null
;
try {
beforePrototypeCreation(beanName
);
prototypeInstance
= createBean(beanName
, mbd
, args
);
}
finally {
afterPrototypeCreation(beanName
);
}
bean
= getObjectForBeanInstance(prototypeInstance
, name
, beanName
, mbd
);
}
else {
String scopeName
= mbd
.getScope();
final Scope scope
= this.scopes
.get(scopeName
);
if (scope
== null
) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName
+ "'");
}
try {
Object scopedInstance
= scope
.get(beanName
, () -> {
beforePrototypeCreation(beanName
);
try {
return createBean(beanName
, mbd
, args
);
}
finally {
afterPrototypeCreation(beanName
);
}
});
bean
= getObjectForBeanInstance(scopedInstance
, name
, beanName
, mbd
);
}
catch (IllegalStateException ex
) {
throw new BeanCreationException(beanName
,
"Scope '" + scopeName
+ "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex
);
}
}
}
catch (BeansException ex
) {
cleanupAfterBeanCreationFailure(beanName
);
throw ex
;
}
}
if (requiredType
!= null
&& !requiredType
.isInstance(bean
)) {
try {
T convertedBean
= getTypeConverter().convertIfNecessary(bean
, requiredType
);
if (convertedBean
== null
) {
throw new BeanNotOfRequiredTypeException(name
, requiredType
, bean
.getClass());
}
return convertedBean
;
}
catch (TypeMismatchException ex
) {
if (logger
.isTraceEnabled()) {
logger
.trace("Failed to convert bean '" + name
+ "' to required type '" +
ClassUtils
.getQualifiedName(requiredType
) + "'", ex
);
}
throw new BeanNotOfRequiredTypeException(name
, requiredType
, bean
.getClass());
}
}
return (T
) bean
;
}
@Override
public boolean containsBean(String name
) {
String beanName
= transformedBeanName(name
);
if (containsSingleton(beanName
) || containsBeanDefinition(beanName
)) {
return (!BeanFactoryUtils
.isFactoryDereference(name
) || isFactoryBean(name
));
}
BeanFactory parentBeanFactory
= getParentBeanFactory();
return (parentBeanFactory
!= null
&& parentBeanFactory
.containsBean(originalBeanName(name
)));
}
@Override
public boolean isSingleton(String name
) throws NoSuchBeanDefinitionException
{
String beanName
= transformedBeanName(name
);
Object beanInstance
= getSingleton(beanName
, false);
if (beanInstance
!= null
) {
if (beanInstance
instanceof FactoryBean) {
return (BeanFactoryUtils
.isFactoryDereference(name
) || ((FactoryBean
<?>) beanInstance
).isSingleton());
}
else {
return !BeanFactoryUtils
.isFactoryDereference(name
);
}
}
BeanFactory parentBeanFactory
= getParentBeanFactory();
if (parentBeanFactory
!= null
&& !containsBeanDefinition(beanName
)) {
return parentBeanFactory
.isSingleton(originalBeanName(name
));
}
RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
if (mbd
.isSingleton()) {
if (isFactoryBean(beanName
, mbd
)) {
if (BeanFactoryUtils
.isFactoryDereference(name
)) {
return true;
}
FactoryBean
<?> factoryBean
= (FactoryBean
<?>) getBean(FACTORY_BEAN_PREFIX
+ beanName
);
return factoryBean
.isSingleton();
}
else {
return !BeanFactoryUtils
.isFactoryDereference(name
);
}
}
else {
return false;
}
}
@Override
public boolean isPrototype(String name
) throws NoSuchBeanDefinitionException
{
String beanName
= transformedBeanName(name
);
BeanFactory parentBeanFactory
= getParentBeanFactory();
if (parentBeanFactory
!= null
&& !containsBeanDefinition(beanName
)) {
return parentBeanFactory
.isPrototype(originalBeanName(name
));
}
RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
if (mbd
.isPrototype()) {
return (!BeanFactoryUtils
.isFactoryDereference(name
) || isFactoryBean(beanName
, mbd
));
}
if (BeanFactoryUtils
.isFactoryDereference(name
)) {
return false;
}
if (isFactoryBean(beanName
, mbd
)) {
final FactoryBean
<?> fb
= (FactoryBean
<?>) getBean(FACTORY_BEAN_PREFIX
+ beanName
);
if (System
.getSecurityManager() != null
) {
return AccessController
.doPrivileged((PrivilegedAction
<Boolean>) () ->
((fb
instanceof SmartFactoryBean && ((SmartFactoryBean
<?>) fb
).isPrototype()) || !fb
.isSingleton()),
getAccessControlContext());
}
else {
return ((fb
instanceof SmartFactoryBean && ((SmartFactoryBean
<?>) fb
).isPrototype()) ||
!fb
.isSingleton());
}
}
else {
return false;
}
}
@Override
public boolean isTypeMatch(String name
, ResolvableType typeToMatch
) throws NoSuchBeanDefinitionException
{
return isTypeMatch(name
, typeToMatch
, true);
}
protected boolean isTypeMatch(String name
, ResolvableType typeToMatch
, boolean allowFactoryBeanInit
)
throws NoSuchBeanDefinitionException
{
String beanName
= transformedBeanName(name
);
boolean isFactoryDereference
= BeanFactoryUtils
.isFactoryDereference(name
);
Object beanInstance
= getSingleton(beanName
, false);
if (beanInstance
!= null
&& beanInstance
.getClass() != NullBean
.class) {
if (beanInstance
instanceof FactoryBean) {
if (!isFactoryDereference
) {
Class
<?> type
= getTypeForFactoryBean((FactoryBean
<?>) beanInstance
);
return (type
!= null
&& typeToMatch
.isAssignableFrom(type
));
}
else {
return typeToMatch
.isInstance(beanInstance
);
}
}
else if (!isFactoryDereference
) {
if (typeToMatch
.isInstance(beanInstance
)) {
return true;
}
else if (typeToMatch
.hasGenerics() && containsBeanDefinition(beanName
)) {
RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
Class
<?> targetType
= mbd
.getTargetType();
if (targetType
!= null
&& targetType
!= ClassUtils
.getUserClass(beanInstance
)) {
Class
<?> classToMatch
= typeToMatch
.resolve();
if (classToMatch
!= null
&& !classToMatch
.isInstance(beanInstance
)) {
return false;
}
if (typeToMatch
.isAssignableFrom(targetType
)) {
return true;
}
}
ResolvableType resolvableType
= mbd
.targetType
;
if (resolvableType
== null
) {
resolvableType
= mbd
.factoryMethodReturnType
;
}
return (resolvableType
!= null
&& typeToMatch
.isAssignableFrom(resolvableType
));
}
}
return false;
}
else if (containsSingleton(beanName
) && !containsBeanDefinition(beanName
)) {
return false;
}
BeanFactory parentBeanFactory
= getParentBeanFactory();
if (parentBeanFactory
!= null
&& !containsBeanDefinition(beanName
)) {
return parentBeanFactory
.isTypeMatch(originalBeanName(name
), typeToMatch
);
}
RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
BeanDefinitionHolder dbd
= mbd
.getDecoratedDefinition();
Class
<?> classToMatch
= typeToMatch
.resolve();
if (classToMatch
== null
) {
classToMatch
= FactoryBean
.class;
}
Class
<?>[] typesToMatch
= (FactoryBean
.class == classToMatch
?
new Class<?>[] {classToMatch
} : new Class<?>[] {FactoryBean
.class, classToMatch
});
Class
<?> predictedType
= null
;
if (!isFactoryDereference
&& dbd
!= null
&& isFactoryBean(beanName
, mbd
)) {
if (!mbd
.isLazyInit() || allowFactoryBeanInit
) {
RootBeanDefinition tbd
= getMergedBeanDefinition(dbd
.getBeanName(), dbd
.getBeanDefinition(), mbd
);
Class
<?> targetType
= predictBeanType(dbd
.getBeanName(), tbd
, typesToMatch
);
if (targetType
!= null
&& !FactoryBean
.class.isAssignableFrom(targetType
)) {
predictedType
= targetType
;
}
}
}
if (predictedType
== null
) {
predictedType
= predictBeanType(beanName
, mbd
, typesToMatch
);
if (predictedType
== null
) {
return false;
}
}
ResolvableType beanType
= null
;
if (FactoryBean
.class.isAssignableFrom(predictedType
)) {
if (beanInstance
== null
&& !isFactoryDereference
) {
beanType
= getTypeForFactoryBean(beanName
, mbd
, allowFactoryBeanInit
);
predictedType
= beanType
.resolve();
if (predictedType
== null
) {
return false;
}
}
}
else if (isFactoryDereference
) {
predictedType
= predictBeanType(beanName
, mbd
, FactoryBean
.class);
if (predictedType
== null
|| !FactoryBean
.class.isAssignableFrom(predictedType
)) {
return false;
}
}
if (beanType
== null
) {
ResolvableType definedType
= mbd
.targetType
;
if (definedType
== null
) {
definedType
= mbd
.factoryMethodReturnType
;
}
if (definedType
!= null
&& definedType
.resolve() == predictedType
) {
beanType
= definedType
;
}
}
if (beanType
!= null
) {
return typeToMatch
.isAssignableFrom(beanType
);
}
return typeToMatch
.isAssignableFrom(predictedType
);
}
@Override
public boolean isTypeMatch(String name
, Class
<?> typeToMatch
) throws NoSuchBeanDefinitionException
{
return isTypeMatch(name
, ResolvableType
.forRawClass(typeToMatch
));
}
@Override
@Nullable
public Class
<?> getType(String name
) throws NoSuchBeanDefinitionException
{
return getType(name
, true);
}
@Override
@Nullable
public Class
<?> getType(String name
, boolean allowFactoryBeanInit
) throws NoSuchBeanDefinitionException
{
String beanName
= transformedBeanName(name
);
Object beanInstance
= getSingleton(beanName
, false);
if (beanInstance
!= null
&& beanInstance
.getClass() != NullBean
.class) {
if (beanInstance
instanceof FactoryBean && !BeanFactoryUtils
.isFactoryDereference(name
)) {
return getTypeForFactoryBean((FactoryBean
<?>) beanInstance
);
}
else {
return beanInstance
.getClass();
}
}
BeanFactory parentBeanFactory
= getParentBeanFactory();
if (parentBeanFactory
!= null
&& !containsBeanDefinition(beanName
)) {
return parentBeanFactory
.getType(originalBeanName(name
));
}
RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
BeanDefinitionHolder dbd
= mbd
.getDecoratedDefinition();
if (dbd
!= null
&& !BeanFactoryUtils
.isFactoryDereference(name
)) {
RootBeanDefinition tbd
= getMergedBeanDefinition(dbd
.getBeanName(), dbd
.getBeanDefinition(), mbd
);
Class
<?> targetClass
= predictBeanType(dbd
.getBeanName(), tbd
);
if (targetClass
!= null
&& !FactoryBean
.class.isAssignableFrom(targetClass
)) {
return targetClass
;
}
}
Class
<?> beanClass
= predictBeanType(beanName
, mbd
);
if (beanClass
!= null
&& FactoryBean
.class.isAssignableFrom(beanClass
)) {
if (!BeanFactoryUtils
.isFactoryDereference(name
)) {
return getTypeForFactoryBean(beanName
, mbd
, allowFactoryBeanInit
).resolve();
}
else {
return beanClass
;
}
}
else {
return (!BeanFactoryUtils
.isFactoryDereference(name
) ? beanClass
: null
);
}
}
@Override
public String
[] getAliases(String name
) {
String beanName
= transformedBeanName(name
);
List
<String> aliases
= new ArrayList<>();
boolean factoryPrefix
= name
.startsWith(FACTORY_BEAN_PREFIX
);
String fullBeanName
= beanName
;
if (factoryPrefix
) {
fullBeanName
= FACTORY_BEAN_PREFIX
+ beanName
;
}
if (!fullBeanName
.equals(name
)) {
aliases
.add(fullBeanName
);
}
String
[] retrievedAliases
= super.getAliases(beanName
);
for (String retrievedAlias
: retrievedAliases
) {
String alias
= (factoryPrefix
? FACTORY_BEAN_PREFIX
: "") + retrievedAlias
;
if (!alias
.equals(name
)) {
aliases
.add(alias
);
}
}
if (!containsSingleton(beanName
) && !containsBeanDefinition(beanName
)) {
BeanFactory parentBeanFactory
= getParentBeanFactory();
if (parentBeanFactory
!= null
) {
aliases
.addAll(Arrays
.asList(parentBeanFactory
.getAliases(fullBeanName
)));
}
}
return StringUtils
.toStringArray(aliases
);
}
@Override
@Nullable
public BeanFactory
getParentBeanFactory() {
return this.parentBeanFactory
;
}
@Override
public boolean containsLocalBean(String name
) {
String beanName
= transformedBeanName(name
);
return ((containsSingleton(beanName
) || containsBeanDefinition(beanName
)) &&
(!BeanFactoryUtils
.isFactoryDereference(name
) || isFactoryBean(beanName
)));
}
@Override
public void setParentBeanFactory(@Nullable BeanFactory parentBeanFactory
) {
if (this.parentBeanFactory
!= null
&& this.parentBeanFactory
!= parentBeanFactory
) {
throw new IllegalStateException("Already associated with parent BeanFactory: " + this.parentBeanFactory
);
}
this.parentBeanFactory
= parentBeanFactory
;
}
@Override
public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader
) {
this.beanClassLoader
= (beanClassLoader
!= null
? beanClassLoader
: ClassUtils
.getDefaultClassLoader());
}
@Override
@Nullable
public ClassLoader
getBeanClassLoader() {
return this.beanClassLoader
;
}
@Override
public void setTempClassLoader(@Nullable ClassLoader tempClassLoader
) {
this.tempClassLoader
= tempClassLoader
;
}
@Override
@Nullable
public ClassLoader
getTempClassLoader() {
return this.tempClassLoader
;
}
@Override
public void setCacheBeanMetadata(boolean cacheBeanMetadata
) {
this.cacheBeanMetadata
= cacheBeanMetadata
;
}
@Override
public boolean isCacheBeanMetadata() {
return this.cacheBeanMetadata
;
}
@Override
public void setBeanExpressionResolver(@Nullable BeanExpressionResolver resolver
) {
this.beanExpressionResolver
= resolver
;
}
@Override
@Nullable
public BeanExpressionResolver
getBeanExpressionResolver() {
return this.beanExpressionResolver
;
}
@Override
public void setConversionService(@Nullable ConversionService conversionService
) {
this.conversionService
= conversionService
;
}
@Override
@Nullable
public ConversionService
getConversionService() {
return this.conversionService
;
}
@Override
public void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar
) {
Assert
.notNull(registrar
, "PropertyEditorRegistrar must not be null");
this.propertyEditorRegistrars
.add(registrar
);
}
public Set
<PropertyEditorRegistrar> getPropertyEditorRegistrars() {
return this.propertyEditorRegistrars
;
}
@Override
public void registerCustomEditor(Class
<?> requiredType
, Class
<? extends PropertyEditor> propertyEditorClass
) {
Assert
.notNull(requiredType
, "Required type must not be null");
Assert
.notNull(propertyEditorClass
, "PropertyEditor class must not be null");
this.customEditors
.put(requiredType
, propertyEditorClass
);
}
@Override
public void copyRegisteredEditorsTo(PropertyEditorRegistry registry
) {
registerCustomEditors(registry
);
}
public Map
<Class
<?>, Class
<? extends PropertyEditor>> getCustomEditors() {
return this.customEditors
;
}
@Override
public void setTypeConverter(TypeConverter typeConverter
) {
this.typeConverter
= typeConverter
;
}
@Nullable
protected TypeConverter
getCustomTypeConverter() {
return this.typeConverter
;
}
@Override
public TypeConverter
getTypeConverter() {
TypeConverter customConverter
= getCustomTypeConverter();
if (customConverter
!= null
) {
return customConverter
;
}
else {
SimpleTypeConverter typeConverter
= new SimpleTypeConverter();
typeConverter
.setConversionService(getConversionService());
registerCustomEditors(typeConverter
);
return typeConverter
;
}
}
@Override
public void addEmbeddedValueResolver(StringValueResolver valueResolver
) {
Assert
.notNull(valueResolver
, "StringValueResolver must not be null");
this.embeddedValueResolvers
.add(valueResolver
);
}
@Override
public boolean hasEmbeddedValueResolver() {
return !this.embeddedValueResolvers
.isEmpty();
}
@Override
@Nullable
public String
resolveEmbeddedValue(@Nullable String value
) {
if (value
== null
) {
return null
;
}
String result
= value
;
for (StringValueResolver resolver
: this.embeddedValueResolvers
) {
result
= resolver
.resolveStringValue(result
);
if (result
== null
) {
return null
;
}
}
return result
;
}
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor
) {
Assert
.notNull(beanPostProcessor
, "BeanPostProcessor must not be null");
this.beanPostProcessors
.remove(beanPostProcessor
);
if (beanPostProcessor
instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors
= true;
}
if (beanPostProcessor
instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors
= true;
}
this.beanPostProcessors
.add(beanPostProcessor
);
}
@Override
public int getBeanPostProcessorCount() {
return this.beanPostProcessors
.size();
}
public List
<BeanPostProcessor> getBeanPostProcessors() {
return this.beanPostProcessors
;
}
protected boolean hasInstantiationAwareBeanPostProcessors() {
return this.hasInstantiationAwareBeanPostProcessors
;
}
protected boolean hasDestructionAwareBeanPostProcessors() {
return this.hasDestructionAwareBeanPostProcessors
;
}
@Override
public void registerScope(String scopeName
, Scope scope
) {
Assert
.notNull(scopeName
, "Scope identifier must not be null");
Assert
.notNull(scope
, "Scope must not be null");
if (SCOPE_SINGLETON
.equals(scopeName
) || SCOPE_PROTOTYPE
.equals(scopeName
)) {
throw new IllegalArgumentException("Cannot replace existing scopes 'singleton' and 'prototype'");
}
Scope previous
= this.scopes
.put(scopeName
, scope
);
if (previous
!= null
&& previous
!= scope
) {
if (logger
.isDebugEnabled()) {
logger
.debug("Replacing scope '" + scopeName
+ "' from [" + previous
+ "] to [" + scope
+ "]");
}
}
else {
if (logger
.isTraceEnabled()) {
logger
.trace("Registering scope '" + scopeName
+ "' with implementation [" + scope
+ "]");
}
}
}
@Override
public String
[] getRegisteredScopeNames() {
return StringUtils
.toStringArray(this.scopes
.keySet());
}
@Override
@Nullable
public Scope
getRegisteredScope(String scopeName
) {
Assert
.notNull(scopeName
, "Scope identifier must not be null");
return this.scopes
.get(scopeName
);
}
public void setSecurityContextProvider(SecurityContextProvider securityProvider
) {
this.securityContextProvider
= securityProvider
;
}
@Override
public AccessControlContext
getAccessControlContext() {
return (this.securityContextProvider
!= null
?
this.securityContextProvider
.getAccessControlContext() :
AccessController
.getContext());
}
@Override
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory
) {
Assert
.notNull(otherFactory
, "BeanFactory must not be null");
setBeanClassLoader(otherFactory
.getBeanClassLoader());
setCacheBeanMetadata(otherFactory
.isCacheBeanMetadata());
setBeanExpressionResolver(otherFactory
.getBeanExpressionResolver());
setConversionService(otherFactory
.getConversionService());
if (otherFactory
instanceof AbstractBeanFactory) {
AbstractBeanFactory otherAbstractFactory
= (AbstractBeanFactory
) otherFactory
;
this.propertyEditorRegistrars
.addAll(otherAbstractFactory
.propertyEditorRegistrars
);
this.customEditors
.putAll(otherAbstractFactory
.customEditors
);
this.typeConverter
= otherAbstractFactory
.typeConverter
;
this.beanPostProcessors
.addAll(otherAbstractFactory
.beanPostProcessors
);
this.hasInstantiationAwareBeanPostProcessors
= this.hasInstantiationAwareBeanPostProcessors
||
otherAbstractFactory
.hasInstantiationAwareBeanPostProcessors
;
this.hasDestructionAwareBeanPostProcessors
= this.hasDestructionAwareBeanPostProcessors
||
otherAbstractFactory
.hasDestructionAwareBeanPostProcessors
;
this.scopes
.putAll(otherAbstractFactory
.scopes
);
this.securityContextProvider
= otherAbstractFactory
.securityContextProvider
;
}
else {
setTypeConverter(otherFactory
.getTypeConverter());
String
[] otherScopeNames
= otherFactory
.getRegisteredScopeNames();
for (String scopeName
: otherScopeNames
) {
this.scopes
.put(scopeName
, otherFactory
.getRegisteredScope(scopeName
));
}
}
}
@Override
public BeanDefinition
getMergedBeanDefinition(String name
) throws BeansException
{
String beanName
= transformedBeanName(name
);
if (!containsBeanDefinition(beanName
) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory
) getParentBeanFactory()).getMergedBeanDefinition(beanName
);
}
return getMergedLocalBeanDefinition(beanName
);
}
@Override
public boolean isFactoryBean(String name
) throws NoSuchBeanDefinitionException
{
String beanName
= transformedBeanName(name
);
Object beanInstance
= getSingleton(beanName
, false);
if (beanInstance
!= null
) {
return (beanInstance
instanceof FactoryBean);
}
if (!containsBeanDefinition(beanName
) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory
) getParentBeanFactory()).isFactoryBean(name
);
}
return isFactoryBean(beanName
, getMergedLocalBeanDefinition(beanName
));
}
@Override
public boolean isActuallyInCreation(String beanName
) {
return (isSingletonCurrentlyInCreation(beanName
) || isPrototypeCurrentlyInCreation(beanName
));
}
protected boolean isPrototypeCurrentlyInCreation(String beanName
) {
Object curVal
= this.prototypesCurrentlyInCreation
.get();
return (curVal
!= null
&&
(curVal
.equals(beanName
) || (curVal
instanceof Set && ((Set
<?>) curVal
).contains(beanName
))));
}
@SuppressWarnings("unchecked")
protected void beforePrototypeCreation(String beanName
) {
Object curVal
= this.prototypesCurrentlyInCreation
.get();
if (curVal
== null
) {
this.prototypesCurrentlyInCreation
.set(beanName
);
}
else if (curVal
instanceof String) {
Set
<String> beanNameSet
= new HashSet<>(2);
beanNameSet
.add((String
) curVal
);
beanNameSet
.add(beanName
);
this.prototypesCurrentlyInCreation
.set(beanNameSet
);
}
else {
Set
<String> beanNameSet
= (Set
<String>) curVal
;
beanNameSet
.add(beanName
);
}
}
@SuppressWarnings("unchecked")
protected void afterPrototypeCreation(String beanName
) {
Object curVal
= this.prototypesCurrentlyInCreation
.get();
if (curVal
instanceof String) {
this.prototypesCurrentlyInCreation
.remove();
}
else if (curVal
instanceof Set) {
Set
<String> beanNameSet
= (Set
<String>) curVal
;
beanNameSet
.remove(beanName
);
if (beanNameSet
.isEmpty()) {
this.prototypesCurrentlyInCreation
.remove();
}
}
}
@Override
public void destroyBean(String beanName
, Object beanInstance
) {
destroyBean(beanName
, beanInstance
, getMergedLocalBeanDefinition(beanName
));
}
protected void destroyBean(String beanName
, Object bean
, RootBeanDefinition mbd
) {
new DisposableBeanAdapter(bean
, beanName
, mbd
, getBeanPostProcessors(), getAccessControlContext()).destroy();
}
@Override
public void destroyScopedBean(String beanName
) {
RootBeanDefinition mbd
= getMergedLocalBeanDefinition(beanName
);
if (mbd
.isSingleton() || mbd
.isPrototype()) {
throw new IllegalArgumentException(
"Bean name '" + beanName
+ "' does not correspond to an object in a mutable scope");
}
String scopeName
= mbd
.getScope();
Scope scope
= this.scopes
.get(scopeName
);
if (scope
== null
) {
throw new IllegalStateException("No Scope SPI registered for scope name '" + scopeName
+ "'");
}
Object bean
= scope
.remove(beanName
);
if (bean
!= null
) {
destroyBean(beanName
, bean
, mbd
);
}
}
protected String
transformedBeanName(String name
) {
return canonicalName(BeanFactoryUtils
.transformedBeanName(name
));
}
protected String
originalBeanName(String name
) {
String beanName
= transformedBeanName(name
);
if (name
.startsWith(FACTORY_BEAN_PREFIX
)) {
beanName
= FACTORY_BEAN_PREFIX
+ beanName
;
}
return beanName
;
}
protected void initBeanWrapper(BeanWrapper bw
) {
bw
.setConversionService(getConversionService());
registerCustomEditors(bw
);
}
protected void registerCustomEditors(PropertyEditorRegistry registry
) {
PropertyEditorRegistrySupport registrySupport
=
(registry
instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport
) registry
: null
);
if (registrySupport
!= null
) {
registrySupport
.useConfigValueEditors();
}
if (!this.propertyEditorRegistrars
.isEmpty()) {
for (PropertyEditorRegistrar registrar
: this.propertyEditorRegistrars
) {
try {
registrar
.registerCustomEditors(registry
);
}
catch (BeanCreationException ex
) {
Throwable rootCause
= ex
.getMostSpecificCause();
if (rootCause
instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce
= (BeanCreationException
) rootCause
;
String bceBeanName
= bce
.getBeanName();
if (bceBeanName
!= null
&& isCurrentlyInCreation(bceBeanName
)) {
if (logger
.isDebugEnabled()) {
logger
.debug("PropertyEditorRegistrar [" + registrar
.getClass().getName() +
"] failed because it tried to obtain currently created bean '" +
ex
.getBeanName() + "': " + ex
.getMessage());
}
onSuppressedException(ex
);
continue;
}
}
throw ex
;
}
}
}
if (!this.customEditors
.isEmpty()) {
this.customEditors
.forEach((requiredType
, editorClass
) ->
registry
.registerCustomEditor(requiredType
, BeanUtils
.instantiateClass(editorClass
)));
}
}
protected RootBeanDefinition
getMergedLocalBeanDefinition(String beanName
) throws BeansException
{
RootBeanDefinition mbd
= this.mergedBeanDefinitions
.get(beanName
);
if (mbd
!= null
&& !mbd
.stale
) {
return mbd
;
}
return getMergedBeanDefinition(beanName
, getBeanDefinition(beanName
));
}
protected RootBeanDefinition
getMergedBeanDefinition(String beanName
, BeanDefinition bd
)
throws BeanDefinitionStoreException
{
return getMergedBeanDefinition(beanName
, bd
, null
);
}
protected RootBeanDefinition
getMergedBeanDefinition(
String beanName
, BeanDefinition bd
, @Nullable BeanDefinition containingBd
)
throws BeanDefinitionStoreException
{
synchronized (this.mergedBeanDefinitions
) {
RootBeanDefinition mbd
= null
;
RootBeanDefinition previous
= null
;
if (containingBd
== null
) {
mbd
= this.mergedBeanDefinitions
.get(beanName
);
}
if (mbd
== null
|| mbd
.stale
) {
previous
= mbd
;
mbd
= null
;
if (bd
.getParentName() == null
) {
if (bd
instanceof RootBeanDefinition) {
mbd
= ((RootBeanDefinition
) bd
).cloneBeanDefinition();
}
else {
mbd
= new RootBeanDefinition(bd
);
}
}
else {
BeanDefinition pbd
;
try {
String parentBeanName
= transformedBeanName(bd
.getParentName());
if (!beanName
.equals(parentBeanName
)) {
pbd
= getMergedBeanDefinition(parentBeanName
);
}
else {
BeanFactory parent
= getParentBeanFactory();
if (parent
instanceof ConfigurableBeanFactory) {
pbd
= ((ConfigurableBeanFactory
) parent
).getMergedBeanDefinition(parentBeanName
);
}
else {
throw new NoSuchBeanDefinitionException(parentBeanName
,
"Parent name '" + parentBeanName
+ "' is equal to bean name '" + beanName
+
"': cannot be resolved without an AbstractBeanFactory parent");
}
}
}
catch (NoSuchBeanDefinitionException ex
) {
throw new BeanDefinitionStoreException(bd
.getResourceDescription(), beanName
,
"Could not resolve parent bean definition '" + bd
.getParentName() + "'", ex
);
}
mbd
= new RootBeanDefinition(pbd
);
mbd
.overrideFrom(bd
);
}
if (!StringUtils
.hasLength(mbd
.getScope())) {
mbd
.setScope(RootBeanDefinition
.SCOPE_SINGLETON
);
}
if (containingBd
!= null
&& !containingBd
.isSingleton() && mbd
.isSingleton()) {
mbd
.setScope(containingBd
.getScope());
}
if (containingBd
== null
&& isCacheBeanMetadata()) {
this.mergedBeanDefinitions
.put(beanName
, mbd
);
}
}
if (previous
!= null
) {
copyRelevantMergedBeanDefinitionCaches(previous
, mbd
);
}
return mbd
;
}
}
private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous
, RootBeanDefinition mbd
) {
if (ObjectUtils
.nullSafeEquals(mbd
.getBeanClassName(), previous
.getBeanClassName()) &&
ObjectUtils
.nullSafeEquals(mbd
.getFactoryBeanName(), previous
.getFactoryBeanName()) &&
ObjectUtils
.nullSafeEquals(mbd
.getFactoryMethodName(), previous
.getFactoryMethodName())) {
ResolvableType targetType
= mbd
.targetType
;
ResolvableType previousTargetType
= previous
.targetType
;
if (targetType
== null
|| targetType
.equals(previousTargetType
)) {
mbd
.targetType
= previousTargetType
;
mbd
.isFactoryBean
= previous
.isFactoryBean
;
mbd
.resolvedTargetType
= previous
.resolvedTargetType
;
mbd
.factoryMethodReturnType
= previous
.factoryMethodReturnType
;
mbd
.factoryMethodToIntrospect
= previous
.factoryMethodToIntrospect
;
}
}
}
protected void checkMergedBeanDefinition(RootBeanDefinition mbd
, String beanName
, @Nullable Object
[] args
)
throws BeanDefinitionStoreException
{
if (mbd
.isAbstract()) {
throw new BeanIsAbstractException(beanName
);
}
}
protected void clearMergedBeanDefinition(String beanName
) {
RootBeanDefinition bd
= this.mergedBeanDefinitions
.get(beanName
);
if (bd
!= null
) {
bd
.stale
= true;
}
}
public void clearMetadataCache() {
this.mergedBeanDefinitions
.forEach((beanName
, bd
) -> {
if (!isBeanEligibleForMetadataCaching(beanName
)) {
bd
.stale
= true;
}
});
}
@Nullable
protected Class
<?> resolveBeanClass(final RootBeanDefinition mbd
, String beanName
, final Class
<?>... typesToMatch
)
throws CannotLoadBeanClassException
{
try {
if (mbd
.hasBeanClass()) {
return mbd
.getBeanClass();
}
if (System
.getSecurityManager() != null
) {
return AccessController
.doPrivileged((PrivilegedExceptionAction
<Class
<?>>) () ->
doResolveBeanClass(mbd
, typesToMatch
), getAccessControlContext());
}
else {
return doResolveBeanClass(mbd
, typesToMatch
);
}
}
catch (PrivilegedActionException pae
) {
ClassNotFoundException ex
= (ClassNotFoundException
) pae
.getException();
throw new CannotLoadBeanClassException(mbd
.getResourceDescription(), beanName
, mbd
.getBeanClassName(), ex
);
}
catch (ClassNotFoundException ex
) {
throw new CannotLoadBeanClassException(mbd
.getResourceDescription(), beanName
, mbd
.getBeanClassName(), ex
);
}
catch (LinkageError err
) {
throw new CannotLoadBeanClassException(mbd
.getResourceDescription(), beanName
, mbd
.getBeanClassName(), err
);
}
}
@Nullable
private Class
<?> doResolveBeanClass(RootBeanDefinition mbd
, Class
<?>... typesToMatch
)
throws ClassNotFoundException
{
ClassLoader beanClassLoader
= getBeanClassLoader();
ClassLoader dynamicLoader
= beanClassLoader
;
boolean freshResolve
= false;
if (!ObjectUtils
.isEmpty(typesToMatch
)) {
ClassLoader tempClassLoader
= getTempClassLoader();
if (tempClassLoader
!= null
) {
dynamicLoader
= tempClassLoader
;
freshResolve
= true;
if (tempClassLoader
instanceof DecoratingClassLoader) {
DecoratingClassLoader dcl
= (DecoratingClassLoader
) tempClassLoader
;
for (Class
<?> typeToMatch
: typesToMatch
) {
dcl
.excludeClass(typeToMatch
.getName());
}
}
}
}
String className
= mbd
.getBeanClassName();
if (className
!= null
) {
Object evaluated
= evaluateBeanDefinitionString(className
, mbd
);
if (!className
.equals(evaluated
)) {
if (evaluated
instanceof Class) {
return (Class
<?>) evaluated
;
}
else if (evaluated
instanceof String) {
className
= (String
) evaluated
;
freshResolve
= true;
}
else {
throw new IllegalStateException("Invalid class name expression result: " + evaluated
);
}
}
if (freshResolve
) {
if (dynamicLoader
!= null
) {
try {
return dynamicLoader
.loadClass(className
);
}
catch (ClassNotFoundException ex
) {
if (logger
.isTraceEnabled()) {
logger
.trace("Could not load class [" + className
+ "] from " + dynamicLoader
+ ": " + ex
);
}
}
}
return ClassUtils
.forName(className
, dynamicLoader
);
}
}
return mbd
.resolveBeanClass(beanClassLoader
);
}
@Nullable
protected Object
evaluateBeanDefinitionString(@Nullable String value
, @Nullable BeanDefinition beanDefinition
) {
if (this.beanExpressionResolver
== null
) {
return value
;
}
Scope scope
= null
;
if (beanDefinition
!= null
) {
String scopeName
= beanDefinition
.getScope();
if (scopeName
!= null
) {
scope
= getRegisteredScope(scopeName
);
}
}
return this.beanExpressionResolver
.evaluate(value
, new BeanExpressionContext(this, scope
));
}
@Nullable
protected Class
<?> predictBeanType(String beanName
, RootBeanDefinition mbd
, Class
<?>... typesToMatch
) {
Class
<?> targetType
= mbd
.getTargetType();
if (targetType
!= null
) {
return targetType
;
}
if (mbd
.getFactoryMethodName() != null
) {
return null
;
}
return resolveBeanClass(mbd
, beanName
, typesToMatch
);
}
protected boolean isFactoryBean(String beanName
, RootBeanDefinition mbd
) {
Boolean result
= mbd
.isFactoryBean
;
if (result
== null
) {
Class
<?> beanType
= predictBeanType(beanName
, mbd
, FactoryBean
.class);
result
= (beanType
!= null
&& FactoryBean
.class.isAssignableFrom(beanType
));
mbd
.isFactoryBean
= result
;
}
return result
;
}
protected ResolvableType
getTypeForFactoryBean(String beanName
, RootBeanDefinition mbd
, boolean allowInit
) {
ResolvableType result
= getTypeForFactoryBeanFromAttributes(mbd
);
if (result
!= ResolvableType
.NONE
) {
return result
;
}
if (allowInit
&& mbd
.isSingleton()) {
try {
FactoryBean
<?> factoryBean
= doGetBean(FACTORY_BEAN_PREFIX
+ beanName
, FactoryBean
.class, null
, true);
Class
<?> objectType
= getTypeForFactoryBean(factoryBean
);
return (objectType
!= null
) ? ResolvableType
.forClass(objectType
) : ResolvableType
.NONE
;
}
catch (BeanCreationException ex
) {
if (ex
.contains(BeanCurrentlyInCreationException
.class)) {
logger
.trace(LogMessage
.format("Bean currently in creation on FactoryBean type check: %s", ex
));
}
else if (mbd
.isLazyInit()) {
logger
.trace(LogMessage
.format("Bean creation exception on lazy FactoryBean type check: %s", ex
));
}
else {
logger
.debug(LogMessage
.format("Bean creation exception on non-lazy FactoryBean type check: %s", ex
));
}
onSuppressedException(ex
);
}
}
return ResolvableType
.NONE
;
}
ResolvableType
getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes
) {
Object attribute
= attributes
.getAttribute(FactoryBean
.OBJECT_TYPE_ATTRIBUTE
);
if (attribute
instanceof ResolvableType) {
return (ResolvableType
) attribute
;
}
if (attribute
instanceof Class) {
return ResolvableType
.forClass((Class
<?>) attribute
);
}
return ResolvableType
.NONE
;
}
@Nullable
@Deprecated
protected Class
<?> getTypeForFactoryBean(String beanName
, RootBeanDefinition mbd
) {
return getTypeForFactoryBean(beanName
, mbd
, true).resolve();
}
protected void markBeanAsCreated(String beanName
) {
if (!this.alreadyCreated
.contains(beanName
)) {
synchronized (this.mergedBeanDefinitions
) {
if (!this.alreadyCreated
.contains(beanName
)) {
clearMergedBeanDefinition(beanName
);
this.alreadyCreated
.add(beanName
);
}
}
}
}
protected void cleanupAfterBeanCreationFailure(String beanName
) {
synchronized (this.mergedBeanDefinitions
) {
this.alreadyCreated
.remove(beanName
);
}
}
protected boolean isBeanEligibleForMetadataCaching(String beanName
) {
return this.alreadyCreated
.contains(beanName
);
}
protected boolean removeSingletonIfCreatedForTypeCheckOnly(String beanName
) {
if (!this.alreadyCreated
.contains(beanName
)) {
removeSingleton(beanName
);
return true;
}
else {
return false;
}
}
protected boolean hasBeanCreationStarted() {
return !this.alreadyCreated
.isEmpty();
}
protected Object
getObjectForBeanInstance(
Object beanInstance
, String name
, String beanName
, @Nullable RootBeanDefinition mbd
) {
if (BeanFactoryUtils
.isFactoryDereference(name
)) {
if (beanInstance
instanceof NullBean) {
return beanInstance
;
}
if (!(beanInstance
instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(beanName
, beanInstance
.getClass());
}
if (mbd
!= null
) {
mbd
.isFactoryBean
= true;
}
return beanInstance
;
}
if (!(beanInstance
instanceof FactoryBean)) {
return beanInstance
;
}
Object object
= null
;
if (mbd
!= null
) {
mbd
.isFactoryBean
= true;
}
else {
object
= getCachedObjectForFactoryBean(beanName
);
}
if (object
== null
) {
FactoryBean
<?> factory
= (FactoryBean
<?>) beanInstance
;
if (mbd
== null
&& containsBeanDefinition(beanName
)) {
mbd
= getMergedLocalBeanDefinition(beanName
);
}
boolean synthetic
= (mbd
!= null
&& mbd
.isSynthetic());
object
= getObjectFromFactoryBean(factory
, beanName
, !synthetic
);
}
return object
;
}
public boolean isBeanNameInUse(String beanName
) {
return isAlias(beanName
) || containsLocalBean(beanName
) || hasDependentBean(beanName
);
}
protected boolean requiresDestruction(Object bean
, RootBeanDefinition mbd
) {
return (bean
.getClass() != NullBean
.class &&
(DisposableBeanAdapter
.hasDestroyMethod(bean
, mbd
) || (hasDestructionAwareBeanPostProcessors() &&
DisposableBeanAdapter
.hasApplicableProcessors(bean
, getBeanPostProcessors()))));
}
protected void registerDisposableBeanIfNecessary(String beanName
, Object bean
, RootBeanDefinition mbd
) {
AccessControlContext acc
= (System
.getSecurityManager() != null
? getAccessControlContext() : null
);
if (!mbd
.isPrototype() && requiresDestruction(bean
, mbd
)) {
if (mbd
.isSingleton()) {
registerDisposableBean(beanName
,
new DisposableBeanAdapter(bean
, beanName
, mbd
, getBeanPostProcessors(), acc
));
}
else {
Scope scope
= this.scopes
.get(mbd
.getScope());
if (scope
== null
) {
throw new IllegalStateException("No Scope registered for scope name '" + mbd
.getScope() + "'");
}
scope
.registerDestructionCallback(beanName
,
new DisposableBeanAdapter(bean
, beanName
, mbd
, getBeanPostProcessors(), acc
));
}
}
}
protected abstract boolean containsBeanDefinition(String beanName
);
protected abstract BeanDefinition
getBeanDefinition(String beanName
) throws BeansException
;
protected abstract Object
createBean(String beanName
, RootBeanDefinition mbd
, @Nullable Object
[] args
)
throws BeanCreationException
;
}