准备在这种情况下使用的bean工厂
AbstractApplicationContext类的prepareBeanFactory(beanFactory)方法
/**
* 配置工厂的标准上下文特性,
* 如上下文的类装载器和后处理器。
* @param beanFactory the BeanFactory to configure
*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 告诉内部bean工厂使用上下文的类装载器等。
beanFactory.setBeanClassLoader(getClassLoader());
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// 上下文回调配置bean工厂。
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
// BeanFactory接口未登记为普通工厂的解析式。
// MessageSource注册(发现自动装配)作为一个bean。
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 发现LoadTimeWeaver准备编织,如果发现。
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 注册默认环境beans。
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
beanFactory.setBeanClassLoader(getClassLoader());
加载了当前线程spring使用所有的接口以及类,下面只显示一部分接口和类
class com.feiniu.springframework.test.TestAnimal,
interface org.springframework.core.env.EnvironmentCapable,
interface org.springframework.beans.factory.BeanFactory,
interface org.springframework.beans.factory.ListableBeanFactory,
interface org.springframework.beans.factory.HierarchicalBeanFactory,
interface org.springframework.context.MessageSource,
interface org.springframework.context.ApplicationEventPublisher,
interface org.springframework.core.io.ResourceLoader,
interface org.springframework.core.io.support.ResourcePatternResolver,
interface org.springframework.context.ApplicationContext,
interface org.springframework.beans.factory.Aware,
interface org.springframework.beans.factory.BeanNameAware,
interface org.springframework.beans.factory.InitializingBean,
interface org.springframework.context.Lifecycle,
interface org.springframework.context.ConfigurableApplicationContext,
interface org.springframework.beans.factory.DisposableBean,
class org.springframework.core.io.DefaultResourceLoader,
class org.springframework.context.support.AbstractApplicationContext,
class org.springframework.context.support.AbstractRefreshableApplicationContext,
class org.springframework.context.support.AbstractRefreshableConfigApplicationContext,
class org.springframework.context.support.AbstractXmlApplicationContext,
class org.springframework.context.support.FileSystemXmlApplicationContext,
interface org.springframework.core.io.InputStreamSource,
interface org.springframework.core.io.Resource
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
BeanExpressionResolver:通过计算一个表达式来解析为值的策略接口。
/**
* Strategy interface for resolving a value through evaluating it
* as an expression, if applicable.
*
* <p>A raw {@link org.springframework.beans.factory.BeanFactory} does not
* contain a default implementation of this strategy. However,
* {@link org.springframework.context.ApplicationContext} implementations
* will provide expression support out of the box.
*
* @author Juergen Hoeller
* @since 3.0
*/
public interface BeanExpressionResolver {
/**
* Evaluate the given value as an expression, if applicable;
* return the value as-is otherwise.
* @param value the value to check
* @param evalContext the evaluation context
* @return the resolved value (potentially the given value as-is)
* @throws BeansException if evaluation failed
*/
Object evaluate(String value, BeanExpressionContext evalContext) throws BeansException;
}
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
PropertyEditorRegistrar: 用一个属性编辑注册器(PropertyEditorRegistry)注册一个自定义的属性编辑器(PropertyEditor)的策略接口。
package org.springframework.beans;
/**
* Interface for strategies that register custom
* {@link java.beans.PropertyEditor property editors} with a
* {@link org.springframework.beans.PropertyEditorRegistry property editor registry}.
*
* <p>This is particularly useful when you need to use the same set of
* property editors in several different situations: write a corresponding
* registrar and reuse that in each case.
*
* @author Juergen Hoeller
* @since 1.2.6
* @see PropertyEditorRegistry
* @see java.beans.PropertyEditor
*/
public interface PropertyEditorRegistrar {
/**
* Register custom {@link java.beans.PropertyEditor PropertyEditors} with
* the given <code>PropertyEditorRegistry</code>.
* <p>The passed-in registry will usually be a {@link BeanWrapper} or a
* {@link org.springframework.validation.DataBinder DataBinder}.
* <p>It is expected that implementations will create brand new
* <code>PropertyEditors</code> instances for each invocation of this
* method (since <code>PropertyEditors</code> are not threadsafe).
* @param registry the <code>PropertyEditorRegistry</code> to register the
* custom <code>PropertyEditors</code> with
*/
void registerCustomEditors(PropertyEditorRegistry registry);
}
Spring中的后置处理器BeanPostProcessor
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
ignoreDependencyInterface的主要功能是忽略给定接口的自动装配功能,目的是:实现了BeanNameAware接口的属性,不会被Spring自动初始化。自动装配时忽略给定的依赖接口,典型应用是通过其他方式解析Application上下文注册依赖,类似于BeanFactory通过BeanFactoryAware进行注入或者ApplicationContext通过ApplicationContextAware进行注入。