`
hepeng19861212
  • 浏览: 146901 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

    博客分类:
  • java
 
阅读更多

包含如下模块: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

 

 

  • 大小: 147.7 KB
  • 大小: 305.9 KB
  • 大小: 113.1 KB
  • 大小: 95.4 KB
  • 大小: 379.9 KB
  • 大小: 428.6 KB
分享到:
评论

相关推荐

    开发工具 spring-context-4.3.6.RELEASE

    开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring...

    spring-context-3.1.0.RELEASE-sources.jar

    spring-context-3.1.0的源码包直接导入就OK,或者用jd-gui工具查看

    spring3源码集合

    spring-context-3.1.3.RELEASE-sources.jar spring-context-support-3.1.3.RELEASE-sources.jar spring-core-2.5.6-sources.jar spring-core-3.2.0.RELEASE-sources.jar spring-expression-3.1.3.RELEASE-sources....

    spring-framework-5.1.8.RELEASE-dist.zip

    Spring最新所有Jar包、文档、源码,包括spring-aop-5.1.8.RELEASE、spring-aspects-5.1.8.RELEASE、spring-beans-5.1.8.RELEASE、spring-context-5.1.8.RELEASE、spring-context-indexer-5.1.8.RELEASE、spring-...

    spring3源码

    spring-context-3.2.3.RELEASE-sources.jar;spring-context-support-3.2.3.RELEASE-sources.jar;spring-core-3.2.3.RELEASE-sources.jar;spring-data-commons-1.5.1.RELEASE-sources.jar;spring-data-jpa-1.3.2....

    spring-context-5.3.7-API文档-中文版.zip

    赠送jar包:spring-context-5.3.7.jar; 赠送原API文档:spring-context-5.3.7-javadoc.jar; 赠送源代码:spring-context-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-context-5.3.7.pom; 包含翻译后的API...

    spring-context-4.2.4.RELEASE-sources.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

    spring-context-5.3.6.jar

    这个 jar 文件为 Spring 核心提供了大量扩展。可以找到使用 Spring ApplicationContext 特 性时所需的全部类, JDNI 所需的全部类, UI 方面的用来与模板 (Templating) 引擎如 Velocity、 FreeMarker、...

    spring-context-support-4.2.4.RELEASE-sources.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

    基于SpringMVC+Hibernate4的考勤管理系统+.zip

    spring-context-4.0.0.RELEASE.jar Spring 核心提供了大量扩展 spring-core-4.0.0.RELEASE.jar Spring 框架基本的核心工具类,是其它组件的基本核心 spring-expression-4.0.0.RELEASE.jar Spring表达式语言 spring-...

    spring-beans-4.2.4.RELEASE-sources.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

    spring-expression-4.2.4.RELEASE-sources.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

    spring-core-4.2.4.RELEASE-sources.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

    spring-context-3.2.5.RELEASE.jar

    spring-context-3.2.5.RELEASE.jar 源码,本人亲测,拿来分享。

    spring-context-support-4.2.4.RELEASE.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

    官方原版源码 spring-framework-5.1.18.RELEASE.zip

    官方原版源码 spring-framework-5.1.18.RELEASE.zip

    Spring-context4.1.0的源码

    Spring-context4.1.0的源码,我们在学习Spring源码时,可以在ide中添加源码方便追溯运行过程

    spring4.3jar包源码

    包含 spring-context-4.3.12.RELEASE-sources、spring-web-4.3.12.RELEASE-sources、spring-core-4.3.12.RELEASE-sources等源码包

    Asyncweb 2.0

    这个库是自己从源代码上编译过来的,一共包括如下JAR包(二进制版本和源码版本): asyncweb-client-2.0.0-SNAPSHOT-sources.jar asyncweb-client-2.0.0-SNAPSHOT.jar asyncweb-common-2.0.0-SNAPSHOT-sources.jar ...

    spring-expression-4.2.4.RELEASE.jar

    spring-4.2.4-RELEASE....spring-context-4.2.4-RELEASE.jar,免费下载 spring-core-4.2.4-RELEASE.jar,免费下载 spring-expression-4.2.4-RELEASE.jar,免费下载 spring-context-support-4.2.4-RELEASE.jar,免费下载

Global site tag (gtag.js) - Google Analytics