第三章 spring-bean之beanFactory系列(1)

前言

spring的基本問題,每次面試基本會問題。好比 - spring是什麼回答是ioc,aop,第三個就是beanfactory。 - spring使用什麼模式,100%的人回答是工廠模式。 - 這面的問題你們都知道。關於工廠模式,寫過的人都以爲很簡單。可是對於容器級的spring來講這個beanfactory就沒有那麼簡單了。接下來鳥菜啊爲你們深刻解讀beanfactory。java

類圖

須要實現的接口面試

  • AliasRegistry
  • BeanDefinitionRegistry
  • BeanFactory
  • SingletonBeanRegistry
  • HierarchicalBeanFactory
  • ConfigurableBeanFactory
  • ListableBeanFactory
  • AutowireCapableBeanFactory
  • ConfigurableListableBeanFactory

查看接口做用

AliasRegistry

public interface AliasRegistry {

	void registerAlias(String name, String alias);

	void removeAlias(String alias);

	boolean isAlias(String name);

	String[] getAliases(String name);
}

別名註冊,每一個bean都又一個beanName,也有多個alisa。可是從接口看來,name是key,alias是一個list。經過beanName查詢alias,沒法經過alias查詢bean。spring

BeanDefinitionRegistry

public interface BeanDefinitionRegistry extends AliasRegistry {

	void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException;

	void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

	BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

	boolean containsBeanDefinition(String beanName);

	String[] getBeanDefinitionNames();

	int getBeanDefinitionCount();

	boolean isBeanNameInUse(String beanName);
}

BeanDefinitionRegistry 繼承了AliasRegistry 接口。負責BeanDefinition的註冊與管理。app

BeanFactory

public interface BeanFactory {

	String FACTORY_BEAN_PREFIX = "&";

	Object getBean(String name) throws BeansException;

	<T> T getBean(String name, Class<T> requiredType) throws BeansException;

	Object getBean(String name, Object... args) throws BeansException;

	<T> T getBean(Class<T> requiredType) throws BeansException;

	<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

	boolean containsBean(String name);

	boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

	boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

	boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;

	boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;

	Class<?> getType(String name) throws NoSuchBeanDefinitionException;

	String[] getAliases(String name);
}

經過方法的聲明能夠看出BeanFactory負責經過beanName與class得到bean,經過beanName識別bean的一些特性。好比是不是單例,類型是什麼,別名又那些。ui

SingletonBeanRegistry

public interface SingletonBeanRegistry {

	void registerSingleton(String beanName, Object singletonObject);

	Object getSingleton(String beanName);

	boolean containsSingleton(String beanName);

	String[] getSingletonNames();

	int getSingletonCount();

	Object getSingletonMutex();

}

SingletonBeanRegistry 負責管理單例的bean。prototype

HierarchicalBeanFactory

public interface HierarchicalBeanFactory extends BeanFactory {

	BeanFactory getParentBeanFactory();

	boolean containsLocalBean(String name);

}

ConfigurableBeanFactory

public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {

	
	String SCOPE_SINGLETON = "singleton";

	String SCOPE_PROTOTYPE = "prototype";

	void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;

	void setBeanClassLoader(ClassLoader beanClassLoader);

	ClassLoader getBeanClassLoader();

	void setTempClassLoader(ClassLoader tempClassLoader);

	ClassLoader getTempClassLoader();

	void setCacheBeanMetadata(boolean cacheBeanMetadata);

	boolean isCacheBeanMetadata();

	void setBeanExpressionResolver(BeanExpressionResolver resolver);

	BeanExpressionResolver getBeanExpressionResolver();

	void setConversionService(ConversionService conversionService);

	ConversionService getConversionService();

	void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar);

	void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass);

	void copyRegisteredEditorsTo(PropertyEditorRegistry registry);

	void setTypeConverter(TypeConverter typeConverter);

	TypeConverter getTypeConverter();

	void addEmbeddedValueResolver(StringValueResolver valueResolver);

	boolean hasEmbeddedValueResolver();

	String resolveEmbeddedValue(String value);

	void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

	int getBeanPostProcessorCount();

	void registerScope(String scopeName, Scope scope);

	String[] getRegisteredScopeNames();

	Scope getRegisteredScope(String scopeName);

	AccessControlContext getAccessControlContext();

	void copyConfigurationFrom(ConfigurableBeanFactory otherFactory);

	void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException;

	void resolveAliases(StringValueResolver valueResolver);

	BeanDefinition getMergedBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

	boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException;

	void setCurrentlyInCreation(String beanName, boolean inCreation);

	boolean isCurrentlyInCreation(String beanName);

	void registerDependentBean(String beanName, String dependentBeanName);

	String[] getDependentBeans(String beanName);

	String[] getDependenciesForBean(String beanName);

	void destroyBean(String beanName, Object beanInstance);

	void destroyScopedBean(String beanName);

	void destroySingletons();
}

