基本spring2.5版本
XmlBeanFactory是springIOC的基础类。
写了一个DEMO来列举XmlBeanFactory实现的接口。
@Test
public void testBeanInterFace() throws Exception{
Class<XmlBeanFactory> clazz = XmlBeanFactory.class;
Set<Class<?>> set = new HashSet<Class<?>>();
getAllInterFaces(clazz, set);
for (Class<?> class1 : set) {
System.out.println(class1);
}
}
private void getAllInterFaces(Class<?> clazz, Set<Class<?>> set) {
if(null == clazz) {
return;
}
if (clazz.equals(Object.class)) {
return;
}
if (clazz.isInterface()) {
set.add(clazz);
}
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> class1 : interfaces) {
getAllInterFaces(class1, set);
}
Class<?> superclass = clazz.getSuperclass();
getAllInterFaces(superclass, set);
}
输出如下:
interface org.springframework.beans.factory.BeanFactory
interface org.springframework.beans.factory.HierarchicalBeanFactory
interface org.springframework.beans.factory.ListableBeanFactory
interface org.springframework.beans.factory.config.ConfigurableListableBeanFactory
interface org.springframework.beans.factory.support.BeanDefinitionRegistry
interface org.springframework.beans.factory.config.ConfigurableBeanFactory
interface org.springframework.beans.factory.config.AutowireCapableBeanFactory
interface org.springframework.core.AliasRegistry
interface org.springframework.beans.factory.config.SingletonBeanRegistry
下面来分析下这些接口的作用。
BeanFactory
BeanFactory是springIOC中最重要的接口,这个接口主要提供了IOC容器的一些基本功能,例如获取bean,bean是否存在等。
常用方法如下:
Object getBean(String name) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
要求子类实现此接口时,必须满足如下功能:
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
1. BeanNameAware's setBeanName
2. BeanClassLoaderAware's setBeanClassLoader
3. BeanFactoryAware's setBeanFactory
4. ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)
5. ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)
6. MessageSourceAware's setMessageSource (only applicable when running in an application context)
7. ApplicationContextAware's setApplicationContext (only applicable when running in an application context)
8. ServletContextAware's setServletContext (only applicable when running in a web application context)
9. postProcessBeforeInitialization methods of BeanPostProcessors
10. InitializingBean's afterPropertiesSet
11. a custom init-method definition
12. postProcessAfterInitialization methods of BeanPostProcessors
On shutdown of a bean factory, the following lifecycle methods apply:
1. DisposableBean's destroy
2. a custom destroy-method definition
ListableBeanFactory
ListableBeanFactory时BeanFactory的子类,Listable的意思是可以列举的,就是可以遍历它所管理的bean。
这是一个很自私(专注)的接口,他只管理自己的bean,不会管理继承的bean。如果需要对这两者同时管理的话,需要使用BeanFactoryUtils中的方法。什么是继承bean?这个就是面向对象中的封装,即一个类A中的有另一个类B的引用,类A的某些功能是由类B来完成的。
ListableBeanFactory的主要接口如下:
boolean containsBeanDefinition(String beanName);
int getBeanDefinitionCount();
String[] getBeanDefinitionNames();
String[] getBeanNamesForType(Class type);
Map getBeansOfType(Class type) throws BeansException;
从以上接口的名字也可以理解Listable的含义:
可以获取(列举)所有的BeanDefinition(bean定义,spring是使用BeanDefinition来代表一个bean的,在第一次获取单例类的bean的时候会根据BeanDefinition来创建bean,对于applicationcontext类来讲,会自动初始化这些非延迟加载的单例类),可以获取(列举)某一类的bean。
AutowireCapableBeanFactory
自动装配基础接口,定义了如下常量和方法:
int AUTOWIRE_NO = 0;
int AUTOWIRE_BY_NAME = 1;
....
Object createBean(Class beanClass) throws BeansException;
void autowireBean(Object existingBean) throws BeansException;
Object configureBean(Object existingBean, String beanName) throws BeansException;
Object resolveDependency(DependencyDescriptor descriptor, 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;
主要是bean的初始化操作,例如执行init方法,各种aware接口的调用。
SingletonBeanRegistry
单例bean的注册接口,主要有以下方法:
void registerSingleton(String beanName, Object singletonObject);
Object getSingleton(String beanName);
boolean containsSingleton(String beanName);
String[] getSingletonNames();
int getSingletonCount();
HierarchicalBeanFactory
可以继承的beanfactory,即使用了面向对象中的封装模式(设计模式中的代理模式)。
主要提供了以下方法:
BeanFactory getParentBeanFactory();
boolean containsLocalBean(String name);
ConfigurableBeanFactory
可以配置的beanfactory,提供的方法比较多比较杂,主要是spring内部使用。
主要方法简单列举一二如下:
void setTempClassLoader(ClassLoader tempClassLoader);
// 我的理解是解析properties标签?TODO
void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar);
void registerCustomEditor(Class requiredType, Class propertyEditorClass);
void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);
// 处理scpoe标签
void registerScope(String scopeName, Scope scope);
void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException;
// 处理parent属性
BeanDefinition getMergedBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
// 被依赖的beans
String[] getDependentBeans(String beanName);
// 获取bean的依赖,即dependson属性
String[] getDependenciesForBean(String beanName);
ConfigurableListableBeanFactory
相对于ConfigurableBeanFactory而言,ConfigurableListableBeanFactory提供了用来分析和修改BeanDefinition的方法。
void registerResolvableDependency(Class dependencyType, Object autowiredValue);
boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
void freezeConfiguration();
void preInstantiateSingletons() throws BeansException;
AliasRegistry
别名注册器,主要方法:
void registerAlias(String name, String alias);
void removeAlias(String alias);
boolean isAlias(String beanName);
String[] getAliases(String name);
BeanDefinitionRegistry
这个接口提供了注册BeanDefinition的方法。主要方法有:
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException;
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;