Spring5.0源码深度解析之容器的功能扩展
文章目录

版权所有:https://my.oschina.net/u/3995125,禁止转载
容器的功能扩展
前期准备:
同样,我们还是以 AnnotationConfigApplicationContext 作为切入点,开始对整体功能进行分析。
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); register(annotatedClasses); refresh(); }
一:创建IOC容器
进入this();
public AnnotationConfigApplicationContext() { this.reader = new AnnotatedBeanDefinitionReader(this); this.scanner = new ClassPathBeanDefinitionScanner(this); }
先执行父类的初始化方法,创建IOC容器
public GenericApplicationContext() { this.beanFactory = new DefaultListableBeanFactory(); }

2.进入AnnotatedBeanDefinitionReader方法执行初始化方法创建BeanDefinition读取器
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) { Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); Assert.notNull(environment, "Environment must not be null"); this.registry = registry; this.conditionEvaluator = new ConditionEvaluator(registry, environment, null); AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry); }
AnnotationConfigUtils获取所有BeanPostProcessor的Bean,把下面6个对象注入到IOC容器中了

这里用到了CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME 、AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME 等几个常量
常量
对应的BeanPostProcessor
对应的注解
CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME
ConfigurationClassPostProcessor
@Configuration
AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME
AutowiredAnnotationBeanPostProcessor
@AutoWired
REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME
RequiredAnnotationBeanPostProcessor
@Required
COMMON_ANNOTATION_PROCESSOR_BEAN_NAME
CommonAnnotationBeanPostProcessor
@javax.annotation.PostConstruct、@javax.annotation.PreDestroy
等等,还有几个,就不列了。
3.进入ClassPathBeanDefinitionScanner方法创建classPath下扫描器
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, @Nullable ResourceLoader resourceLoader) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null"); this.registry = registry;
if (useDefaultFilters) { registerDefaultFilters(); } setEnvironment(environment); setResourceLoader(resourceLoader); }

二:注册配置类
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); register(annotatedClasses); refresh(); }
public void register(Class<?>... annotatedClasses) { Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified"); this.reader.register(annotatedClasses); }
public void register(Class... annotatedClasses) { for (Class annotatedClass : annotatedClasses) { registerBean(annotatedClass); } }

public void registerBean(Class<?> annotatedClass) { doRegisterBean(annotatedClass, null, null, null); }

void doRegisterBean(Class annotatedClass, @Nullable Supplier instanceSupplier, @Nullable String name, @Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);//创建BeanDefinition对象 if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) { //处理 @Condition注解 return; }
abd.setInstanceSupplier(instanceSupplier); //设置对象是单例模式还是多例模式,默认单例 ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd); abd.setScope(scopeMetadata.getScopeName()); //获取BeanName,设置的化就采用默认值,否则类名第一个字母小写 String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd); //处理Lazy,primary等注解 .....
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);//判断对象是否需要代理,不需要直接返回,需要的化,重新创建BeanDefinition加入代理的信息 BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry); //注册配置类到IOC容器 }