ConfigurableBeanFactory是beanFactory體系裏面最複雜的接口了。主要功能是管理beanFactory須要的組件。好比類加載器,父beanfactory等等。設計

public interface ListableBeanFactory extends BeanFactory {

	boolean containsBeanDefinition(String beanName);

	int getBeanDefinitionCount();

	String[] getBeanDefinitionNames();

	String[] getBeanNamesForType(ResolvableType type);

	String[] getBeanNamesForType(Class<?> type);

	String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);

	<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;

	<T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException;

	String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);

	Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;

	<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException;
}

從方法的聲明能夠看出ListableBeanFactorys接口是對BeanDefinitionRegistry,SingletonBeanRegistry,BeanFactory的補充。好比經過類型得到實現或者繼承該類型的beanName等等一些比較重要的方法code

AutowireCapableBeanFactory

public interface AutowireCapableBeanFactory extends BeanFactory {

	<T> T createBean(Class<T> beanClass) throws BeansException;

	void autowireBean(Object existingBean) throws BeansException;

	Object configureBean(Object existingBean, String beanName) throws BeansException;


	Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;

	Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;

	void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException;

	void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException;

	Object initializeBean(Object existingBean, String beanName) throws BeansException;

	Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException;

	Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException;

	void destroyBean(Object existingBean);

	<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException;

	Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName) throws BeansException;

	Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException;

}

從AutowireCapableBeanFactory接口的方法聲明能夠看出,主要的做用的手動建立bean。blog

ConfigurableListableBeanFactory

public interface ConfigurableListableBeanFactory extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {

	void ignoreDependencyType(Class<?> type);

	void ignoreDependencyInterface(Class<?> ifc);

	void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue);

	boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor) throws NoSuchBeanDefinitionException;

	BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

	Iterator<String> getBeanNamesIterator();

	void clearMetadataCache();

	void freezeConfiguration();

	boolean isConfigurationFrozen();

	void preInstantiateSingletons() throws BeansException;

}

從ConfigurableListableBeanFactory接口名就知道。ConfigurableListableBeanFactory是對ListableBeanFactory與ConfigurableBeanFactory的補充。方法的聲明也說明了是補充。繼承

接口的實現

接口已有的實現

  • SimpleAliasRegistry實現了AliasRegistry
  • DefaultSingletonBeanRegistry實現了SingletonBeanRegistry
  • FactoryBeanRegistrySupport 繼承了DefaultSingletonBeanRegistry
  • AbstractBeanFactory實現了ConfigurableBeanFactory,HierarchicalBeanFactory,BeanFactory
  • AbstractAutowireCapableBeanFactory實現了AutowireCapableBeanFactory
  • DefaultListableBeanFactory實現了ConfigurableListableBeanFactory,BeanDefinitionRegistry,ListableBeanFactory

從類圖看到,beanFactory的總體設計十分複雜。有得麻煩了。可是最終實現已有一個DefaultListableBeanFactory,只要深刻理解,研究DefaultListableBeanFactory就行了。接下來又六節用於解讀上面beanFactory中factory的六個實現類。

PS:工做與生活都十分忙碌,兩個多月沒有出博客。在此對給位讀者說抱歉。目前存稿仍是比較多的,工做方面基本進入正規,會陸續除一些源碼解讀博客寫出來。

相關文章
相關標籤/搜索