Spring源码 - 核心、容器

核心思想

  • IOC:控制反转,将原本由开发者管理Bean的生命周期转到容器内部,开发者在需要Bean实例时直接从容器中获取;
    因为容器在启动过程过程中,将解析配置文件和注解将配置的Bean解析初始化到容器中
  • DI:依赖注入,容器负责维护对象之间的依赖关系,通过对Bean的配置、注解等方式提醒容器将对应的值注入到该Bean的实例对应属性
  • AOP:面向切面编程,通过动态代理模式实现无侵入式编程,通过AOP可以将日志管理、事务处理、异常处理等通用
    模块抽离出来,提高代码复用性,减少系统模块间的耦合度
容器
  • 容器
    • Servlet容器、web容器 ServletContext
        //web容器(tomcat等)启动加载web.xml配置文件,将配置信息封装成ServletContext
        
        //spring配置
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring-dal.xml;classpath*:/spring-service*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        //mvc 配置
        <servlet>
            <servlet-name>springServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
    • Spring容器、BeanFactory
      • 入口
          配置监听器ContextLoaderListener(ServletContextListener)监听web容器启动,
          在监听事件中调用Spring容器初始化方法initWebApplicationContext(ServletContext sc)
      
      • 获取WebApplicationContext赋值this.context
          通过反射获取 XmlWebApplicationContext 实例,可配置contextClass项来指定获取的类,
          默认为Spring提供的 ContextLoader.properties 中配置的 XmlWebApplicationContext
      
      • refresh()
          ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
          cwac.refresh();
      
      • 将context设置为sc的属性,属性名为 WebApplicationContext.root
    • MVC容器、BeanFactory WebApplicationContext
      • 入口
          配置DispatcherServlet,在init()中调用初始化方法
      
      • 获取WebApplicationContext
          WebApplicationContext rootContext = sc.getAttribute("WebApplicationContext.root");
          ConfigurableWebApplicationContext wac = BeanUtils.instantiateClass("XmlWebApplicationContext");
          wac.setParent(rootContext);
      
      • wac.refresh()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值