1、 要想在java技术上提升一下,不看一下java源码是不行的,jdk源码,框架源码等。但是源码那么多,专门去看源码肯定很枯燥,所以就得一点一点看,坚持下去。有一点心得就记一点,如org.springframework.stereotype包下有@Controller注解。

2、见名知意,如org.springframework.context.annotation包下的一些enable*注解,DelegatingWebMvcConfiguration,这个是类包含了很多web应用的默认配置。

3、Crtl+H全局搜索文件,方法,类等。。当时遇到AspectJAutoProxy注解。想找一个包在哪里都找不到-_-,尴尬。

--------------下面开始正文--------

  Spring呢,首先是一个容器,比如帮我们装配bean,面向切面功能等,所以最重要的类应该是ApplicationContext,首先应该抓住这个类看,说不定会好点。

package org.springframework.context;
// 这是一个借口,代码很简单,但是包含的信息量还是很多的,比如各个接口
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
@Nullable
String getId(); String getApplicationName(); String getDisplayName(); long getStartupDate(); @Nullable
ApplicationContext getParent(); AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}

  接下来还是要看ApplicationContext的继承类的实现源码,搜到几篇不错的文章:https://www.jianshu.com/p/8aaad9cff96b,https://www.jianshu.com/p/2537e2fec546,其中提到了,spring中使用的Context实际上是XmlWebApplicationContext,下面看看该源码:

package org.springframework.web.context.support;

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml"; public XmlWebApplicationContext() {
}
//省略了一些方法,看到了"/WEB-INF/“是不是感觉很熟悉,没错,这就是我们要将配置文件写到这里的原因。
}

  那么问题来了,要看看AbstractRefreshableWebApplicationContext的实现,这个类中应该可以看到很多内容

package org.springframework.web.context.support;public abstract class AbstractRefreshableWebApplicationContext extends AbstractRefreshableConfigApplicationContext implements ConfigurableWebApplicationContext, ThemeSource {
@Nullable
private ServletContext servletContext;
@Nullable
private ServletConfig servletConfig;
@Nullable
private String namespace;
@Nullable
private ThemeSource themeSource;
public AbstractRefreshableWebApplicationContext() {
this.setDisplayName("Root WebApplicationContext");
}
public void setServletContext(@Nullable ServletContext servletContext) {
this.servletContext = servletContext;
}
@Nullable
public ServletContext getServletContext() {
return this.servletContext;
}
//这里仍然只列举一些简要的代价,其中可以看到该对象可以获取到ServletContext,作为一个容器,要能访问(容纳)更多的对象
}

  AbstractRefreshableWebApplicationContext类中,方法不多,接下来还得看AbstractRefreshableConfigApplicationContext,这个类代码也不多,还得看其父类

AbstractRefreshableApplicationContext。
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
@Nullable
private Boolean allowBeanDefinitionOverriding;
@Nullable
private Boolean allowCircularReferences;
@Nullable
private DefaultListableBeanFactory beanFactory;
private final Object beanFactoryMonitor = new Object(); public AbstractRefreshableApplicationContext() {
}
//当然也省略了很多代码,追根溯源,马上就到根了,大多数代码还是在AbstractApplicationContext中,
}

   在看一下spring的启动代码:

package org.springframework.web;

