📄 abstractbeanfactory.java
字号:
return getParentBeanFactory().isSingleton(name);
}
RootBeanDefinition bd = getMergedBeanDefinition(beanName, false);
if (bd.hasBeanClass()) {
beanClass = bd.getBeanClass();
}
singleton = bd.isSingleton();
}
// In case of FactoryBean, return singleton status of created object if not a dereference.
if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass) &&
!isFactoryDereference(name)) {
FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
return factoryBean.isSingleton();
}
return singleton;
}
public Class getType(String name) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name);
try {
Class beanClass = null;
// Check manually registered singletons.
Object beanInstance = null;
synchronized (this.singletonCache) {
beanInstance = this.singletonCache.get(beanName);
}
if (beanInstance != null) {
beanClass = beanInstance.getClass();
}
else {
// No singleton instance found -> check bean definition.
if (getParentBeanFactory() != null && !containsBeanDefinition(beanName)) {
// No bean definition found in this factory -> delegate to parent.
return getParentBeanFactory().getType(name);
}
RootBeanDefinition mergedBeanDefinition = getMergedBeanDefinition(beanName, false);
// Delegate to getTypeForFactoryMethod in case of factory method.
if (mergedBeanDefinition.getFactoryMethodName() != null) {
return getTypeForFactoryMethod(name, mergedBeanDefinition);
}
// Return "undeterminable" for beans without class.
if (!mergedBeanDefinition.hasBeanClass()) {
return null;
}
beanClass = mergedBeanDefinition.getBeanClass();
}
// Check bean class whether we're dealing with a FactoryBean.
if (FactoryBean.class.isAssignableFrom(beanClass) && !isFactoryDereference(name)) {
// If it's a FactoryBean, we want to look at what it creates, not the factory class.
FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
return factoryBean.getObjectType();
}
return beanClass;
}
catch (BeanCreationException ex) {
if (ex.contains(BeanCurrentlyInCreationException.class) ||
ex.contains(FactoryBeanNotInitializedException.class)) {
// Can only happen when checking a FactoryBean.
logger.debug("Ignoring BeanCreationException on FactoryBean type check", ex);
return null;
}
throw ex;
}
}
public String[] getAliases(String name) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name);
// Check if bean actually exists in this bean factory.
if (containsSingleton(beanName) || containsBeanDefinition(beanName)) {
// If found, gather aliases.
List aliases = new ArrayList();
synchronized (this.aliasMap) {
for (Iterator it = this.aliasMap.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
if (entry.getValue().equals(beanName)) {
aliases.add(entry.getKey());
}
}
}
return StringUtils.toStringArray(aliases);
}
else {
// Not found -> check parent.
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null) {
return parentBeanFactory.getAliases(name);
}
throw new NoSuchBeanDefinitionException(beanName, toString());
}
}
//---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory interface
//---------------------------------------------------------------------
public BeanFactory getParentBeanFactory() {
return parentBeanFactory;
}
public boolean containsLocalBean(String name) {
String beanName = transformedBeanName(name);
return (containsSingleton(beanName) || containsBeanDefinition(beanName));
}
//---------------------------------------------------------------------
// Implementation of ConfigurableBeanFactory interface
//---------------------------------------------------------------------
public void setParentBeanFactory(BeanFactory parentBeanFactory) {
if (this.parentBeanFactory != null && this.parentBeanFactory != parentBeanFactory) {
throw new IllegalStateException("Already associated with parent BeanFactory: " + this.parentBeanFactory);
}
this.parentBeanFactory = parentBeanFactory;
}
public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
Assert.notNull(requiredType, "Required type must not be null");
Assert.notNull(propertyEditor, "PropertyEditor must not be null");
this.customEditors.put(requiredType, propertyEditor);
}
/**
* Return the map of custom editors, with Classes as keys
* and PropertyEditors as values.
*/
public Map getCustomEditors() {
return this.customEditors;
}
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
this.beanPostProcessors.add(beanPostProcessor);
if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
}
public int getBeanPostProcessorCount() {
return this.beanPostProcessors.size();
}
/**
* Return the list of BeanPostProcessors that will get applied
* to beans created with this factory.
*/
public List getBeanPostProcessors() {
return this.beanPostProcessors;
}
/**
* Return whether this factory holds a DestructionAwareBeanPostProcessor
* that will get applied to singleton beans on shutdown.
* @see #addBeanPostProcessor
* @see org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor
*/
protected boolean hasDestructionAwareBeanPostProcessors() {
return this.hasDestructionAwareBeanPostProcessors;
}
public void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.hasText(alias, "Alias must not be empty");
if (!alias.equals(beanName)) {
// Only actually register the alias if it is not equal to the bean name itself.
if (logger.isDebugEnabled()) {
logger.debug("Registering alias '" + alias + "' for bean with name '" + beanName + "'");
}
synchronized (this.aliasMap) {
Object registeredName = this.aliasMap.get(alias);
if (registeredName != null && !registeredName.equals(beanName)) {
throw new BeanDefinitionStoreException("Cannot register alias '" + alias + "' for bean name '" +
beanName + "': It's already registered for bean name '" + registeredName + "'.");
}
this.aliasMap.put(alias, beanName);
}
}
}
public void registerSingleton(String beanName, Object singletonObject) throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(singletonObject, "Singleton object must not be null");
synchronized (this.singletonCache) {
Object oldObject = this.singletonCache.get(beanName);
if (oldObject != null) {
throw new BeanDefinitionStoreException("Could not register object [" + singletonObject +
"] under bean name '" + beanName + "': there's already object [" + oldObject + " bound");
}
addSingleton(beanName, singletonObject);
}
}
/**
* Add the given singleton object to the singleton cache of this factory.
* <p>To be called for eager registration of singletons, e.g. to be able to
* resolve circular references.
* @param beanName the name of the bean
* @param singletonObject the singleton object
*/
protected void addSingleton(String beanName, Object singletonObject) {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(singletonObject, "Singleton object must not be null");
synchronized (this.singletonCache) {
this.singletonCache.put(beanName, singletonObject);
}
}
/**
* Remove the bean with the given name from the singleton cache of this factory.
* <p>To be able to clean up eager registration of a singleton if creation failed.
* @param beanName the name of the bean
*/
protected void removeSingleton(String beanName) {
Assert.hasText(beanName, "Bean name must not be empty");
synchronized (this.singletonCache) {
this.singletonCache.remove(beanName);
}
}
/**
* Return the number of beans in the singleton cache.
* <p>Does not consider any hierarchy this factory may participate in.
*/
public int getSingletonCount() {
synchronized (this.singletonCache) {
return this.singletonCache.size();
}
}
/**
* Return the names of beans in the singleton cache.
* <p>Does not consider any hierarchy this factory may participate in.
*/
public String[] getSingletonNames() {
synchronized (this.singletonCache) {
return StringUtils.toStringArray(this.singletonCache.keySet());
}
}
/**
* Return whether the specified singleton is currently in creation
* @param beanName the name of the bean
*/
protected boolean isSingletonCurrentlyInCreation(String beanName) {
return this.currentlyInCreation.contains(beanName);
}
public boolean containsSingleton(String beanName) {
Assert.hasText(beanName, "Bean name must not be empty");
synchronized (this.singletonCache) {
return this.singletonCache.containsKey(beanName);
}
}
public void destroySingletons() {
if (logger.isInfoEnabled()) {
logger.info("Destroying singletons in factory {" + this + "}");
}
synchronized (this.singletonCache) {
synchronized (this.disposableBeans) {
String[] disposableBeanNames = StringUtils.toStringArray(this.disposableBeans.keySet());
for (int i = 0; i < disposableBeanNames.length; i++) {
destroyDisposableBean(disposableBeanNames[i]);
}
}
this.singletonCache.clear();
}
}
//---------------------------------------------------------------------
// Implementation methods
//---------------------------------------------------------------------
/**
* Return whether the given name is a factory dereference
* (beginning with the factory dereference prefix).
* @see BeanFactory#FACTORY_BEAN_PREFIX
* @see org.springframework.beans.factory.BeanFactoryUtils#isFactoryDereference
*/
protected boolean isFactoryDereference(String name) {
return BeanFactoryUtils.isFactoryDereference(name);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -