博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何看Spring源码、Java每日六道面试分享,打卡第二天
阅读量:4178 次
发布时间:2019-05-26

本文共 10234 字,大约阅读时间需要 34 分钟。

想要深入的熟悉了解Spring源码,我觉得第一步就是要有一个能跑起来的极尽简单的框架,下面我就教大家搭建一个最简单的Spring框架,而且是基于Java Config形式的零配置Spring框架。

首先第一步创建一个空的maven web项目,这步很简单,自行百度。

在maven项目的pom.xml文件中添加Spring基础依赖:

 
        
            
4.3.7.RELEASE
            
1.7.21
            
2.8.2
            
1.2
        
        
        
            
                
org.springframework
                
spring-framework-bom
                
${spring.version}
                
pom
                
import
            
        
    
    
        
            
org.springframework
            
spring-context
        
        
            
org.springframework
            
spring-web
        
        
            
org.springframework
            
spring-webmvc
        
        
            
org.springframework
            
spring-core
        
        
            
org.springframework
            
spring-beans
        
        
            
org.springframework
            
spring-aop
        
        
        
            
org.apache.logging.log4j
            
log4j-api
            
${log4j.version}
        
        
            
org.apache.logging.log4j
            
log4j-core
            
${log4j.version}
        
        
        
            
org.apache.logging.log4j
            
log4j-web
            
${log4j.version}
        
        
 
            
org.apache.logging.log4j
            
log4j-slf4j-impl
            
${log4j.version}
        
        
 
            
org.apache.logging.log4j
            
log4j-jcl
            
${log4j.version}
        
        
            
org.slf4j
            
slf4j-api
            
${slf4j.version}
        
    
            
4.3.7.RELEASE
            
1.7.21
            
2.8.2
            
1.2
                 
        
            
                
org.springframework
                
spring-framework-bom
                
${spring.version}
                
pom
                
import
            
        
    
    
        
            
org.springframework
            
spring-context
        
        
            
org.springframework
            
spring-web
        
        
            
org.springframework
            
spring-webmvc
        
        
            
org.springframework
            
spring-core
        
        
            
org.springframework
            
spring-beans
        
        
            
org.springframework
            
spring-aop
        
        
        
            
org.apache.logging.log4j
            
log4j-api
            
${log4j.version}
        
        
            
org.apache.logging.log4j
            
log4j-core
            
${log4j.version}
        
        
        
            
org.apache.logging.log4j
            
log4j-web
            
${log4j.version}
        
        
 
            
org.apache.logging.log4j
            
log4j-slf4j-impl
            
${log4j.version}
        
        
 
            
org.apache.logging.log4j
            
log4j-jcl
            
${log4j.version}
        
        
            
org.slf4j
            
slf4j-api
            
${slf4j.version}
        
    

我推荐搭建基于Java Config形式的Spring框架,不需要配置文件,全部使用Java代码形式来定义,简洁明了,对于想要深入了解Spring源码来说这点很重要,否则可能需要看非常多的Spring解析XML配置文件的解析类,对于任何人都不是很容易的功夫。而使用Java Config形式能直接看到配置类的运行流程,对了解Spring源码很有帮助。

要搭建基于Java Config形式的Spring框架,要求Servlet-api版本3.0以上,同时推荐Spring版本4.0以上,日志可以使用我另一篇博客上的log4j2的配置,自己修改下输出日志文件路径就可以了。

 
public class WebContextInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    @Override    protected Class
[] getRootConfigClasses() {        return new Class
[] {RootContextConfig.class};    }}@Configuration@ComponentScanpublic class RootContextConfig {    @Bean    public Child child() {        return new Child();    }}public class Child {}@Componentpublic class Mother {}class WebContextInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override     protected Class
[] getRootConfigClasses() {
        return new Class
[] {RootContextConfig.class};     } } @Configuration @ComponentScan public class RootContextConfig {
    @Bean     public Child child() {
        return new Child();     } } public class Child {
} @Component public class Mother {
}

把这几个类都放在一个包下,然后启动tomcat,应该就可以启动Spring了,这就是一个最基本的Spring框架,而且包含了Java Config的Bean的定义以及组件扫描,RootContextConfig这个类就是容器的配置类,之后就可以开始跟着Spring框架的启动流程看了,DEBUG跟着一步一步的走。

WebContextInitializer 的父类的将RootContextConfig 当做配置类生成一个AnnotationConfigWebApplicationContext 类型ApplicationContext容器,注入到ContextLoaderListener中。

 
    ......    protected WebApplicationContext createRootApplicationContext() {        Class
[] configClasses = getRootConfigClasses();        if (!ObjectUtils.isEmpty(configClasses)) {            AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();            rootAppContext.register(configClasses);            return rootAppContext;        }        else {            return null;        }    }    ......    protected void registerContextLoaderListener(ServletContext servletContext) {        WebApplicationContext rootAppContext = createRootApplicationContext();        if (rootAppContext != null) {            ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);            listener.setContextInitializers(getRootApplicationContextInitializers());            servletContext.addListener(listener);        }        else {            logger.debug("No ContextLoaderListener registered, as " +                    "createRootApplicationContext() did not return an application context");        }    }protected WebApplicationContext createRootApplicationContext() {
        Class
[] configClasses = getRootConfigClasses();         if (!ObjectUtils.isEmpty(configClasses)) {
            AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();             rootAppContext.register(configClasses);             return rootAppContext;         }         else {
            return null;         }     }     ......     protected void registerContextLoaderListener(ServletContext servletContext) {
        WebApplicationContext rootAppContext = createRootApplicationContext();         if (rootAppContext != null) {
            ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);             listener.setContextInitializers(getRootApplicationContextInitializers());             servletContext.addListener(listener);         }         else {
            logger.debug("No ContextLoaderListener registered, as " +                     "createRootApplicationContext() did not return an application context");         }     }

