慢慢看Spring源码
1、 要想在java技术上提升一下,不看一下java源码是不行的,jdk源码,框架源码等,java的源码相对于c/c++,还是很容易看懂的。但是源码那么多,专门去看源码肯定很枯燥,所以就得一点一点看,坚持下去。有一点心得就记一点,如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的启动代码:
1 2 3 4 5 6 7 | 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, <br>javax.servlet.ServletContext servletContext) <br> throws javax.servlet.ServletException { /* compiled code */ } } |
这段代码是springboot的启动代码。
1.注解不仅可以用在方法上,也可以用在参数上。
2.在servlet3.0环境中,容器会查找实现了javax.servlet.ServletContainerInitializer接口的类,如代码中的SpringServletContainerInitializer,就会使用其配置servlet容器。而该类又会查找实现了WebApplicationInitializer的类,AbstractAnnotationConfigDispatcherServletInitializer就实现了该类。
参考文章:https://www.jianshu.com/p/2854d8984dfc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义