ApplicationContext的实现

ApplicationContext 的实现

ClassPathXmlApplicationContext

从类路径下读取 Spring 配置文件

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class ApplicationContextMainV1 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 上面applicationContext所做的事情大致可以被以下流程概括:
        // 1. applicationContext内部也是创建一个beanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 2. 通过BeanDefinitionReader来读取配置文件, 将Bean对象注入到BeanFactory中
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        beanDefinitionReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
    }
}

FileSystemXmlApplicationContext

从磁盘路径下读取 Spring 配置文件

AnnotationConfigApplicationContext

基于 Java 配置类

AnnotationConfigServletWebServerApplicationContext

用于 Web 应用

import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * web 应用至少需要三个基本的 Bean
 */
@Configuration
public class WebServletConfig {

    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }

    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }

    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet) {
        return new DispatcherServletRegistrationBean(dispatcherServlet, "/");
    }

}

配置 Controller(使用 Spring 的方式来配置,而不是使用 SpringBoot 中的注解 @Controller

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.Controller;

@Configuration
public class ControllerConfig {
    /**
     * 控制器的Bean的命名需要以 "/" 开头
     */
    @Bean("/hello")
    public Controller helloController() {
        // Controller 接口中只有一个方法, 即 handleRequest()
        return (request, response) -> {
            // 拿到字符输出流, 向其中写入"Hello, Spring"
            response.getWriter()
                    .print("Hello, Spring");
            return null;
        };
    }
}

   转载规则


《ApplicationContext的实现》 熊水斌 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
Crossformer Crossformer
Crossformer(2023 ICLR)引言MTS 介绍MTS(multivariate time series)中每一个维度都代表一个时间序列,且维度与维度之间是相互关联的,利用其它维度的历史信息可以更好地预测当前维度的信息。例如预测
2023-04-04
下一篇 
HTTP2 HTTP2
HTTP2sequenceDiagram participant Client participant Server Client->>Server: ClientHello Server-->>Client
2023-04-03
  目录