spring源码阅读——3.spring-context

包含如下模块:cache(略)、context、jmx、remoting(略)、scheduling、scripting(略)等等。

 

JMX模块

查看org.springframework.jmx.export.annotation包下的注解,会使用即可。不作具体分析。

 

context模块

applicationContext接口:

applicationContext实现的接口:通过EnvironmentCapable访问系统环境参数、通过ListableBeanFactory和HierarchicalBeanFactory作为bean容器、通过MessageSource提供资源国际化、通过ApplicationEventPublisher提供applicationContext事件机制、通过ResourcePatternResolver实现资源加载和解析。

 

applicationContext实现类 的层次图:

左边一列是非web环境下的applicationContext实现,最终提供了2种加载xml的实现方式:ClassPathXmlApplicationContext、FileSystemXmlApplicationContext;

右边是web环境下的applicationContext实现,提供了注解配置和xml配置这两种实现:AnnotationConfigWebApplicationContext、XmlWebApplicationContext。

 

GenericApplicationContext:


GenericApplicationContext与其他上上图中其他的ApplicationContext实现所不同的是:

1.它并没有指定加载Bean的途径和方式(xml、注解等),而是在构造器中直接拿到beanFactory。之后可以通过XmlBeanDefinitionReader等等方式加载bean了。这样GenericApplicationContext就提供了load bean的灵活度。一般情况下还是建议使用ClassPathXmlApplicationContext or FileSystemXmlApplicationContext更为方便。

2.refresh()仅能在load bean定义之后调用一次。再次调用会抛异常。

AnnotationConfigApplicationContext提供了2个有用的构造器:public  AnnotationConfigApplicationContext(Class<?>... annotatedClasses)直接指定带注解的bean class,从而加载该bean;public AnnotationConfigApplicationContext(String... basePackages)指定扫描的类路径,使用ClassPathBeanDefinitionScanner扫描该路径下 带注解的bean class。

 

GenericXmlApplicationContext通过手动指定一个或多个xml文件路径来加载bean,最后调用refresh()。功能上等同于ClassPathXmlApplicationContext and FileSystemXmlApplicationContext。

StaticApplicationContext提供手工编程注册bean和Message,而不是从外部配置资源中读取。它及其子类主要用于测试环境,一般不用于生产环境。

applicationContext的生命周期的实现机制:


任意bean都可以实现Lifecycle或SmartLifecycle接口和Phased接口(可选),以用来控制bean和beanFactory的生命周期。

bean factory start()的时候,对实现Lifecycle的所有bean,根据Phase进行分组(没有实现Phase接口则为0)然后按照从小到大的顺序来分组启动。

bean factory close()或stop()的时候,则按逆序分组并关闭这些bean。

对每个bean的启动/关闭,需要先递归启动该bean依赖的其他bean,最后再启动/关闭该bean。

ApplicationContext的事件触发机制:

AbstractApplicationEventMulticaster负责实现listener的管理。在 getApplicationListeners(ApplicationEvent event)中实现了对listener访问的线程安全性和并发保证;并支持对listener执行先后的排序(只需要listener实现Order接口即可)。

spring实现了4种事件:ContextClosedEvent、ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent。也可以自定义事件。

---------------org.springframework.context.annotation---------------

提供了几个注解:Configuration、ComponentScan、Bean、PropertySource、ImportResource、Import、Scope、Primary、Role、Profile、DependsOn、Lazy、EnableMBeanExport、EnableLoadTimeWeaving、EnableAspectJAutoProxy。用法参考注解各自的接口注释。

Configuration注解的class,是通过CGLIB增强,重写了@Bean方法,当容器需要构建这个bean的实例的时候,才会调用原先的方法。(参见ConfigurationClassEnhancer.BeanMethodInterceptor.java)

---------------org.springframework.context.config--------------

 用于解析 与以上注解一一对应的xml配置,并做相应处理。

scheduling模块

---------------org.springframework.scheduling.annotation-------------

注解:EnableScheduling、EnableAsync、Scheduled、Async

@Configuration注解的class:AbstractAsyncConfiguration、ProxyAsyncConfiguration、SchedulingConfiguration

提供的BeanPostProcessor:

AsyncAnnotationBeanPostProcessor用于扫描Async注解的函数,以AOP注入的方式调用executor异步执行(AnnotationAsyncExecutionInterceptor);

ScheduledAnnotationBeanPostProcessor扫描Scheduled注解的函数,把它们作为任务注册到ScheduledTaskRegistrar中,设置scheduler,最后调用registrar.afterPropertiesSet()来schedule这些任务。

---------------org.springframework.scheduling.concurrent--------------


上图中,ExecutorConfigurationSupport封装了对Executor的所有配置项(即其中的属性),包括使用的线程工厂、线程名字、rejectedExecutionHandler、关闭机制、等待终止的时间等等。

ThreadPoolTaskScheduler和ThreadPoolTaskScheduler,调用ScheduledExecutorService来实现一次调度、固定频率调度、固定延迟调度,但是对于周期性cron调度则要调用ReschedulingRunnable和CronTrigger。PeriodicTrigger提供了固定频率调度、固定延迟调度的功能。

----------------------org.springframework.scheduling.config--------------------

对xml配置方式的解析,不作具体分析。

----------------------org.springframework.scheduling.support----------------------

CronTrigger,实现Trigger接口,结合CronSequenceGenerator计算任务的下次执行时间。

SimpleTriggerContext中保存了schedule trigger任务的上下文数据(见接口TriggerContext)。

----------------------org.springframework.stereotype----------------------

提供了几类组件注解:Component、Controller、Repository、Service

猜你喜欢

转载自hepeng19861212.iteye.com/blog/2244235