Phân tích mã nguồn Spring từng bước

Để nâng cao kỹ năng Java, việc xem xét mã nguồn JDK và các framework là điều bắt buộc. Mã nguồn Java dễ hiểu hơn so với C/C++, nhưng việc nghiên cứu toàn bộ sẽ rất nhàm chán. Ta nên tiếp cận từng phần nhỏ, ghi chú lại những gì thấy thú vị như annotation @Controller trong gói org.springframework.stereotype.

Các annotation có tên gợi ý chức năng như enable* trong org.springframework.context.annotation, hay lớp DelegatingWebMvcConfiguration chứa cấu hình mặc định cho ứng dụng web.

Sử dụng Ctrl+H để tìm kiếm toàn bộ dự án. Khi gặp khó khăn với AspectJAutoProxy, ta học được cách tìm kiếm hiệu quả hơn.

package org.springframework.context;
// Giao diện nền tảng với nhiều chức năng quan trọng
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;
}

Tiếp tục phân tích lớp triển khai của ApplicationContext, ví dụ 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() {
    }<br></br>// Đường dẫn "/WEB-INF/" xác nhận lý do đặt file cấu hình tại thư mục này<br></br>}

Khám phá lớp cha 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;<br></br>    }<br></br> // Lớp này cung cấp truy cập đến ServletContext cho container
    }

Quá trình phân tích tiếp tục với 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() {
    }<br></br>// Hầu hết logic tập trung trong AbstractApplicationContext
}

Ví dụ khởi động 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, <br></br>javax.servlet.ServletContext servletContext) <br></br>throws javax.servlet.ServletException { /* compiled code */ }
} 

Các chú thích (annotations) có thể áp dụng cho tham số, không chỉ phương thức. Trong môi trường Servlet 3.0, SpringServletContainerInitializer sẽ cấu hình Servlet container thông qua các lớp implement WebApplicationInitializer.

<br></br><br></br>Tham khảo: https://www.jianshu.com/p/2854d8984dfc

Thẻ: spring-framework java-source-code application-context annotation-processing web-container-initialization

Đăng vào ngày 16 tháng 6 lúc 12:11