扩展功能
进入 refresh()方法,可以说refresh函数中包括了几乎ApplicationContext中提供的全部功能,而且此函数中逻辑非常清晰明了。
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); register(annotatedClasses); refresh(); }
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh();//准备刷新上下文环境
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = **obtainFreshBeanFactory**();**//初始化BeanFacgtory**
// Prepare the bean factory for use in this context.
**prepareBeanFactory**(beanFactory);**//初始化BeanFactory进行各种功能填充**
try {
// Allows post-processing of the bean factory in context subclasses.
**postProcessBeanFactory**(beanFactory);**//子类覆盖方法做额外的处理**
// Invoke factory processors registered as beans in the context.
**invokeBeanFactoryPostProcessors**(beanFactory);**//激活各种BeanFactory处理器**
// Register bean processors that intercept bean creation.
**registerBeanPostProcessors**(beanFactory);**//注册拦截Bean创建的Bean处理器,这里只是注册,真正的调用是在getBean的时候**
// Initialize message source for this context.
**initMessageSource**();**//为上下文初始化Message源,集不同语言的消息体,国际化处理**
// Initialize event multicaster for this context.
**initApplicationEventMulticaster**();**//初始化应用消息广播器,并放入applicationEventMulticaster bean中**
// Initialize other special beans in specific context subclasses.
**onRefresh**();**//留给子类来初始化其它的Bean**
// Check for listener beans and register them.
**registerListeners**();**//在所有注册的Bean中查找Listener bean,注册到消息广播器中**
// Instantiate all remaining (non-lazy-init) singletons.
**finishBeanFactoryInitialization**(beanFactory);**//初始化剩下的单实例(非惰性)**
// Last step: publish corresponding event.
**finishRefresh**();**//完成刷新过程,通知生命周期处理器 LifecycleProcessor刷新过程,同时发出 ContextRefreshedEvent 通知别人**
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
**destroyBeans**();**//销毁Bean**
// Reset 'active' flag.
**cancelRefresh**(ex);**//更改活跃状态**
// Propagate exception to caller.
throw ex;
}
} }
准备刷新上下文环境
prepareRefresh();函数主要作用是对系统属性及环境变量的初始化及验证
/** * Prepare this context for refreshing, setting its startup date and * active flag as well as performing any initialization of property sources. */ protected void prepareRefresh() { this.startupDate = System.currentTimeMillis(); this.closed.set(false); this.active.set(true);
if (logger.isInfoEnabled()) { logger.info("Refreshing " + this); }
// Initialize any placeholder property sources in the context environment initPropertySources();//留给子类覆盖
// Validate that all properties marked as required are resolvable // see ConfigurablePropertyResolver#setRequiredProperties getEnvironment().validateRequiredProperties();//验证需要的属性文件是否都已经放入环境中去了
// Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available... this.earlyApplicationEvents = new LinkedHashSet<>(); }
initPropertySources是空的,没有任何逻辑处理, 而getEnvironment().validateRequiredProperties()也是因为没有需要验证的属性而没有做任何处理,然而这两个方法作用还是很大的。那么该怎么用呢?
1.initPropertySources正符合Spring的开放式结构设计,给用户最大的扩展Spring的能力。
2.validateRequiredProperties则是对属性进行验证,那么如何验证呢?我们举个例子来说明下:
public class MyAnnotationConfigApplication extends AnnotationConfigApplicationContext { public MyAnnotationConfigApplication(Class<?>... annotatedClasses){ super(annotatedClasses); } @Override protected void initPropertySources(){ //添加验证要求 getEnvironment().setRequiredProperties("VAR"); } }
我们自定义了继承自AnnotationConfigApplicationContext的MyAnnotationConfigApplication,并重写了initPropertySources方法,在方法中添加了我们的个性化需求,那么在验证的时候也就是程序走到
validateRequiredProperties()代码的时候,我们还需要替换掉原有的AnnotationConfigApplicationContext为我们自定义的****MyAnnotationConfigApplication。
public class Test002 { public static void main(String[] args) { ApplicationContext context = new MyAnnotationConfigApplication(MyConfig.class); Object payEntity = context.getBean("payEntity"); System.out.println(payEntity); } }
如果系统并没有检测到对应的VAR环境变量,那么就会抛出异常:
初始化BeanFacgtory
obtainFreshBeanFactory()方法的作用是:经过这个方法,ApplicationContext就已经拥有了BeanFactory的全部功能。
/** * Tell the subclass to refresh the internal bean factory. * @return the fresh BeanFactory instance * @see #refreshBeanFactory() * @see #getBeanFactory() */ protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { //初始化BeanFactory refreshBeanFactory(); //返回当前实体的BeanFactory属性 ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }
方法中将核心实现委托给了refreshBeanFactory:
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory();//创建DefaultListableBeanFactory beanFactory.setSerializationId(getId());//序列化id customizeBeanFactory(beanFactory);//定制BeanFactory,设置相关属性,是否允许循环依赖,是否允许覆盖 loadBeanDefinitions(beanFactory);//加载BeanDefinition synchronized (this.beanFactoryMonitor) { ** this.beanFactory = beanFactory**; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
1.定制BeanFactory
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { //如果属性不为空,则设置给beanFactory对象相应属性,此属性的含义是:是否允许覆盖同名称的不同定义的对象 if (this.allowBeanDefinitionOverriding != null) { beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } //如果属性不为空,设置给beanFactory对象相应的属性,这块属性的含义是:是否允许Bean之间存在循环依赖 if (this.allowCircularReferences != null) { beanFactory.setAllowCircularReferences(this.allowCircularReferences); } }
对于允许覆盖和允许循环依赖的设置这里只是进行了非空判断,如果不为空的化要进行设置,但是这里没有设置,所以还是那个套路,使用子类覆盖方法。
public class MyAnnotationConfigApplication extends AnnotationConfigApplicationContext { public MyAnnotationConfigApplication(Class<?>... annotatedClasses){ super(annotatedClasses); } protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory){ super.setAllowBeanDefinitionOverriding(false);//是否允许覆盖同名称的不同定义的对象 super.setAllowCircularReferences(false);//是否允许Bean之间存在循环依赖 } }
2.加载BeanDefinition
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);//为指定的beanFactory创建XmlBeanDefinitionReader
// Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(this.getEnvironment());//对beanDefinitionReader进行环境变量的设置 beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader);//进行设置可以覆盖 loadBeanDefinitions(beanDefinitionReader); }
在初始化了DefaultListableBeanFactory和XmlBeanDefinitionReader后就可以进行配置文件的读取了
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException { Resource[] configResources = getConfigResources(); if (configResources != null) { reader.loadBeanDefinitions(configResources); } String[] configLocations = getConfigLocations(); if (configLocations != null) { reader.loadBeanDefinitions(configLocations); } }
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException { Assert.notNull(resources, "Resource array must not be null"); int counter = 0; for (Resource resource : resources) { counter += loadBeanDefinitions(resource); } return counter; }
经过此步骤,类型DefaultListableBeanFactory的变量beanFactory已经包含了所有已经解析好的配置
功能扩展
进入函数prepareBeanFactory前,Spring已经完成了对配置的解析,而ApplicationContext在功能上的扩展也由此展开。
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // Tell the internal bean factory to use the context's class loader etc. beanFactory.setBeanClassLoader(getClassLoader());//设置BeanFactory的classLoader为当前context的classLoader beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));//设置beanFactory的表达式语言处理器,默认可以使用#{bean.xx}的形式来调用相关的属性值。 beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));//为BeanFactory增加了一个默认的propertyEditor,这个主要是对bean的属性等设置管理的一个工具
// Configure the bean factory with context callbacks. beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));**//添加BeanPostProcessor
//设置了几个忽略自定装配的接口** beanFactory.ignoreDependencyInterface(EnvironmentAware.class); beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
//设置了几个自定装配的规则 beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// Register early post-processor for detecting inner beans as ApplicationListeners. beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// 增加了对AspectJ的支持 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())); }
// 添加了默认的系统环境bean 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()); } }
上面函数中主要进行了几个方面的扩展
- 增加了对SpEL语言的支持
- 增加对属性编辑器的支持
- 增加了对一些内置类,比如实现了Aware接口的类信息注入
- 设置了依赖功能可忽略接口
- 注册一些固定依赖的属性
- 增加了AspectJ的支持,后面再去分析
- 将相关环境变量及属性注册以单例模式注册
上面可扩展功能很多,我就不一一介绍了,下面我们来挑几个我们感兴趣的函数来分析下:
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)),其主要目的就是注册个BeanPostProcessor,而真正的逻辑还是在ApplicationContextAwareProcessor中

ApplicationContextAwareProcessor实现了BeanPostProcessor接口,我们回顾下之前讲解的内容,在Bean实例化的时候,也就是Spring激活Bean的init-method方法前后,
会调用BeanPostProcessor的postProcessBeforeInitialization和postProcessAfterInitialization方法。所以我们也就去看看ApplicationContextAwareProcessor对这两个方法的实现。
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { return bean; }
对于后置处理器BeanPostProcessor的后置处理方法中并没有做任何逻辑处理,那么我们转向后置处理器的前置处理
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { AccessControlContext acc = null;
if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); }
if (acc != null) { AccessController.doPrivileged((PrivilegedAction

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论