@javax.servlet.annotation.HandlesTypes({org.springframework.web.WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements javax.servlet.ServletContainerInitializer {
public SpringServletContainerInitializer() { /* compiled code */
public void onStartup(@org.springframework.lang.Nullable java.util.Set<java.lang.Class<?>> webAppInitializerClasses,
javax.servlet.ServletContext servletContext)
throws javax.servlet.ServletException { /* compiled code */ }
} 

这段代码是springboot的启动代码。 

1.注解不仅可以用在方法上,也可以用在参数上。

2.在servlet3.0环境中,容器会查找实现了javax.servlet.ServletContainerInitializer接口的类,如代码中的SpringServletContainerInitializer,就会使用其配置servlet容器。而该类又会查找实现了WebApplicationInitializer的类,AbstractAnnotationConfigDispatcherServletInitializer就实现了该类。


参考文章:https://www.jianshu.com/p/2854d8984dfc

慢慢看Spring源码的更多相关文章

  1. Spring源码分析(一):从哪里开始看spring源码(系列文章基于Spring5.0)

    概述 对于大多数第一次看spring源码的人来说,都会感觉不知从哪开始看起,因为spring项目源码由多个子项目组成,如spring-beans,spring-context,spring-core, ...

  2. 如何看Spring源码

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

  3. 零基础带你看Spring源码——IOC控制反转

    本章开始来学习下Spring的源码,看看Spring框架最核心.最常用的功能是怎么实现的. 网上介绍Spring,说源码的文章,大多数都是生搬硬推,都是直接看来的观点换个描述就放出来.这并不能说有问题 ...

  4. 看Spring源码不得不会的@Enable模块驱动实现原理讲解

    这篇文章我想和你聊一聊 spring的@Enable模块驱动的实现原理. 在我们平时使用spring的过程中,如果想要加个定时任务的功能,那么就需要加注解@EnableScheduling,如果想使用 ...

  5. Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器

    本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...

  6. Spring源码分析-BeanFactoryPostProcessors 应用之 PropertyPlaceholderConfigurer

    BeanFactoryPostProcessors 介绍 BeanFactoryPostProcessors完整定义: /** * Allows for custom modification of ...

  7. spring源码分析(一)IoC、DI

    创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...

  8. spring源码学习之路---AOP初探(六)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 最近工作很忙,但当初打算学习 ...

  9. Spring源码情操陶冶-ComponentScanBeanDefinitionParser文件扫描解析器

    承接前文Spring源码情操陶冶-自定义节点的解析,本文讲述spring通过context:component-scan节点干了什么事 ComponentScanBeanDefinitionParse ...

随机推荐

  1. Redis进阶之redis的生命周期

    D:\Redis-x64-3.2.100>redis-cli.exe -h 127.0.0.1 -p 6379127.0.0.1:6379> set aa "123"( ...

  2. WIN10平板 传递优化文件能否删除

    在给系统准备做Ghost备份之前,一般会运行一次磁盘清理,但是WIN10系统多了一个传递优化文件(现在看到的体积很小,但其实可能是4-5G) 这个文件只是WIN10改进了系统更新策略产生的,就像是BT ...

  3. iOS 录制视频MOV格式转MP4

    使用UIImagePickerController系统控制器录制视频时,默认生成的格式是MOV,如果要转成MP4格式的,我们需要使用AVAssetExportSession; 支持转换的视频质量:低, ...

  4. linux网络设备—mdio总线

    一.结构体 struct mii_bus { const char *name; //总线名 char id[MII_BUS_ID_SIZE]; //id void *priv; //私有数据 int ...

  5. 使用yocs_velocity_smoother对机器人速度进行限制

    yocs_velocity_smoother是一个速度.加速度限制器,用来防止robot navigation的速度/转速过快,加速度/快减速过大.Bound incoming velocity me ...

  6. windows IOCP入门的一些资料

    最近需要看一个中间件的IOCP处理模块,需要了解这方面的知识 https://www.codeproject.com/articles/13382/a-simple-application-using ...

  7. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    SpringBoot项目编译成功,启动报错 提示信息很明显,通过查看依赖关系,可以找到原因 导致这个问题的原因是因为,在 pom.xml 配置文件中,配置了数据连接技术 spring-boot-sta ...

  8. nginx做负载均衡时其中一台服务器挂掉宕机时响应速度慢的问题解决

    nginx会根据预先设置的权重转发请求, 若给某一台服务器转发请求时,达到默认超时时间未响应,则再向另一台服务器转发请求. 默认超时时间1分钟. 修改默认超时时间为1s: server { liste ...

  9. [svc]find+xargs/sed&sed后向引用+awk多匹配符+过滤行绝招总结&&产生随机数

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz find,文件+超过7天+超过1M的+按日期为文 ...

  10. Android Studio Prettify 插件

    1.功能:能够一键声明layout文件中的所有注明id的控件,节省时间 2.github地址 https://github.com/Haehnchen/idea-android-studio-plug ...