不要对Tomcat内的源码花时间,主要是看Spring的源码,之后就可以看ContextLoaderListener的contextInitialized(…)方法了,Spring容器就是在这个方法里初始化生成的。如何初始化,这个太复杂了,需要花非常多的时间去看,去思考的,这里就不讲了,不过我可以说一些我自己总结的小技巧:

  1. 说是看源码,其实应该叫看和想。Spring源码很复杂,我觉得花在思考上的时间至少要和看的时间对等。看了,如果没有花时间想明白,等于白看。

  2. 理解重于记忆。Spring的流程很长,东西很多,如果单纯靠记忆肯定记不过来的,知道每个组件的大体作用以及作用的节点就够了,源码就在那里,又不会跑,记不清了翻翻就看到了,多翻几次就能够慢慢记住了,最开始看的时候不要执着于记忆。

  3. 多做笔记。Spring组件很多,在最开始看的时候,推荐做些笔记,将每个重要接口的作用及关键代码记录下,有时间就看看,最先了解的组件是你切入Spring源码的切口,借助他们能关联到其他的组件。

  4. 多百度。在看一些关键接口或者类时,如果其代码很复杂,先百度下吧,先对其功能有个了解,然后对照着功能看代码会有很大的帮助。

  5. 要多遍地看,反复地看。别想着看一遍就能看明白,在最开始的几次跟着初始化流程看源码时,不要执着于某个细节。先对Spring所有的组件功能有个大体了解,对初始化流程有个大体的了解,这是深入的基础。

  6. 多看日志,多DEBUG。多看日志能够提高你对Spring流程的了解程度,而且在排错时能有效提高效率;DEBUG是看源码最好的一种方式,是解疑的最直接途径,对于有些运行时难以触及的代码,需要你手动创造条件让流程走入此处。

  7. 多总结,多动手。不要仅局限于看和思考,要动手。源码看的仔细,基本能从源码上看出很多Spring组件的使用方式,总结各种组件的使用方法,然后自己定义相应的组件,将其引入Spring中,查看其作用流程,这是你拓展Spring的第一步,既能增强对Spring的理解,也能提高你对Spring的拓展能力。

  8. 不断完善框架。每熟悉一种组件的使用,就添加到自己的 框架中,不断完善自己的框架,到最后你的框架能够整合Spring大部分好用组件,也是你以后回顾Spring框架的最大助力。

  9. 多看注释,看方法的名称,参数和返回值。看注释,理解类,属性和方法的作用,着重第一段注释;看方法的名称,参数和返回值能对方法的作用有很明显的说明。

以上就是我自己看Spring总结的一些小技巧,希望对你们有些助益。如果在看Spring源码的过程中有些疑问,可以在回复里提,我会尽量帮助解答。

文章转自:https://blog.csdn.net/qq_27529917/article/details/79209846

第二天学习打卡

640?wx_fmt=jpeg

大家有问题也可以在评论区写出来,记载在第二天公众号里面发出来一起探讨。。

今天的主题是关于面试题解答,求职是在每个技术人员的生涯中都要经历多次。对于我们大部分人而言,在进入自己心仪的公司之前少不了准备工作,

所以如果大家觉得这种模式不错的,欢迎大家评论,会坚持下去,每天分享6个面试常问题。

一方面帮助跳槽季来临之时。不时之需,一方面帮助大家巩固下基础,帮助活到学到老的机会 哈哈、另一方面也希望帮助想要换工作的朋友。

坚持半个月试一下、回答问题不仅提升了自己、更加帮助更多程序员们、哈哈 记得坚持来打卡。

640答:

1.

2.

3.

4.

5.

6.

欢迎置顶公众号、晚上10点公众号公布答案、评论区见。

你可能感兴趣的文章
SQL性能优化梳理
查看>>
微服务架构技术栈
查看>>
想面试进BAT,不得不看的分布式锁,面试题都在这里了!!
查看>>
Redis最常被问到知识点总结
查看>>
这才是微服务拆分的正确姿势,值得学习!
查看>>
MySQL中一条SQL是如何执行的?
查看>>
MySQL的索引是什么?怎么优化?
查看>>
2万字长文包教包会 JVM 内存结构
查看>>
不懂 spring 就彻底放弃 Java 吧!
查看>>
从MySQL高可用架构看高可用架构设计
查看>>
可以秒杀全场的SpringCloud微服务电商实战项目,文档贼全!
查看>>
java架构之路(多线程)synchronized详解以及锁的膨胀升级过程
查看>>
java架构之路(多线程)AQS之ReetrantLock显示锁的使用和底层源码解读
查看>>
百度现场面试:JVM+算法+Redis+数据库!(三面)
查看>>
java架构之路(多线程)JMM和volatile关键字
查看>>
创业感悟:技术兄弟为什么一直没有起来
查看>>
(转载)linux命令之十八locate 命令
查看>>
Linux发行光盘(红旗 5.0 SP2发行版,已不使用仅参考)
查看>>
linux下如何将文件打包、压缩并分割成制定大小
查看>>
CentOS6.5升级内核到3.10.28
查看>>