标签:
先来看一下我们学习Spring时候的ABC代码:
BeanFactory beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager=(UserManager)beanFactory.getBean("UserManagerImpl"); userManager.add("dd","d");
通过加在含有bean配置关系的映射文件,get到我们的beanFactory容器,之后就可以从容器中get bean了。接着,我们将跟着这几行最简单代码,来深入search下spring是如何实现IOC 容器的。
先上源码:
public interface BeanFactory { /** * Used to dereference a FactoryBean and distinguish it from beans * <i>created</i> by the FactoryBean. For example, if the bean named * <code>myEjb</code> is a FactoryBean, getting <code>&myEjb</code> will * return the factory, not the instance returned by the factory. */ String FACTORY_BEAN_PREFIX = "&"; /** * Return an instance, which may be shared or independent, of the given bean name. * This method allows a Spring BeanFactory to be used as a replacement for the * Singleton or Prototype design pattern. * <p>Callers may retain references to returned objects in the case of Singleton beans. * <p>Translates aliases back to the corresponding canonical bean name. * Will ask the parent factory if the bean cannot be found in this factory instance. * @param name the name of the bean to return * @return the instance of the bean * @throws NoSuchBeanDefinitionException if there is no bean definition * with the specified name * @throws BeansException if the bean could not be obtained */ Object getBean(String name) throws BeansException; /** * Return an instance (possibly shared or independent) of the given bean name. * <p>Behaves the same as getBean(String), but provides a measure of type safety by * throwing a Spring BeansException if the bean is not of the required type. * This means that ClassCastException can‘t be thrown on casting the result correctly, * as can happen with <code>getBean(String)</code>. * @param name the name of the bean to return * @param requiredType type the bean must match. Can be an interface or superclass * of the actual class, or <code>null</code> for any match. For example, if the value * is <code>Object.class</code>, this method will succeed whatever the class of the * returned instance. * @return an instance of the bean (never <code>null</code>) * @throws BeanNotOfRequiredTypeException if the bean is not of the required type * @throws NoSuchBeanDefinitionException if there‘s no such bean definition * @throws BeansException if the bean could not be created */ Object getBean(String name, Class requiredType) throws BeansException; /** * Does this bean factory contain a bean definition with the given name? * <p>Will ask the parent factory if the bean cannot be found in this factory instance. * @param name the name of the bean to query * @return whether a bean with the given name is defined */ boolean containsBean(String name); /** * Is this bean a singleton? That is, will <code>getBean</code> always return the same object? * <p>Will ask the parent factory if the bean cannot be found in this factory instance. * @param name the name of the bean to query * @return is this bean a singleton * @throws NoSuchBeanDefinitionException if there is no bean with the given name * @see #getBean */ boolean isSingleton(String name) throws NoSuchBeanDefinitionException; /** * Determine the type of the bean with the given name. * More specifically, checks the type of object that <code>getBean</code> would return. * For a FactoryBean, returns the type of object that the FactoryBean creates. * @param name the name of the bean to query * @return the type of the bean, or <code>null</code> if not determinable * @throws NoSuchBeanDefinitionException if there is no bean with the given name * @since 1.1.2 * @see #getBean * @see FactoryBean#getObjectType() */ Class getType(String name) throws NoSuchBeanDefinitionException; /** * Return the aliases for the given bean name, if defined. * <p>If the given name is an alias, the corresponding original bean name * and other aliases (if any) will be returned, with the original bean name * being the first element in the array. * <p>Will ask the parent factory if the bean cannot be found in this factory instance. * @param name the bean name to check for aliases * @return the aliases, or an empty array if none */ String[] getAliases(String name); }
上面一段是从spring源码里面拽出来的,在BeanFactory里面,定义了Spring容器最基本的规范,下面我们来依次讲解下各个方法:
对FactoryBean的转义定义,因为如果使用bean的名字检索FactoryBean得到的对象是工厂生成的对象,如果需要得到工厂本身,需要转义;例如:myEjb是一个FactoryBean,&myEjb将会得到一个工厂,而不是工厂返回的实例。
关于BeanFacoty与FactoryBean: BeanFactory是个Factory,也就是IOC容器或对象工厂,FactoryBean是个Bean。在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器)来进行管理的。但对FactoryBean而言,这个Bean不是简单的Bean,而是一个能生产或者修饰对象生成的工厂Bean,它的实现与设计模式中的工厂模式和修饰器模式类似。(from :http://chenzehe.iteye.com/blog/1481476)
可以看出FactoryBean主要是为了简化复杂bean的装配而产生的。
根据bean的名字,获取在IOC容器中得到bean实例
根据了bean的名字和类型来获取bean实例,比上个方法多了类型条件验证,增强了类型安全验证机制。
根据名称查找是否容器中含有这个bean
判断bean是否为单例的。
得到bean类型的类类型,可能会抛出类不存在的异常。
得到bean的别名,如果根据别名检索,那么其原名也会被检索出来。
BeanFactory只是定义了容器的规范,spring为容器的实现提供了很多实现类,先来看看我们代码里面,从ClassPathXmlApplicationContext到BeanFactory经历了多少级:
ClassPathXmlApplication虽然提供了好几个构造函数,但是,最终还是会调用:
public ClassPathXmlApplicationContext(String[] paths, Class clazz, ApplicationContext parent) throws BeansException { super(parent); Assert.notNull(paths, "Path array must not be null"); Assert.notNull(clazz, "Class argument must not be null"); this.configResources = new Resource[paths.length]; for (int i = 0; i < paths.length; i++) { this.configResources[i] = new ClassPathResource(paths[i], clazz); } refresh(); }
构造函数里面,主要就是调用父类的构造函数,然后我们沿着继承链找:
之后的代码估计是加在配置资源,接着是refresh方法,这个方法的真正实现在AbstractApplicationContext这个抽象类中:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
this.startupTime = System.currentTimeMillis();
synchronized (this.activeMonitor) {
this.active = true;
}
// Tell subclass to refresh the internal bean factory.
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// Tell the internal bean factory to use the context‘s class loader.
beanFactory.setBeanClassLoader(getClassLoader());
// Populate the bean factory with context-specific resource editors.
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));
// Configure the bean factory with context semantics.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered with the context instance.
for (Iterator it = getBeanFactoryPostProcessors().iterator(); it.hasNext();) {
BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next();
factoryProcessor.postProcessBeanFactory(beanFactory);
}
if (logger.isInfoEnabled()) {
if (getBeanDefinitionCount() == 0) {
logger.info("No beans defined in application context [" + getDisplayName() + "]");
}
else {
logger.info(getBeanDefinitionCount() + " beans defined in application context [" + getDisplayName() + "]");
}
}
try {
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors();
// Register bean processors that intercept bean creation.
registerBeanPostProcessors();
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate singletons this late to allow them to access the message source.
beanFactory.preInstantiateSingletons();
// Last step: publish corresponding event.
publishEvent(new ContextRefreshedEvent(this));
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
beanFactory.destroySingletons();
throw ex;
}
}
}
睡觉去了,明天从refresh开始。。。
Spring源码解析——start from BeanFactory(一)
标签:
原文地址:http://blog.csdn.net/lhc1105/article/details/51347730